/*
============ appcuarium ============
Alfie ® Platform JS SDK
====== Apps outside the box.® ======
------------------------------------
Copyright © 2012 Appcuarium
------------------------------------
apps@appcuarium.com
@author Sorin Gheata
@version 1.2.0
====================================
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.options=e.extend({},e.fn.alfie.options,t);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;if(typeof t==="string"){var r=t.split(/[ ,]+/);return n[r].call(n)}else if(typeof t==="object"){e.each(t,function(t,r){if(r.next){var i=r.next;e.when(n[t].call(n,r)).pipe(function(e){return n[i].call(n,e)}).then(function(e){console.log(t+" Resolved -> Chained done for: "+e)},function(e){console.log(t+" Rejected: Reason -> "+e)})}else{return n[t].call(n,r)}})}},searchDelayed:function(){var e=this;e.searchInput.on("keyup",e.search)},search:function(){var t=i,n=this,r=new Date,s='select * from geo.places where text="'+n.value+'"',o="http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(s)+"&rnd="+r.getFullYear()+r.getMonth()+r.getDay()+r.getHours()+"&format=json&callback=?",u=e.Deferred();clearTimeout(t.timer);t.timer=n.value.length>=3&&setTimeout(function(){e.when(t.fetch(o,"json",null)).then(function(n){e.when(t.build(n)).done(function(t){e("#widgets-right #cities").html(t)});u.resolve()})},400);return u.promise()},get_weather:function(t){var n=i;e.when(n.fetch(alfie.path+"/alfie-wp-weather/getfeed.php","json",t.params)).then(function(e){n.build_weather_widget(e)})},getTimeAsDate:function(e){var t=new Date;return new Date(t.toDateString()+" "+e)},build_weather_widget:function(t){var n=this,r=e.Deferred(),i=e.trim(e("#widget-template").html()),s=e("
",{"class":"loaded"}),o,u=e.map(t,function(t,s){var u=t.item.pubDate,a=u.indexOf(":"),f=n.getTimeAsDate(u.substr(a-2,8)),l=n.getTimeAsDate(t.astronomy.sunrise),c=n.getTimeAsDate(t.astronomy.sunset),h="http://l.yimg.com/a/i/us/nws/weather/gr/{{condition_code}}";if(f>l&&f=348.75&&p<=360){p="N"}if(p>=0&&p<11.25){p="N"}if(p>=11.25&&p<33.75){p="NNE"}if(p>=33.75&&p<56.25){p="NE"}if(p>=56.25&&p<78.75){p="ENE"}if(p>=78.75&&p<101.25){p="E"}if(p>=101.25&&p<123.75){p="ESE"}if(p>=123.75&&p<146.25){p="SE"}if(p>=146.25&&p<168.75){p="SSE"}if(p>=168.75&&p<191.25){p="S"}if(p>=191.25&&p<213.75){p="SSW"}if(p>=213.75&&p<236.25){p="SW"}if(p>=236.25&&p<258.75){p="WSW"}if(p>=258.75&&p<281.25){p="W"}if(p>=281.25&&p<303.75){p="WNW"}if(p>=303.75&&p<326.25){p="NW"}if(p>=326.25&&p<348.75){p="NNW"}if(t.item.condition.code==20){h=alfie.path+"/alfie-wp-weather/img/"+t.item.condition.code}else{h="http://l.yimg.com/a/i/us/nws/weather/gr/"+t.item.condition.code}var d=i.replace(/{{city}}/ig,t.location.city).replace(/{{country}}/ig,t.location.country).replace(/{{image_bg}}/ig,h).replace(/{{currentTemp}}/ig,t.item.condition.temp).replace(/{{condition_code}}/ig,t.item.condition.code).replace(/{{daynight}}/ig,o.substring(0,1)).replace(/{{condition}}/ig,t.item.condition.text).replace(/{{high}}/ig,t.item.forecast.today.high).replace(/{{low}}/ig,t.item.forecast.today.low).replace(/{{wind}}/ig,t.wind.speed).replace(/{{wind_direction}}/ig,p).replace(/{{speed_unit}}/ig,t.units.speed).replace(/{{distance_unit}}/ig,t.units.distance).replace(/{{pressure_unit}}/ig,t.units.pressure).replace(/{{temperature_unit}}/ig,t.units.speed).replace(/{{humidity}}/ig,t.atmosphere.humidity).replace(/{{visibility}}/ig,t.atmosphere.visibility).replace(/{{sunrise}}/ig,t.astronomy.sunrise).replace(/{{sunset}}/ig,t.astronomy.sunset).replace(/{{day_one}}/ig,t.item.forecast.today.day).replace(/{{day_two}}/ig,t.item.forecast.tomorrow.day).replace(/{{forecast_one_high}}/ig,t.item.forecast.today.high).replace(/{{forecast_one_low}}/ig,t.item.forecast.today.low).replace(/{{forecast_two_high}}/ig,t.item.forecast.tomorrow.high).replace(/{{forecast_two_low}}/ig,t.item.forecast.tomorrow.low).replace(/{{forecast_one_code}}/ig,t.item.forecast.today.code).replace(/{{forecast_two_code}}/ig,t.item.forecast.tomorrow.code).replace(/{{yahoo_logo}}/ig,t.image.url);var v=e("#woeid-"+t.woeid).html(d)[0];r.resolve(v)});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,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)