/**
 * jQuery wrapper function
 */
( function( $ ) {
    /**
     * Creates a namespace specified by the arguments
     * of the function
     */
    $.createNamespace = function() {
        var a=arguments, o=null, i, j, d;
        for (i=0; i<a.length; i=i+1) {
            d=a[i].split(".");
            o=window;
            for (j=0; j<d.length; j=j+1) {
                o[d[j]]=o[d[j]] || {};
                o=o[d[j]];
            }
        }
        return o;
    };
})( jQuery );

var FARFARIP = jQuery.createNamespace( 'com.farfarip');

( function( $ ) {
	
	FARFARIP.DataManager = function(){
		
		// private fields
		var tmp = {};
		tmp.query = null;
		tmp.first = true;
		
		/**
		 * Process the data returns from the data request
		 * @param {Object} data
		 */
		tmp.processData = function( data ){
			tmp.query = data.refresh_url;
			
			var html = '';
			$.each( data.results, function( i, item ) {
				if ( item.text && item.from_user ){
					var text = tmp.aux_replace_users(tmp.aux_create_urls(item.text));
					html += '<p><a href="http://twitter.com/'+ item.from_user +'" rel="nofollow" target="_blank">@' + item.from_user;
					html += '</a>: ' + text + '</p>';
				}
			});	
			$('#tombstone-text').prepend( html );
			tmp.showUpdate( false );
		}
		
		tmp.aux_create_urls = function(input){
			return input.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,'<a href="$&" class="my_link" target="_blank">$&</a>').replace(/([^\/])(www[\S]+(\b|$))/gim,'$1<a href="http://$2" class="my_link" target="_blank">$2</a>')
		}

		tmp.aux_replace_users = function(input){
			return input.replace(/(^|\s)@(\w+)/gim,'$1<a href="http://twitter.com/$2" target="_blank">@$2</a>')
		}
		
		tmp.aux_replace_hash = function(input){
			return input.replace(/(^|\s)#(\w+)/gim,'$1<a href="http://search.twitter.com/search?q=$2" target="_blank">#$2</a>')
		}		
		
		/**
		 * Returns the current query
		 */
		tmp.getQuery = function(){
			return tmp.query;
		}		
		
		/**
		 * Displays the loading graphics
		 * @param {Object} value
		 */
		tmp.showUpdate = function( value ){
			if ( value ){
				$('#loading').slideDown('slow');
			}
			else {
				$('#loading').slideUp('slow');
			}
		}
		
		// public fields
		var pub = {};
		
		/**
		 * Sets the query value
		 * @param String query	the new query
		 */
		pub.setQuery = function( query ){
			tmp.query = query;
		}
		
		/**
		 * Requests data
		 */
		pub.requestData = function(){
			
			tmp.showUpdate( true );

		    var url = 'http://search.twitter.com/search.json';
			url += tmp.getQuery();
			url += '&page=1';
			
			if ( tmp.first ){
				url += '&rpp=15';
				tmp.first = false;
			}
			
			url += '&callback=?';
			$.getJSON( url, function(data) {
				tmp.processData( data );
			});		
		}
		
		return pub;

	}();
		
	FARFARIP.Application = function(){
		
		// private fields
		var tmp = {};
		
		// public fields
		var pub = {};
		
		pub.initialize = function(){
						
			var query = '?since_id=&q=%23farfar+OR+%23RIPfarfar+OR+%23farfarip';
			
			FARFARIP.DataManager.setQuery( query );
			FARFARIP.DataManager.requestData();

			$('#tombstone-text').everyTime('60s',function(i){
				FARFARIP.DataManager.requestData();
			});			
			
		}
		
		return pub;
	}();
	
})( jQuery );


if (document.readyState === 'complete ') {
	FARFARIP.Application.initialize();
}
else {
	jQuery(document).ready(function(){
		FARFARIP.Application.initialize();
	});
}
