/*--------------------------------------------------
WEATHER
Version:   0.0.2
Generated: 1/4/2010
--------------------------------------------------*/
UKISA.namespace("UKISA.widget.Weather");UKISA.widget.Weather=function(f,b,a){var c,d,e;e=this;if(!b){this.log("There is no canvas element specified.")}d=YAHOO.util.CustomEvent;this.onReady=new YAHOO.util.CustomEvent("onReady",this);this.onComplete=new YAHOO.util.CustomEvent("onComplete",this);this.onXdrReady=new YAHOO.util.CustomEvent("onXdrReady",this);this.onBusy=new YAHOO.util.CustomEvent("onBusy",this);this.onFailure=new YAHOO.util.CustomEvent("onFailure",this);this.onGeoLocationSuccess=new YAHOO.util.CustomEvent("onGeoLocationSuccess",this);this.onGeoLocationFailure=new YAHOO.util.CustomEvent("onGeoLocationFailure",this);this.onWeatherSuccess=new YAHOO.util.CustomEvent("onWeatherSuccess",this);this.onWeatherFailure=new YAHOO.util.CustomEvent("onWeatherFailure",this);this.onWeatherLookupFailure=new YAHOO.util.CustomEvent("onWeatherLookupFailure",this);this.config.weatherDescriptions={foggy:"Foggy",na:"Not available",cloudy:"Cloudy",partlyCloudy:"Partly cloudy",sleet:"Sleet",heavySnow:"Heavy snow",lightSnow:"Light snow",lightRain:"Light rain",heavyRain:"Heavy rain",windy:"Windy",clear:"Clear",sunny:"Sunny",severe:"Severe weather"};for(var c in a){if(typeof this.config[c]!=="undefined"){this.config[c]=a[c]}}this.weatherCodes={1:this.config.weatherDescriptions.severe,2:this.config.weatherDescriptions.severe,3:this.config.weatherDescriptions.severe,4:this.config.weatherDescriptions.severe,5:this.config.weatherDescriptions.sleet,6:this.config.weatherDescriptions.sleet,7:this.config.weatherDescriptions.sleet,8:this.config.weatherDescriptions.lightRain,9:this.config.weatherDescriptions.lightRain,10:this.config.weatherDescriptions.lightRain,11:this.config.weatherDescriptions.heavyRain,12:this.config.weatherDescriptions.heavyRain,13:this.config.weatherDescriptions.lightSnow,14:this.config.weatherDescriptions.lightSnow,15:this.config.weatherDescriptions.heavySnow,16:this.config.weatherDescriptions.heavySnow,17:this.config.weatherDescriptions.sleet,18:this.config.weatherDescriptions.sleet,19:this.config.weatherDescriptions.foggy,20:this.config.weatherDescriptions.foggy,21:this.config.weatherDescriptions.foggy,22:this.config.weatherDescriptions.foggy,23:this.config.weatherDescriptions.windy,24:this.config.weatherDescriptions.windy,25:this.config.weatherDescriptions.clear,26:this.config.weatherDescriptions.cloudy,27:this.config.weatherDescriptions.cloudy,28:this.config.weatherDescriptions.cloudy,29:this.config.weatherDescriptions.partlyCloudy,30:this.config.weatherDescriptions.partlyCloudy,31:this.config.weatherDescriptions.clear,32:this.config.weatherDescriptions.sunny,33:this.config.weatherDescriptions.sunny,34:this.config.weatherDescriptions.sunny,35:this.config.weatherDescriptions.sleet,36:this.config.weatherDescriptions.sunny,37:this.config.weatherDescriptions.heavyRain,38:this.config.weatherDescriptions.heavyRain,39:this.config.weatherDescriptions.heavyRain,40:this.config.weatherDescriptions.heavyRain,41:this.config.weatherDescriptions.heavySnow,42:this.config.weatherDescriptions.lightSnow,43:this.config.weatherDescriptions.heavySnow,44:this.config.weatherDescriptions.partlyCloudy,45:this.config.weatherDescriptions.heavyRain,46:this.config.weatherDescriptions.lightSnow,47:this.config.weatherDescriptions.heavyRain,3200:this.config.weatherDescriptions.na};this.config.id=f;this.config.canvas=b;YAHOO.util.Event.onDOMReady(function(){e.init()})};UKISA.widget.Weather.prototype={config:{id:null,canvas:null,source:null,debug:false,forecastOnLoad:true,render:null,country:"ireland",appId:"o_36KYXV34GRqft4Sow6z6Ys7sEJgeh0AqEQ8a7SrRE1gHzlGxxS42m_c3Cz02ZEMEU-",geoUrl:"http://where.yahooapis.com/v1/",weatherUrl:"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.bylocation%20where%20location%20%3D%20%22{0}%22%20and%20unit%20%3D%20'{1}'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys",unit:"c"},searchTerm:null,searchTermName:null,woeid:null,weather:null,forecast:{},ready:false,readyTimer:null,xdr:false,onReady:null,onBusy:null,onFailure:null,onComplete:null,onXdrReady:null,onGeoLocationSuccess:null,onGeoLocationFailure:null,onWeatherLookupFailure:null,init:function(){var c,b,a;c=this;b=YAHOO.util.Connect;YAHOO.util.Event.addListener(this.config.id,"submit",this.search,null,this);this.onReady.fire(this);a=YAHOO.util.Cookie.get("weatherwidget");if(typeof YAHOO.util.Connect.transport!=="undefined"){this.xdr=true;YAHOO.util.Connect.transport("/web/yui/build/connection/connection.swf");b.xdrReadyEvent.subscribe(function(){c.log("f: init YQL");c.onXdrReady.fire(c)});b.failureEvent.subscribe(function(){c.log("YCM failure")});b.abortEvent.subscribe(function(){c.log("YCM abort")});this.onXdrReady.subscribe(function(){c.ready=true});if(a){this.log("f: init cookie");this.cookieToJson(a);if(this.config.forecastOnLoad){this.render();this.onComplete.fire(this)}}}},search:function(d,a){var f,b,c;f=this;if(typeof d!=="string"){YAHOO.util.Event.stopEvent(d);if(!this.source){b=$("input.field",this.config.id,true);c=b.value;this.log('f search: "'+c+'"')}else{if(typeof this.source==="string"){b=$(this.source,true);c=b.value}else{if(typeof this.source==="function"){c=this.source.call(this)}}this.log('f search (custom): "'+c+'"')}}else{c=d}if(!this.xdr){this.xdr=true;YAHOO.util.Connect.transport("/web/yui/build/connection/connection.swf")}if(c===""){return this.failure("validation")}this.busy();if(this.ready){this.fetchWeather(c,a)}else{this.readyTimer=YAHOO.lang.later(200,this,function(){if(f.ready){f.readyTimer.cancel();f.readyTimer=null;f.fetchWeather(c,a)}},null,true)}},busy:function(){var a;a=$(this.config.canvas,"page",true);a.innerHTML=['<p class="busy">Please wait</p>'].join("");this.onBusy.fire(this)},failure:function(c){var a,b;a=$(this.config.canvas,"page",true);if(c==="location"){b=['<p class="error">Sorry, we cannot find a weather report for you location. Please try again.</p>']}if(c==="validation"){b=['<p class="error">Please enter your location.</p>']}a.innerHTML=b.join("");this.onFailure.fire(this)},getGeoUrl:function(a){return this.config.geoUrl+"places.q('"+YAHOO.lang.trim(a)+","+this.config.country+"')?appid="+this.config.appId+"&format=json"},getWeatherUrl:function(){return this.config.weatherUrl.replace("{0}",encodeURIComponent(this.searchTerm+","+this.config.country)).replace("{1}",this.config.unit)},fetchGeoLocation:function(){var c,a,b;b=this;c={success:function(e){var d;b.log("fetchGeoLocation callback: success");d=YAHOO.lang.JSON.parse(e.responseText);b.woeid=d.places.place[0].woeid;b.onGeoLocationSuccess.fire(b);b.fetchWeather()},failure:function(d){b.log("fetchGeoLocation callback: failure");b.onGeoLocationFailure.fire(b)},xdr:true};a=YAHOO.util.Connect.asyncRequest("GET",b.getGeoUrl("dublin"),c)},fetchWeather:function(b,a){var e,c,d;d=this;this.searchTerm=b;this.searchTermName=a||null;e={success:function(i){var g,h,f;d.log("fetchWeather callback: success");d.onWeatherSuccess.fire(d);g=YAHOO.lang.JSON.parse(i.responseText);d.weather=g.query.results.weather.rss;h=g.query.results.weather.rss.channel;if(h.description!=="Yahoo! Weather Error"){d.log("fetchWeather callback success: success");f=h.location.city;if(d.searchTermName){f=d.searchTermName}d.forecast={local:h.location.city,name:f,region:h.location.region,temperature:h.units.temperature,code:h.item.forecast[0].code,high:h.item.forecast[0].high,low:h.item.forecast[0].low};YAHOO.util.Cookie.remove(" weatherwidget");YAHOO.util.Cookie.remove("weatherwidget");YAHOO.util.Cookie.remove(" weatherwidget");YAHOO.util.Cookie.remove("weatherwidget");YAHOO.util.Cookie.remove(" weatherwidget");YAHOO.util.Cookie.remove("weatherwidget");YAHOO.util.Cookie.set("weatherwidget",d.jsonToCookie());d.render()}else{d.log("fetchWeather callback success: failure");d.failure("location");d.weatherLookupFailure()}d.onComplete.fire(d)},failure:function(f){d.log("fetchWeather callback: failure");d.onWeatherFailure.fire(this)},xdr:true};c=YAHOO.util.Connect.asyncRequest("GET",d.getWeatherUrl(),e)},weatherLookupFailure:function(){this.onWeatherLookupFailure.fire(this)},render:function(){if(typeof this.config.renderSuccess==="function"){this.config.renderSuccess.call(this,this.forecast)}else{this.renderSuccess(this.forecast)}},renderSuccess:function(a){var b,d,e,c;b=$(this.config.canvas,"page",true);e=this.getWeatherDescription();if(b){d=['<div id="weather-forecast">','<p class="weather-location">Currently in <em>'+this.forecast.name+"</em> it is&hellip;</p>",'<p class="weather-visual" id="weather-visual-'+this.forecast.code+'">'+e+"</p>",'<p class="weather-description">'+e+"</p>",'<p class="weather-temperature">'+this.getWeatherTemperature()+"</p>","</div>"];b.innerHTML=d.join("")}},getWeatherDescription:function(){return this.weatherCodes[this.forecast.code]},getWeatherTemperature:function(){return this.forecast.high+this.forecast.temperature},jsonToCookie:function(){var a,b;a=[];for(b in this.forecast){a.push(b+"="+this.forecast[b])}return a.join("|")},cookieToJson:function(d){var b,e,c,a;b=d.split("|");for(c=0,a=b.length;c<a;c++){e=b[c].split("=");this.forecast[e[0]]=e[1]}return this.forecast},log:function(a){if(this.config.debug){console.log(a)}}};