/* ============ appcuarium ============ Alfie ® Platform JS SDK ====== Apps outside the box.® ====== ------------------------------------ Copyright © 2012 Appcuarium ------------------------------------ apps@appcuarium.com @author Sorin Gheata @version 1.0 ==================================== Alfie Weather plugin */ // Create wrapper for non-supporting browsers if ( typeof Object.create !== 'function' ) { Object.create = function ( obj ) { function F() {}; F.prototype = obj; return new F(); }; } ( function ( $, window, document, undefined ) { var Alfie = { init: function ( options, elem ) { var me = this; me.elem = elem; me.$elem = $( elem ); me.options = $.extend( {}, $.fn.alfie.options, options ); me.query = ''; me.searchInput = $( '#widgets-right #search-location' ); me.template = $.trim( $('#weather-template' ).html() ); me.route(); }, // Receive scope and action and route input based on values route: function () { var me = this; action = me.options.action; return me.executeQuery( action ); }, // Execute called function executeQuery: function ( action ) { var me = this; if ( typeof action === 'string' ) { var array = action.split(/[ ,]+/), count = array.length; return $.when( me[array].call( me ) ).done( function( response ) { }); } else if ( typeof action === 'object' ) { $.each( action, function( key, value ) { if( value.next ) { next = value.next; $.when( me[ key ].call( me, value ) ).pipe( function( data ) { return me[ next ].call( me, data ); }).then( function( data ) { console.log( key + ' Resolved -> Chained done for: ' + data ); }, function( message ) { console.log( key + ' Rejected: Reason -> ' + message ) ; }); } else { return me[ key ].call( me, value ); } }); } }, searchDelayed: function( term ) { var me = this; me.searchInput.on( 'keyup', me.search ); }, search: function() { var self = Alfie, input = this, dfd = $.Deferred(); clearTimeout( self.timer ); self.timer = ( input.value.length >= 3 ) && setTimeout(function() { self.query = input.value; now = new Date(); var query = 'select * from geo.places where text="'+ self.query +'"', api = 'http://query.yahooapis.com/v1/public/yql?q='+ encodeURIComponent(query) +'&rnd='+ now.getFullYear() + now.getMonth() + now.getDay() + now.getHours() +'&format=json&callback=?'; $.when( self.fetch( api, 'json' ) ).then( function( response ) { $.when( self.build ( response ) ).done(function(resp){ $('#widgets-right #cities').html( resp ); }); dfd.resolve(); }); }, 400); return dfd.promise(); }, get_weather: function( params ) { var me = Alfie; $.when( me.fetch('/wp-content/plugins/alfie-wp-weather/getfeed.php', 'json', params.params )).then(function( response ) { $.when(me.build_weather_widget(response)).done(function(res){ $('#dummy').append( res ); }); }); }, getTimeAsDate: function(t) { d = new Date(); r = new Date(d.toDateString() +' '+ t); return r; }, build_weather_widget: function( results ) { var me = this, dfd = $.Deferred(), widgetTemplate = $.trim( $('#widget-template' ).html() ), widgetEnvelope = $( '