//Set the location of the CFM file. SHOULD BE in the config area of functions.js
var URL = "/pollen/getPollenByZip.cfm";
var chart_colors = {
					"low": "#008c00",
					"low_medium": "#99ff33",
					"medium": "#feff00",
					"medium_high": "#fa9d00",
					"high": "#f22e24"
					}
		
$(document).ready(function(){	
	pollen_init();
						   
	/************ Pollen count widget **********/
	// Add the click event to the submit button
	$('#submitBtn').click(function(){
		if (/^\d{5}(-\d{4})?$/.test($('#zipField').val())) {
			getPollen($('#zipField').val());
		} else {
			$("#zipCodeLabel, #zipCodeLabel2").html("Please re-enter a valid ZIP code");
			$("#zipCodeLabel, #zipCodeLabel2").addClass("zipError");
			$('#pollenLocation').html("");
			$('#pollenLocation').addClass("noDataTxt");
			$('#pollenTxt').html("");
			$('#pollenTxt').addClass("noDataTxt2");
			$('#pollenNum').html("");
			$('#pollenNum').addClass("noDataDigit");
			$(".pollenChart").css({backgroundPosition: '0px 0px', width: '0px'});
			$(".pollenChart").animate({width: '82px'}, {duration: 800});
			$('#pollenZipTxt').html("");
			$(".pollenChart, .chartPollenLeft").css({backgroundPosition: '0px 0px', width: '0px'});
			$('#predominantPollen').html("");

			//The mini chart on the homepage and on the pollen forecast page.
			if ($(document).find("#homepagePollenCount").length > 0) {
					$(".pollenChart").animate({width: '82px'}, {duration: 800});
				}else{
					$('.chartPollenLeft').animate({width: '153px'}, {duration: 800});
				}

			$('#pollenLink').hide();
			$('#grayPollenTxt').show();

			//The main chart on pollen forecast page
			if ($(document).find("#pollenForecastChart").length > 0) {
				$('#day1Chart, #day2Chart, #day3Chart, #day4Chart').css("height", "0px");
				$('#day1Num, #day2Num, #day3Num, #day4Num').html("");
				$('#day1Num, #day2Num, #day3Num, #day4Num').addClass("noDataChart");
			}
		}

		return false;
	});
	
	// Add the keypress event for the Enter button on the zip field
	$('#zipField').keyup(function(e) {
		if(e.keyCode == 13) {
			getPollen($('#zipField').val());
			return false;									
		}
		return false;
	});

}).ajaxStart(function() {
	$('.loading').show();  	
}).ajaxStop(function() {
	$('.loading').hide();	
});

function pollen_init(){
	/************ Pollen count widget **********/
	//set the 4 days for the pollen count chart
	if ($(document).find("#pollenForecastChart").length > 0){
		var cur_day = new Date();
		var weekday = new Array(7);
		weekday[0]="Sunday";
		weekday[1]="Monday";
		weekday[2]="Tuesday";
		weekday[3]="Wednesday";
		weekday[4]="Thursday";
		weekday[5]="Friday";
		weekday[6]="Saturday";
		
		var starting_day = cur_day.getDay();
		
		for (j=0;j<4;j++){
			var cur_day_ele = "#day" + (j+1);
			var cur_day_week = (starting_day + j) % 7;		
			$(cur_day_ele).html(weekday[cur_day_week]);
		}// end for
			
	}// end if
	
	// Get the pollen initially when the page loads
	if (getURLPara('zipField') != ""){
		getPollen(getURLPara('zipField'));
	}else{
		if (readCookie('zipcode')!= null){
			getPollen(readCookie('zipcode'));
		}
	}
	
	// Clear the zip field text input
	$('#zipField').val('')
	
	// Add the loading indicator
	var loading_img_path = root_path + "images/loading.gif";
	$('.loading').append('<img src="'+loading_img_path+'" class="pushDown2">').css({display:"none"});
}


