/*
============ appcuarium ============
Alfie ® Platform JS SDK
====== Apps outside the box.® ======
------------------------------------
Copyright © 2012 Appcuarium
------------------------------------
apps@appcuarium.com
@author Sorin Gheata
@version 1.2.11
====================================
Alfie Weather plugin
*/
if(typeof Object.create!=="function"){Object.create=function(e){function t(){}t.prototype=e;return new t}}(function(e,t,n,r){var i={init:function(t,n){var r=this;r.elem=n;r.$elem=e(n);r.date=new Date;r.options=e.extend({},e.fn.alfie.options,t);r.random=r.date.getFullYear()+r.date.getMonth()+r.date.getDay()+r.date.getHours();r.refresh_in=r.random+3660;r.now=e.now().toString();r.timestamp=r.now.substring(0,10);r.next_refresh=sessionStorage.getItem("next_refresh");r.query="";r.searchInput=e("#widgets-right #search-location");r.template=e.trim(e("#weather-template").html());r.route()},route:function(){var e=this,t=e.options.action;return e.executeQuery(t)},executeQuery:function(t){var n=this;e.each(t,function(e,t){return n[e].call(n,t)})},searchDelayed:function(){var e=this;e.searchInput.on("keyup",e.search)},get_coordinates_by_ip:function(){var t=this,n=alfie.path+"/alfie-wp-weather/get_coordinates.php",r=e.Deferred();e.when(t.fetch(n,"json",null)).then(function(e){r.resolve(e)});return r.promise()},get_woeid_by_coordinates:function(t,n){var r=this,i=new Date,s=i.getFullYear()+i.getMonth()+i.getDay()+i.getHours(),o='select * from geo.placefinder where text="'+t+", "+n+'" and gflags="R"',u="http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(o)+"&rnd="+s+"&format=json&callback=?",a=e.Deferred();e.when(r.fetch(u,"json",null)).then(function(e){a.resolve(e)});return a.promise()},ip_to_woeid:function(){var t=this,n=e.Deferred(),r=JSON.parse(sessionStorage.getItem("client_position"));if(r){if(r.data.lat!==0&&r.data.lng!==0){e.when(t.get_woeid_by_coordinates(r.data.lat,r.data.lng)).then(function(e){n.resolve(e.query.results.Result.woeid)})}else{n.resolve(0)}}else{e.when(t.get_coordinates_by_ip()).then(function(r){if(r.data.lat==0&&r.data.lng==0){n.resolve(0)}else{e.when(t.get_woeid_by_coordinates(r.data.lat,r.data.lng)).then(function(e){n.resolve(e.query.results.Result.woeid)})}sessionStorage.setItem("client_position",JSON.stringify(r))})}return n.promise()},search:function(){var t=i,n=this,r=new Date,s=r.getFullYear()+r.getMonth()+r.getDay()+r.getHours(),o='select * from geo.places where text="'+n.value+'"',u="http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(o)+"&rnd="+s+"&format=json&callback=?",a=e.Deferred();clearTimeout(t.timer);t.timer=n.value.length>=3&&setTimeout(function(){e.when(t.fetch(u,"json",null)).then(function(n){e.when(t.build(n)).done(function(t){e("#widgets-right #cities").html(t)});a.resolve()})},400);return a.promise()},get_weather:function(n){var r=this,i=n.params.auto_location,s=n.params.locale,o=n.params.timestamp,u=new Date(o*1e3),a=u.getUTCHours(),f=u.getUTCDate(),l=u.getUTCMonth(),c=u.getUTCFullYear(),h=new Date(Date.UTC(c,l,f,a)),p=h.getTime()/1e3,d=p+3600,v=sessionStorage.getItem("next_refresh");if(i==true){if(t.sessionStorage){if(sessionStorage.getItem("location_woeid")&&sessionStorage.getItem("location_woeid")!=="0"){n.params.woeid=parseInt(sessionStorage.getItem("location_woeid"));var m="weatherData_"+n.params.woeid+"_"+s,g=JSON.parse(sessionStorage.getItem(m));if(o",{"class":"loaded"}),o,u=e.map(t,function(e,t){var s=e.item.pubDate,u=s.indexOf(":"),a=n.getTimeAsDate(s.substr(u-2,8)),f=n.getTimeAsDate(e.astronomy.sunrise),l=n.getTimeAsDate(e.astronomy.sunset),c="http://l.yimg.com/a/i/us/nws/weather/gr/{{condition_code}}";if(a>f&&a=348.75&&h<=360){h="N"}if(h>=0&&h<11.25){h="N"}if(h>=11.25&&h<33.75){h="NNE"}if(h>=33.75&&h<56.25){h="NE"}if(h>=56.25&&h<78.75){h="ENE"}if(h>=78.75&&h<101.25){h="E"}if(h>=101.25&&h<123.75){h="ESE"}if(h>=123.75&&h<146.25){h="SE"}if(h>=146.25&&h<168.75){h="SSE"}if(h>=168.75&&h<191.25){h="S"}if(h>=191.25&&h<213.75){h="SSW"}if(h>=213.75&&h<236.25){h="SW"}if(h>=236.25&&h<258.75){h="WSW"}if(h>=258.75&&h<281.25){h="W"}if(h>=281.25&&h<303.75){h="WNW"}if(h>=303.75&&h<326.25){h="NW"}if(h>=326.25&&h<348.75){h="NNW"}if(e.item.condition.code==20){c=alfie.path+"/alfie-wp-weather/img/"+e.item.condition.code}else{c="http://l.yimg.com/a/i/us/nws/weather/gr/"+e.item.condition.code}var p=i.replace(/{{city}}/ig,e.location.city).replace(/{{country}}/ig,e.location.country).replace(/{{image_bg}}/ig,c).replace(/{{currentTemp}}/ig,e.item.condition.temp).replace(/{{condition_code}}/ig,e.item.condition.code).replace(/{{daynight}}/ig,o.substring(0,1)).replace(/{{condition}}/ig,e.item.condition.text).replace(/{{high}}/ig,e.item.forecast.today.high).replace(/{{low}}/ig,e.item.forecast.today.low).replace(/{{wind}}/ig,e.wind.speed).replace(/{{wind_direction}}/ig,h).replace(/{{speed_unit}}/ig,e.units.speed).replace(/{{distance_unit}}/ig,e.units.distance).replace(/{{pressure_unit}}/ig,e.units.pressure).replace(/{{temperature_unit}}/ig,e.units.speed).replace(/{{humidity}}/ig,e.atmosphere.humidity).replace(/{{visibility}}/ig,e.atmosphere.visibility).replace(/{{sunrise}}/ig,e.astronomy.sunrise).replace(/{{sunset}}/ig,e.astronomy.sunset).replace(/{{day_one}}/ig,e.item.forecast.today.day).replace(/{{day_two}}/ig,e.item.forecast.tomorrow.day).replace(/{{forecast_one_high}}/ig,e.item.forecast.today.high).replace(/{{forecast_one_low}}/ig,e.item.forecast.today.low).replace(/{{forecast_two_high}}/ig,e.item.forecast.tomorrow.high).replace(/{{forecast_two_low}}/ig,e.item.forecast.tomorrow.low).replace(/{{forecast_one_code}}/ig,e.item.forecast.today.code).replace(/{{forecast_two_code}}/ig,e.item.forecast.tomorrow.code).replace(/{{yahoo_logo}}/ig,e.image.url);r.resolve(p)});return r.promise()},fetch:function(t,n,r){var i=this,s=t.encoding||n,o=t.url||t,u=t.params||r;return e.ajax({url:o,type:"POST",async:true,cache:false,data:u,dataType:s})},build:function(t){var n=this,r=e.Deferred(),i=e.trim(e("#weather-template").html()),s=e("",{"class":"loaded"});var o=e.map(t.query.results,function(t,n){e.each(t,function(t,n){var o=i.replace(/{{woeid}}/ig,n.woeid).replace(/{{location}}/ig,n.name).replace(/{{country}}/ig,n.country.content);var u=e(s).append(o)[0];r.resolve(u)})});return r.promise()}};e.fn.alfie=function(t){var n=Object.create(i);if(n[t]){return n[t].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof t==="object"||!t){return this.each(function(){n.init(t,this);e.data(this,"alfie",n)})}};e.fn.alfie.options={}})(jQuery,window,document)