/*************
*
*Weather.js
*	retrieves weather information
*		Requires: Prototype
****************/

Weather = {
		WeatherAlertBase: 'http://www.hanford.gov/amh/',
		FrontPage: false,
		Footer: true,
		FooterClass:  'footerTemperature',
		FrontPageWeatherLoc : 'weatherContent',
		weatherPage : 'http://www.hanford.gov/amh/Weather/weather.cfc?method=getDataJSON',
		weatherPageJSONP: 'http://www.hanford.gov/amh/Weather/weather.cfc?method=getDataJSONP',
		waitBetweenLoads: 60, // in Seconds 	
		jsonPre: '//',
		base: '',
		iconLocation : 'appimages/hanfordGov/weather/',
		footerTemplate: 'Current Temperature: #{TEMP}&deg;F',
		headerTemplate: '<table width="100%" style="padding-bottom:15px;"><tr><td colspan=2 class="weatherHeadline">Current Conditions</td></tr>'
						+'<tr><td align="center" width="50%"><img width=95 height=95 src="#{iconLocation}#{icon}" alt="#{currentText}"></td><td align="center"><span class="currentTemp">#{currentTemp}&deg;F</span><span class="currentHighLow">#{currentWind}</span><span class="currentText">#{currentText}</span>'
						+'</td>'
						+'</tr>'
						+'<tr valign="top" class="forecast"><td align="center">Today: #{todayHigh}&deg;F (#{todayLow}&deg;F)<br> #{todayText}</td>'
						+'<td align="center"> Tomorrow: #{tomorrowHigh}&deg;F (#{tomorrowLow}&deg;F)<br>#{tomorrowText}</td>'
						+'</tr>'
			+'</table>', /* <a href="#{WeatherAlertBase}pageAction.cfm/weather"> */
		weatherAlertTemplate : '<div class="BoxSuround" type="weatherAlert"><div class="BoxHeader">&nbsp;</div><div class="BoxContent"><a href="#{Link}">#{WeatherText}</a></div><div class="BoxFooter"></div></div>',
		useJSONPAlways: true,
		JSONPCall : function()
		{
			var scriptElement = new Element('script', { src: this.weatherPageJSONP + '&JSONP=Weather.useData' } )
			$$('head')[0].insert(scriptElement);
		},
		cleanJSON : function (str)
		{
			if ( str.substr(0, this.jsonPre.length) == this.jsonPre)
				str = str.substr(this.jsonPre.length);
			return str;
		},
		initialize : function()
		{
			var tmpBases = document.getElementsByTagName("base");
			
			if (tmpBases.length > 0)
				this.base = tmpBases[0].href;
			if ( this.FrontPage || this.Footer )
			{
				this.attemptLoad();
			}
		},
		attemptLoad : function()
		{
			var tmpStr = LinkProcessor.parseUri(this.base);
			if ( LinkProcessor.parseUri(this.weatherPage).protocol != "") 
			{
				this.useJSONPAlways = true;
			}
				var outHost = tmpStr["host"];
				var currentHost = window.location.host;
				if (!this.useJSONPAlways &&  outHost == currentHost )
				{
			new Ajax.Request(this.base + this.weatherPage, {
				onSuccess :  this.attemptLoad_onData.bindAsEventListener(this)
				});
				} else {
			this.JSONPCall();
				}
				
		},
		useData: function(resp)
		{
			if ( resp.Dirty == 1)
			{
				window.setTimeout(this.attemptLoad.bindAsEventListener(this), this.waitBetweenLoads * 1000 );				
			if ( resp.CacheStatus == "New")
			{
				var arrElms = $$("." + this.FooterClass);
				for ( var i =0; i < arrElms.length; i++)
				{
					arrElms[i].innerHTML =  "processing..."
				}
				var arrElms = $$("." + this.FrontPageWeatherLoc);
				for ( var i =0; i < arrElms.length; i++)
				{
					arrElms[i].innerHTML =  "processing...";
				}
				
			}
			}
			
				if ( this.Footer )
					this.updateFooter(resp.Current.Temperature);
				if ( this.FrontPage)
					this.updateFrontPage(resp);
		},
		
		attemptLoad_onData: function(transport)
		{
			var resp = eval( "(" + this.cleanJSON(transport.responseText) + ")");
		
			
			this.useData(resp);

			
		},
		updateFooter: function(temp)
		{
			var arrElms = $$("." + this.FooterClass);
			var t = new Template(this.footerTemplate);		

			var str = t.evaluate({TEMP: temp });
			for ( var i =0; i < arrElms.length; i++)
			{
				arrElms[i].innerHTML =  str;
			}
		},
		updateFrontPage: function(resp)
		{
			var arrElms = $$("." + this.FrontPageWeatherLoc);
			var t = new Template(this.headerTemplate);		
			var weatherObj = {};
			weatherObj.iconLocation = this.iconLocation;
			weatherObj.icon = resp.Current.SimpleIcon;
			weatherObj.currentText = resp.Current.WeatherText;
			weatherObj.currentTemp =  resp.Current.Temperature;
			weatherObj.currentWind = resp.Current.WindText;
			weatherObj.todayHigh = resp.ForeCast.Today.High;
			weatherObj.todayLow = resp.ForeCast.Today.Low;
			weatherObj.todayText = resp.ForeCast.Today.WeatherText;
			weatherObj.tomorrowHigh = resp.ForeCast.Tomorrow.High;
			weatherObj.tomorrowLow = resp.ForeCast.Tomorrow.Low;
			weatherObj.tomorrowText = resp.ForeCast.Tomorrow.WeatherText;
			
			var str = t.evaluate(weatherObj);
			if ( resp.Alert.active )
			{
				t = new Template(this.weatherAlertTemplate);
				resp.Alert.WeatherAlertBase = this.WeatherAlertBase;
				str += t.evaluate(resp.Alert);
			}
			for ( var i =0; i < arrElms.length; i++)
			{
				arrElms[i].innerHTML =  str;
			}
			CorneredBoxes.initialize();
		}
}

Event.observe(window, "load", Weather.initialize.bindAsEventListener(Weather));

