
/**
 * Project:     TaggerNet - Social Hashtags for Twitter
 * File:        base.tagger.js
 *
 * Please do not copy or distribute this code without prior consent
 * from Webstrong Ltd.
 *
 * This software is protected under Irish Copyright Law.
 *
 * @link http://taggernet.com/
 * @copyright 2009 Webstrong Ltd
 * @author Iarfhlaith Kelly <ik at webstrong dot ie>
 * @package TaggerNet
 * @version 1.0
 *
 * For questions, help, comments, discussion, etc., please visit the
 * main Webstrong website. http://webstrong.ie
 *
 */

function taggerStart(vars)
{	
	// Show Loader to User
	$('#stat').html('Initializing...');
	$('#stop').show();

	$.get('/api/index.php?'+vars, function(data)
	{
		if(isNaN(data))
		{
			$('#stat').html('Hey, we need more data! Please finish the form.');
			$('#patience').hide();
			$('#intro').show();
		}
		else
		{
			$('#stat').html('Monitoring Twitter...');
			$('#stop, #loader').show();
			$('#state').html('go');
			taggerAsk();
		}
	});
}

function taggerAsk()
{	
	var tots;
	var tweets = '';
	var last = $('#last').html();
	
	$.getJSON('/api/index.php?action=ask&last='+last, function(data)
	{
		$('#spacer').hide();
		$('#feed').fadeIn('slow');
		
		// Display Traffic Details
		if (data.users != false && data.limit != false)
		{
			$('.traffic').fadeOut('fast', function()
			{
				$('#live_users').html(data.users);
				$('#remaining_calls').html(data.limit);
				$('.traffic').fadeIn();
			});
		}
		
		// Call This Function Again in 30 seconds
		if ($('#state').html() == 'go')
		{
			gear = setTimeout("taggerAsk()", 30000);
		}
		
		// Check if New Tweets Were Found
		if (data.response == false) return (false); else
		{	
			$('#last').html(data['response'][0].id);
			
			for(var i = 0; i < data.response.length; i++)
			{
				if (data.response[i].count_common_friends != false) {
					var title = data.response[i].count_common_friends + ' friends in common';
				}
				else 
				{
					var title = '';
				}
				
				tweets = tweets + '<div class=\'comments new\'>\
						           <img src=\'media/images/degree_'+data.response[i].degree_of_separation+'.png\' alt=\''+data.response[i].degree_of_separation+'\' class=\'degree\' title=\''+title+'\' />\
						           <img src=\'media/images/bg_transparent.png\' class=\'transparent\' height=75 width=1 />\
						           <img src=\''+data.response[i].profile_image_url+'\' class=\'avatar\' width=48 height=48 />\
						           <h3><a href=\'http://twitter.com/'+data.response[i].from_user+'\' target=_blank>'+data.response[i].from_user+'</a></h3>\
						           <p>'+data.response[i].text+'</p><div class=\'clear\'></div>\
						           <div class=\'pad\'><span>';
				
				if (data.response[i].degree_of_separation == '0')
				{
					tweets = tweets + 'It\'s you!';
				}
				else if (data.response[i].degree_of_separation == '1')
				{
					tweets = tweets + 'You follow this person.';
				}
				else if (data.response[i].degree_of_separation == '2')
				{
					tweets = tweets + title;
				}
				else
				{
					tweets = tweets + 'Not in your network.';
				}
				
				tweets = tweets + '</span><a href=\'http://twitter.com/'+data.response[i].from_user+'/statuses/'+data.response[i].id+'\' target=_blank>'+data.response[i].relative_time+'</a></div></div>';
      		}
			
			// Display New Tweets to Screen
			$('#innerFeed').fadeOut('fast', function()
			{
				$('#innerFeed').prepend(tweets).fadeIn('fast', function()
				{
					if (firstLoad === false) {
						$('.new').highlightFade({
							color: '#FFFF66',
							speed: 1000,
							iterator: 'exponential'
						}).removeClass('new');
					}
					else
					{
						firstLoad = false;
						$('.new').removeClass('new');
					}
				});
			});
			
			// Get Current Totals
			var totsNew = $('#totsNew').html();
			var totsTot = $('#totsTot').html();
			
			// Add them Together
			totsTot = totsTot*1 + data.response.length;
			
			// Update the HTML
			$('#totsNew').html(data.response.length);
			$('#totsTot').html(totsTot);
			$('#tots').fadeIn('slow');
		}
		
	});
}
