// Copyright 2009 CloudAppers FZ-LLC

var Lens = 
{
	lastQuery:    '',
	tweetsLoaded: {'#tweets': 0, '#new-tweets': 0},
	
	listenForTweets: function()
	{
		Lens.loadTweets
		(
			{newer_than_id: $(( $('#new-tweets li').length > 0 ? '#new-tweets' : '#tweets' ) + ' li:first').attr('id').substring(1)},
			'#new-tweets',
			'prepend',
			false,
			'tweetsAvailable'
		);
	},
	
	renderNewTweets: function()
	{
		$('#tweets').prepend($('#new-tweets li').clone());		
	},
	
	clearNewTweetsAlert: function()
	{
		$('#new-tweets').empty();
		Lens.tweetsLoaded['#new-tweets'] = 0;
		$('#new-tweets-alert').hide();
	},
		
	loadTweets: function(options, container, concatMethod, loader, tweetsLoadedEvent)
	{
		if (loader)
		{
			Lens.clearNewTweetsAlert();
		}
		
		concatMethod = concatMethod || 'append';
		
		if (typeof options['query'] != 'string')
		{
			$.extend(options, {query: Lens.lastQuery});
		}
		else
		{
			Lens.lastQuery = options['query'];
		}
			
		$.getJSON('/xhr.php?get=tweets&format=json', options, function(json)
		{
			var tweetsHTML = [];

			$.each(json.tweets, function (i, tweet)
			{
				tweetsHTML[tweetsHTML.length] = '<li id="t';
				tweetsHTML[tweetsHTML.length] = tweet.tweet_id;
				tweetsHTML[tweetsHTML.length] = '" class="clearfix"><div class="profile-image"><a href="http://twitter.com/';
				tweetsHTML[tweetsHTML.length] = tweet.screen_name;
				tweetsHTML[tweetsHTML.length] = '"><img src="';
				tweetsHTML[tweetsHTML.length] = tweet.profile_image_url;
				tweetsHTML[tweetsHTML.length] = '" /></a>';
				tweetsHTML[tweetsHTML.length] = '</div><div class="tweet-content"><span class="twitter-id"><a href="http://twitter.com/';
				tweetsHTML[tweetsHTML.length] = tweet.screen_name;
				tweetsHTML[tweetsHTML.length] = '">';
				tweetsHTML[tweetsHTML.length] = tweet.screen_name;
				tweetsHTML[tweetsHTML.length] = '</a>:</span> <span class="tweet">';
				tweetsHTML[tweetsHTML.length] = tweet.text;
				tweetsHTML[tweetsHTML.length] = '</span><br /><span class="tweet-meta"><span class="tweet-time" title="';
				tweetsHTML[tweetsHTML.length] = tweet.created;
				tweetsHTML[tweetsHTML.length] = ' UTC+4"><a href="http://twitter.com/';
				tweetsHTML[tweetsHTML.length] = tweet.screen_name;
				tweetsHTML[tweetsHTML.length] = '/status/';
				tweetsHTML[tweetsHTML.length] = tweet.tweet_id;
				tweetsHTML[tweetsHTML.length] = '">';
				tweetsHTML[tweetsHTML.length] = tweet.since;
				tweetsHTML[tweetsHTML.length] = '</a></span> by <span class="by-tag">';
				tweetsHTML[tweetsHTML.length] = tweet.name;
				tweetsHTML[tweetsHTML.length] = '</span></span></div></li>';
			});
			
			$(container)[concatMethod](tweetsHTML.join(''));

			if (loader)
			{
				$(loader).removeClass('loading');
			}
			
			Lens.tweetsLoaded[container] = $(container).find('li').length;
			
			if (tweetsLoadedEvent && Lens.tweetsLoaded[container] > 0)
			{
				$('body').trigger(tweetsLoadedEvent, [ Lens.tweetsLoaded[container] ]);
			}
		})
	}
}

$(document).ready(function()
{
	// listen for tweets every 5 minutes
		
	setInterval(Lens.listenForTweets, 300000);
	
	$('body').bind('tweetsAvailable', function(event, newTweetsCount)
	{
		$('#new-tweets-count').html(newTweetsCount);
		$('#new-tweets-alert').show();	
	});
	
	$('#see-new-tweets').click(function(event)
	{
		Lens.renderNewTweets();
		Lens.clearNewTweetsAlert();
	});
	
	$('#more-tweets').click(function(event)
	{
		event.preventDefault();
		
		if ($(this).hasClass('loading'))
			return false;
			
		$(this).addClass('loading');
		
		Lens.loadTweets({'older_than_id': $('#tweets li:last-child').addClass('bookmark').attr('id').substring(1)}, '#tweets', 'append', '#more-tweets');
	});
	
//	$('#search-form').submit(function(event)
//	{
//		event.preventDefault();
		
//		var input = $('#search-input');
		
//		if (input.hasClass('loading'))
//			return false;
			
//		input.addClass('loading');
		
//		$('#tweets-panel h2').addClass('loading').text('Search results for "' + input.val()  + '"').prepend('<a class="home-link" href="#">Home</a> &raquo; ');
		
//		Lens.loadTweets({query: input.val()}, '#tweets', 'html', '#search-input,#tweets-panel h2');
		
//	});
	
//	$('#trends li').click(function(event)
//	{
//		if ($(this).hasClass('loading'))
//			return false;
//			
//		$(this).addClass('loading');
//		
//		var topic = $(this).find('span').text();
//		
//		$('#tweets-panel h2').addClass('loading').text('Search results for "' + topic + '"').prepend('<a class="home-link" href="#">Home</a> &raquo; ');
//		
//		Lens.loadTweets({query: topic}, '#tweets', 'html', '#' + $(this).attr('id') + ',#tweets-panel h2');
		
//	});
	
	$('.home-link').live('click',function(event)
	{
		event.preventDefault();
		$('#tweets-panel h2').addClass('loading').text('Latest tweets from the UAE');
		Lens.loadTweets({query: ''}, '#tweets', 'html', '#tweets-panel h2');
	});
	
//	if (typeof document.location.hash == 'string' && document.location.hash.charAt(0) == '#')
//   {
//
//		$('#tweets-panel h2').addClass('loading').text('Search results for "' + document.location.hash  + '"').prepend('<a class="home-link" href="#">Home</a> &raquo; ');
//
//		Lens.loadTweets({query: document.location.hash }, '#tweets', 'html', '#search-input,#tweets-panel h2');
//    }
	
});

jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox() 
})