/*
============ appcuarium ============
Alfie ® Platform JS SDK
====== Apps outside the box.® ======
------------------------------------
Copyright © 2012 Appcuarium
------------------------------------
apps@appcuarium.com
@author Sorin Gheata
@version 1.1.0
====================================
Alfie Weather plugin
*/
if(typeof Object.create!=="function"){Object.create=function(e){function t(){}t.prototype=e;return new t}}(function(e,t,r,i){var s={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;action=e.options.action;return e.executeQuery(action)},executeQuery:function(t){var n=this;if(typeof t==="string"){var r=t.split(/[ ,]+/),i=r.length;return e.when(n[r].call(n)).done(function(e){})}else if(typeof t==="object"){e.each(t,function(t,r){if(r.next){next=r.next;e.when(n[t].call(n,r)).pipe(function(e){return n[next].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(e){var t=this;t.searchInput.on("keyup",t.search)},search:function(){var t=s,n=this,r=e.Deferred();clearTimeout(t.timer);t.timer=n.value.length>=3&&setTimeout(function(){t.query=n.value;now=new Date;var i='select * from geo.places where text="'+t.query+'"',s="http://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent(i)+"&rnd="+now.getFullYear()+now.getMonth()+now.getDay()+now.getHours()+"&format=json&callback=?";e.when(t.fetch(s,"json")).then(function(n){e.when(t.build(n)).done(function(t){e("#widgets-right #cities").html(t)});r.resolve()})},400);return r.promise()},get_weather:function(t){var n=s;e.when(n.fetch(alfie.path+"/alfie-wp-weather/getfeed.php","json",t.params)).then(function(t){e.when(n.build_weather_widget(t)).done(function(e){})})},getTimeAsDate:function(e){var t=new Date,n=new Date(t.toDateString()+" "+e);return n},build_weather_widget:function(t){var r=this,i=e.Deferred(),s=e.trim(e("#widget-template").html()),o=e("
",{"class":"loaded"});var u=e.map(t,function(t,o){wpd=t.item.pubDate;n=wpd.indexOf(":");tpb=r.getTimeAsDate(wpd.substr(n-2,8));tsr=r.getTimeAsDate(t.astronomy.sunrise);tss=r.getTimeAsDate(t.astronomy.sunset);if(tpb>tsr&&tpb=348.75&&u<=360){u="N"}if(u>=0&&u<11.25){u="N"}if(u>=11.25&&u<33.75){u="NNE"}if(u>=33.75&&u<56.25){u="NE"}if(u>=56.25&&u<78.75){u="ENE"}if(u>=78.75&&u<101.25){u="E"}if(u>=101.25&&u<123.75){u="ESE"}if(u>=123.75&&u<146.25){u="SE"}if(u>=146.25&&u<168.75){u="SSE"}if(u>=168.75&&u<191.25){u="S"}if(u>=191.25&&u<213.75){u="SSW"}if(u>=213.75&&u<236.25){u="SW"}if(u>=236.25&&u<258.75){u="WSW"}if(u>=258.75&&u<281.25){u="W"}if(u>=281.25&&u<303.75){u="WNW"}if(u>=303.75&&u<326.25){u="NW"}if(u>=326.25&&u<348.75){u="NNW"}var a="http://l.yimg.com/a/i/us/nws/weather/gr/{{condition_code}}";if(t.item.condition.code==20){a="http://content.appcuarium.com/img/alfie-wp-weather/"+t.item.condition.code}else{a="http://l.yimg.com/a/i/us/nws/weather/gr/"+t.item.condition.code}var f=s.replace(/{{city}}/ig,t.location.city).replace(/{{country}}/ig,t.location.country).replace(/{{image_bg}}/ig,a).replace(/{{currentTemp}}/ig,t.item.condition.temp).replace(/{{condition_code}}/ig,t.item.condition.code).replace(/{{daynight}}/ig,daynight.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,u).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 l=e("#woeid-"+t.woeid).html(f)[0];i.resolve(l)});return i.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(s);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)