/******************************************************************** 
***  function for Pollen Count Widget     			              ***
*********************************************************************/
function getPollen(zip){																				
	$.ajax({
		url: zip ? URL + "?zipcode=" + zip : URL,
		error: function(){
			$("#zipCodeLabel, #zipCodeLabel2").html("The server is unavailable at the time.");
			$("#zipCodeLabel, #zipCodeLabel2").addClass("zipError");
		},
		success: function(data){															  
			var pollenArray = data.split("|");
			$('#pollenLocation').removeClass("noDataTxt");
			$('#pollenTxt').removeClass("noDataTxt2");
			$('#pollenNum').removeClass("noDataDigit");
			$('#day1Num, #day2Num, #day3Num, #day4Num').removeClass("noDataChart");
			$("#zipCodeLabel, #zipCodeLabel2").removeClass("zipError");
			$("#zipCodeLabel, #zipCodeLabel2").html("Enter your ZIP code");
			
			if (parseFloat(pollenArray[1]) != 204) {
				if (parseFloat(pollenArray[1]) != 203){	
					$('#pollenLocation').html(pollenArray[1] + ", " + pollenArray[2]);
					$('#pollenNum').html(pollenArray[3]);
					var pollen_num = parseFloat(pollenArray[3]);
					var pollen_txt = "";
					
					if (pollen_num <= 0.5){
						var pollen_chart = -15;
					}else{
						var pollen_chart = -(Math.round(pollen_num) * 15);
					}
					
					var pollen_chart_position = "0px " + pollen_chart +"px"
					var pollen_plants = pollenArray[7].split(", ");
					
					if ((pollen_num >= 0) && (pollen_num <= 2.4)){
						pollen_txt = "Low";
					}else if ((pollen_num >= 2.5) && (pollen_num <= 4.8)){
						pollen_txt = "Low-Med";
					}else if ((pollen_num >= 4.9) && (pollen_num <= 7.2)){
						pollen_txt = "Medium";
					}else if ((pollen_num >= 7.3) && (pollen_num <= 9.6)){
						pollen_txt = "Med-High";
					}else if (pollen_num >= 9.7){
						pollen_txt = "High";
					}
					$('input:text#zipField').val(jQuery.trim(pollenArray[0]));
					$('#pollenTxt').html(pollen_txt);
					$('#grayPollenTxt').hide();
					$("#pollenLink").show();
					$('.pollenChart, .chartPollenLeft').css({backgroundPosition: pollen_chart_position, width: '0px'});
					$("#zipCodeLabel, #zipCodeLabel2").removeClass("zipError");
					$("#zipCodeLabel, #zipCodeLabel2").html("See another ZIP code");
					
					//The mini chart on the homepage and on the pollen forecast page.
					if ($(document).find("#homepagePollenCount").length > 0){
						$('.pollenChart').animate({width: '82px'}, {duration: 'slow', easing: 'easeInQuad'});
					}else{
						$('.chartPollenLeft').animate({width: '153px'}, {duration: 'slow', easing: 'easeInQuad'});
						$('#pollenZipTxt').html("(" + pollenArray[0] +")");
					}
					
					//The main chart on pollen forecast page
					if ($(document).find("#pollenForecastChart").length > 0){
						$('#day1Num').html(pollenArray[3]);
						$('#day1Chart').css("background-color", chartColor(parseFloat(pollenArray[3])));
						$('#day1Chart').animate({height: ((147*parseFloat(pollenArray[3])/12)) + 'px'}, {duration: 'slow', easing: 'easeInCubic'});
						$('#day2Num').html(pollenArray[4]);
						$('#day2Chart').css("background-color", chartColor(parseFloat(pollenArray[4])));
						$('#day2Chart').animate({height: ((147*parseFloat(pollenArray[4])/12)) + 'px'}, {duration: 'slow', easing: 'easeInCubic'});
						$('#day3Num').html(pollenArray[5]);
						$('#day3Chart').css("background-color", chartColor(parseFloat(pollenArray[5])));
						$('#day3Chart').animate({height: ((147*parseFloat(pollenArray[5])/12)) + 'px'}, {duration: 'slow', easing: 'easeInCubic'});
						$('#day4Num').html(pollenArray[6]);
						$('#day4Chart').css("background-color", chartColor(parseFloat(pollenArray[6])));
						$('#day4Chart').animate({height: ((147*parseFloat(pollenArray[6])/12)) + 'px'}, {duration: 'slow', easing: 'easeInCubic'});
					}
							
					$('#predominantPollen').html("");
					for (i=0;i<pollen_plants.length;i++){
						if (i < (pollen_plants.length - 1)){
							$('#predominantPollen').append("<li>" + pollen_plants[i] + "</li>");
						}else{
							var more_pollen_plants = pollen_plants[i].split(" and ");
							for (var j=0;j<more_pollen_plants.length;j++){
								$('#predominantPollen').append("<li>" + more_pollen_plants[j] + "</li>");
							}

						}
					}
				
				eraseCookie('zipcode');
				createCookie('zipcode', zip, 7);
				
					
				}// end if not 203
			}else{
				// 204 error code when the zipcode is NOT found.
				$("#zipCodeLabel, #zipCodeLabel2").html("Please re-enter a valid ZIP code");
				$("#zipCodeLabel, #zipCodeLabel2").addClass("zipError");
				$('#pollenLocation').html("");
				$('#pollenLocation').addClass("noDataTxt");
				$('#pollenTxt').html("");
				$('#pollenTxt').addClass("noDataTxt2");
				$('#pollenNum').html("");
				$('#pollenNum').addClass("noDataDigit");
				$(".pollenChart").css({backgroundPosition: '0px 0px', width: '0px'});
				$(".pollenChart").animate({width: '82px'}, {duration: 800});
				$('#pollenZipTxt').html("");
				$(".pollenChart, .chartPollenLeft").css({backgroundPosition: '0px 0px', width: '0px'});
				$('#predominantPollen').html("");
				
				//The mini chart on the homepage and on the pollen forecast page.
				if ($(document).find("#homepagePollenCount").length > 0){
						$(".pollenChart").animate({width: '82px'}, {duration: 800});
					}else{
						$('.chartPollenLeft').animate({width: '153px'}, {duration: 800});
					}
				
				$('#pollenLink').hide();
				$('#grayPollenTxt').show();
				
				//The main chart on pollen forecast page
				if ($(document).find("#pollenForecastChart").length > 0){
					$('#day1Chart, #day2Chart, #day3Chart, #day4Chart').css("height", "0px");
					$('#day1Num, #day2Num, #day3Num, #day4Num').html("");
					$('#day1Num, #day2Num, #day3Num, #day4Num').addClass("noDataChart");
				}// end the main chart
			}// end if not 204
		} // success
	});
}

function chartColor(p_num){
	if ((p_num >= 0) && (p_num <= 2.4)){
		bg_color = chart_colors["low"];
	}else if ((p_num >= 2.5) && (p_num <= 4.8)){
		bg_color = chart_colors["low_medium"];
	}else if ((p_num >= 4.9) && (p_num <= 7.2)){
		bg_color = chart_colors["medium"];
	}else if ((p_num >= 7.3) && (p_num <= 9.6)){
		bg_color = chart_colors["medium_high"];
	}else if (p_num >= 9.7){
		bg_color = chart_colors["high"];
	}
	return bg_color;
}

function getURLPara(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return results[1];
}





/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright Ã‚Â© 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

/*
 *
 * TERMS OF USE - EASING EQUATIONS
 * 
 * Open source under the BSD License. 
 * 
 * Copyright Ã‚Â© 2001 Robert Penner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */