/**
 * JS_TVTWEETS - By Bramus! - http://www.bram.us/
 * ----------------------------------------------------------------
 */

	var JS_TVTWEETS = {}




/**
 *
 * jQuery Extensions
 * ----------------------------------------------------------------
 */

	// reverse for jQuery
	jQuery.fn.reverse=Array.prototype.reverse;




/**
 * JS_TVTWEETS - Helper Object
 * ----------------------------------------------------------------
 */

	JS_TVTWEETS.helper = {


		/**
		 * replaceAll - replace all occurences of one instring into a string
		 * @param string str
		 * @param string needle
		 * @param string replacement
		 * @return string
		 * -------------------------------------------------------------
		 */

			replaceAll						: function(str, needle, replacement) {

				if (!str || (str == undefined) || (str == ''))	return '';
				return str.replace(new RegExp(needle, 'ig'), replacement);
			},


		/**
		 * assignFromObject - kinda like Spoon its "$tpl->assignFromArray($associativeArray)" ;)
		 * @param string str
		 * @param Object object
		 * @return string
		 * -------------------------------------------------------------
		 */

			assignFromObject				: function(str, object) {

				if (!str || (str == undefined) || (str == ''))	return '';

				for (index in object) {

					str = JS_TVTWEETS.helper.replaceAll(str, '{' + index + '}', object[index]);

				}

				return str;

			},


		/**
		 * html_entity_decode (cfr. PHP)
		 * @author http://kevin.vanzonneveld.net/code/php_equivalents/php.js
		 * @param string str
		 * @return string
		 * -------------------------------------------------------------
		 */

			html_entity_decode				: function (str) {

				if (!str || (str == undefined) || (str == ''))	return '';

				var ta 			= document.createElement("textarea");
				ta.innerHTML	= str.replace(/</g,"<").replace(/>/g,">");
				toReturn 		= ta.value;
				ta 				= null;
				return toReturn;
			},


		/**
		 * reworkHashTag
		 * @param string str
		 * @param string tag
		 * @return string
		 * -------------------------------------------------------------
		 */
			
			reworkHashTag						: function(str, tag)
			{
				return JS_TVTWEETS.helper.replaceAll(str, '#' + tag, '<strong>#' + tag + '</strong>');
			},


		/**
		 * end of object
		 * -------------------------------------------------------------
		 */
			_eoo				: true
	
		
	
	}




/**
 * JS_TVTWEETS - Ticker Object
 * ----------------------------------------------------------------
 */

	JS_TVTWEETS.ticker = {
	
		/**
		 * dataMembers
		 * ----------------------------------------------------------------
		 */
		
			// defines
			timeBetweenFetch		: 10000, 		// time between two fetches of new tweets, in ms
			timeBetweenDisplay		: 2000,			// time beteen showing tweets, in ms
			timeToShow				: 1000,			// time to animate a new tweet before it is shown
			maxResultsToFetch		: 25,			// the number of results to fetch at once
			blob					: '' + "\n"		// the HTML that defines a new tweet
									+ '					<li class="result" id="result_{id}">' + "\n"
									+ '						<div class="avatar">' + "\n"
									+ '							<a href="http://twitter.com/{from_user}">' + "\n"
									+ '								<img alt="{from_user}" src="{profile_image_url}">' + "\n"
									+ '							</a>' + "\n"
									+ '						</div>' + "\n"
									+ '						<div class="msg">' + "\n"
									+ '							<a href="http://twitter.com/{from_user}">{from_user}</a>: <span class="msgtxt {iso_language_code}">{text}</span>' + "\n"
									+ '						</div>' + "\n"
									+ '						<div class="info">Op {created_at} van <span class="source">{source}</span> &middot; <a href="http://twitter.com/home?in_reply_to={from_user}&amp;in_reply_to_status_id={id}&amp;status=%40{from_user}+" class="litnv">Reply</a> &middot; <a href="http://twitter.com/{from_user}/status/{id}" class="lit">Bekijk Tweet</a></div>' + "\n"
									+ '						<p class="clearleft"></p>' + "\n"
									+ '					</li>' + "\n" + "\n",
			
			// dynamic datamembers
			theList					: null,		// the jQuery object that holds the ul where the tweets get added onto
			tagToSearch				: null,		// the tag to search
			lastTweetId				: null,		// id of the last tweet fetched (initially the last one shown)
			tweetsInQueue			: [],		// the tweets in the queue
			timer					: null,		// the timer to show the tweets
			
		
		/**
		 * init - inits JS_TVTWEETS.ticker
		 * ----------------------------------------------------------------
		 */
		
			init					: function(theList, tagToSearch)
			{
				
				// store theList
				JS_TVTWEETS.ticker.theList		= theList;
				
				// set hover on list
				JS_TVTWEETS.ticker.theList.hover(function() {
					JS_TVTWEETS.ticker.hoverOverList = true;
				}, function() {
					JS_TVTWEETS.ticker.hoverOverList = false;
				});
				
				// store tagToSearch
				JS_TVTWEETS.ticker.tagToSearch 	= tagToSearch;
				
				// define last fetched id
				JS_TVTWEETS.ticker.lastTweetId	= $('.result:first').attr('id').replace('result_','');
				
				// Start Fetch
				JS_TVTWEETS.ticker.fetch();
				
				// Start Process
				JS_TVTWEETS.ticker.timer 		= setInterval(JS_TVTWEETS.ticker.process, JS_TVTWEETS.ticker.timeBetweenDisplay);
				
			},
			
		
		/**
		 * fetch - fetches new tweets JS_TVTWEETS.ticker and stores them on our internal queue
		 * ----------------------------------------------------------------
		 */
			
			fetch					: function()
			{
				
				// Yay, Web 2.0 ... Fancy Schmanzy!
				$.getJSON(
					'http://search.twitter.com/search.json?q=%23' + JS_TVTWEETS.ticker.tagToSearch + '&rpp=' + JS_TVTWEETS.ticker.maxResultsToFetch + '&since_id=' + JS_TVTWEETS.ticker.lastTweetId + '&callback=?',
					function (json)
					{
						
						// we haz got results
						if (json.results.length > 0)
						{
						
							// loop all results
							jQuery(json.results).reverse().each(function(i) {
								
								// rework source
								this.source = JS_TVTWEETS.helper.html_entity_decode(this.source);
								
								// rework text
								this.text	= JS_TVTWEETS.helper.reworkHashTag(this.text, JS_TVTWEETS.ticker.tagToSearch);
								
								// push tweet on queue
								JS_TVTWEETS.ticker.tweetsInQueue.push(this);
								
							});
							
							// store lastTweetId
							if (JS_TVTWEETS.ticker.tweetsInQueue.length > 0)
							{
								JS_TVTWEETS.ticker.lastTweetId = JS_TVTWEETS.ticker.tweetsInQueue[JS_TVTWEETS.ticker.tweetsInQueue.length-1].id;
							}
							
						}
						
						// now, fetch the results again
						setTimeout(function() {
							JS_TVTWEETS.ticker.fetch();
						}, JS_TVTWEETS.ticker.timeBetweenFetch);
							
					}
				);
				
			},
			
		
		/**
		 * process - processes our internal queue and displays the tweets
		 * ----------------------------------------------------------------
		 */
			
			process					: function()
			{
				
				// no items in queue? Just return then
				if (JS_TVTWEETS.ticker.tweetsInQueue.length == 0)	return;
				
				// user is hovering over list? Don't show!
				if (JS_TVTWEETS.ticker.hoverOverList)		return;
				
				// shift current item from tweetsInQueue
				var curItem = JS_TVTWEETS.ticker.tweetsInQueue.shift();
				
				// define blob
				var blob	= JS_TVTWEETS.helper.assignFromObject(JS_TVTWEETS.ticker.blob, curItem);
				
				// inject it and show it
				$(blob).css('display','none').prependTo(JS_TVTWEETS.ticker.theList).slideDown(JS_TVTWEETS.ticker.timeToShow);
				
			},


		/**
		 * end of object
		 * -------------------------------------------------------------
		 */
		
			_eoo					: true
		 
	}
	
// EOF