/* ============ appcuarium ============ Alfie ® Platform JS SDK ====== Apps outside the box.® ====== ------------------------------------ Copyright © 2012 Appcuarium ------------------------------------ apps@appcuarium.com @author Sorin Gheata @version 1.0.6 ==================================== 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( /[ ,]+/ ); return me[array].call( me ); } else if ( typeof action === 'object' ) { $.each( action, function ( key, value ) { if ( value.next ) { var 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 () { var me = this; me.searchInput.on( 'keyup', me.search ); }, search: function () { var self = Alfie, input = this, now = new Date(), query = 'select * from geo.places where text="' + input.value + '"', api = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent( query ) + '&rnd=' + now.getFullYear() + now.getMonth() + now.getDay() + now.getHours() + '&format=json&callback=?', dfd = $.Deferred(); clearTimeout( self.timer ); self.timer = ( input.value.length >= 3 ) && setTimeout( function () { $.when( self.fetch( api, 'json', null ) ).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( alfie.path + '/alfie-wp-weather/getfeed.php', 'json', params.params ) ).then( function ( response ) { me.build_weather_widget( response ); } ); }, getTimeAsDate: function ( t ) { var d = new Date(); return new Date( d.toDateString() + ' ' + t ); }, build_weather_widget: function ( results ) { var me = this, dfd = $.Deferred(), widgetTemplate = $.trim( $( '#widget-template' ).html() ), widgetEnvelope = $( '