/**
 * TweetQuote (tweetquote.co.uk)
 *
 * The MIT License
 * 
 * Copyright (c) 2009 Paul James Campbell (tweetquote.co.uk)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
var tweetquote = function () {
	
	// Variables
	var config = {
		'refreshRate'			:  100000, 
		'numTweets'				:  5,
		'phrase'				: 	false, 		
		'username'				: 	false, 
		'hashtag'				: 	true,
		'geocode'				: 	false, 
		'advancedquery'			: 	false,
		'includeauthor'			: 	false,
		'stripurls'				: 	false, 
		'stripTwitterTags'		: 	false,
		'classname'				:  false, 
		'lang'					: 	"en", // http://en.wikipedia.org/wiki/ISO_639-1
		'animationtype'			:  'fade', 
		'animationspeed'		:  'slow', 
		'defaulttext'			:  ""
	};
	
	var rootId = 'tweet_quote', wrapId = 'tweet_quote_wrapper', textId = "tweet_quote_text", authorId = 'tweet_quote_author', rootDom, quoteDom, intId, sinceId, handlerTextParser, handlerQuoteUpdate; 
	var loadedVertJquery = false;
	
	// Construct
	(function () {
		if( !window['jQuery'] ) {
			// Load jQuery from Google API
			document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>');
		}		
	})();
	
	
	// Initiate
	function init () {
		try {
			if( config.phrase || config.username || config.hashtag || config.advancedquery ) {
				
				document.write('<ul class="verticalScroll" id="' + rootId + '"></ul>');
				rootDom = $("#" + rootId);
				
				if( config.classname.constructor === String ) {
					rootDom.addClass( config.classname );
				}
				
				if( config.defaulttext.constructor === String ) {
					rootDom.html( '<div id="' + wrapId + '"><span id="' + textId + '">' + config.defaulttext + '</span></div>' );
				}
				
			}
		} catch ( e ) {
			logError( e );
			return;
		}		
		
		getTweet();
		initRefresh();
	}
	
	if(!String.linkify) {
		String.prototype.linkify = function() {
			// http://, https://, ftp://
			var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;          
			// www. sans http:// or https://         
			var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;          
			// Email addresses         
			var emailAddressPattern = /\w+@[a-zA-Z_]+?(?:\.[a-zA-Z]{2,6})+/gim;          
			return this             
			.replace(urlPattern, '<a href="$&">$&</a>')             
			.replace(pseudoUrlPattern, '$1<a href="http://$2">$2</a>')             
			.replace(emailAddressPattern, '<a href="mailto:$&">$&</a>');     
			}; 
			} 
	
	function initRefresh () {
		//intId = setInterval( getTweet, config.refreshRate );
	}
	
	function stopRefresh () {
		clearInterval( intId );
	}
	
	function getTweet () {

		rootDom.html( '<div id="' + wrapId + '"><span id="' + textId + '">' + config.defaulttext + '</span></div>' );

		
		// http://apiwiki.twitter.com/Search+API+Documentation
		var api = 'http://search.twitter.com/search.json?rpp=' + config.numTweets + '&callback=tweetquote.onApiCallback&lang=' + config.lang + "&"; // 140 character limit
		
		if( sinceId ) {
			api += 'since_id=' + sinceId + '&';
		}
		
		// Construct API call
		if( config.phrase ) {
			api += 'q=' + config.phrase;
		} else if ( config.username ) {
			api += 'q=from%3A' + config.username;
		} else if ( config.hashtag ) {
			api += 'q=%23' + config.hashtag;
		} else if ( config.advancedquery ) {
			api += 'q=' + config.advancedquery;
		}
		
		if( config.geocode ) {
			api += "&geocode=" + config.geocode;
		}

		/*alert(api);*/
		try {
			rootDom.before('<script type="text/javascript" src="' + api + '"></script>');
		} catch ( e ) {
			logError ( e );
		}
	}
	
	function filterTweet ( json ) { 
		if( json.results && json.results.length > 0 ) {

		html = '';
		
		for (var i=0;i<=json.results.length-1;i++){
		
			var tweet = json.results[i];

			sinceId = tweet.id;
			html += '<li id="' + wrapId + '">' + getTweetText( tweet ) + '</li>';
			
		}
		rootDom.html( html )
		
		// do scrolling
		if(!loadedVertJquery){
			$('ul.verticalScroll').verticalScroll();
			loadedVertJquery = true;
		}
		
		}
	}
	
	function getTweetText ( tweet ) {
		var text = tweet.text;
		
		if ( config.stripurls ) {
			var pattern_url = /https?:\/\/([\-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/ig;
			text = text.replace( pattern_url , '');
		}
		
		if( config.stripTwitterTags ) { // Still working on
			var pattern_tags = /RT|\'|\"|/ig;
			text = text.replace( pattern_tags , '');
		}
		
		var date = new Date(tweet.created_at.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/, "$1 $2 $4 $3 UTC"));
		
		var html = ' <span id="' + authorId + '">' + '<a href="http://www.twitter.com/' + tweet.from_user + '">' + '@SharePoint911 ' + '</a>' + dateFormat(date, "mmm. d, yyyy - h:MM tt") + '</span><br/>';
		html += '<span id="' + textId + '">' + text.linkify() + '</span>';
		
		
		/*if( config.includeauthor ) {
			html += ' <span id="' + authorId + '">from ' + '<a href="http://www.twitter.com/' + tweet.from_user + '">' + tweet.from_user + '</a></span>';
		}*/		
		if( handlerTextParser ) {
			return( handlerTextParser( html, tweet ) );
		}
		return( html.replace(/^\s+|\s+$/g, '') );
	}
	
	function logError ( e ) {
		// For clued up Firefox/Safari developers
		if(console && console.log) {
			console.log("TQ: " + e);
		}
	}
	
	return {
		
		load : function ( options ) {
			
			if( options.constructor === String) {
				// Assume basic string, add as phrase
				config.phrase = options;
			} else {
				$.extend(config, options);
			}
			
			init();
		},
		
		stop : function () {
			stopRefresh();
		},
		
		// Public method for Twitter search API (don't use)
		onApiCallback : function ( json ) {
			filterTweet( json );
		},
		
		// Custom callback handlers
		onTextParse : function ( func ) {
			handlerTextParser = func;
		},
		
		onQuoteUpdate : function ( func ) {
			handlerQuoteUpdate = func;
		}
		
	};
	
}();
window.tweetquote = tweetquote; // Ensure global scope
