/**
 * User: Shane Johnson
 * Date: 05/04/11
 * Time: 09:57
 */

var SUCCINCTCOMMS = SUCCINCTCOMMS || {};

SUCCINCTCOMMS.twitterFeed = function(e)
{
	var tweets = [], $container, index = 0, play, html;

	if (e != undefined)
	{
		e.preventDefault();
	}
	tweets = [];
	index = 0;
	$container = $('#tweets');
	if ($container.length)
	{
		$container.fadeOut(1000);

                /* The line below was originally just q=%23BOPA but they wanted only BOPAChair tweets instead of pulling in two sets of tweets - quick hack for speed */
		$.getJSON("http://search.twitter.com/search.json?callback=?&rpp=10&q=%23BOPAChair",
		          function(data)
		          {
			          $.each(data.results, function(key, val)
			          {
				          html = "<br />";
				          html += prepTweet(val);
				          tweets.push(html);
			          });
			          getAboutChairTweets();
		          });

		function getAboutChairTweets()
		{
			$.getJSON("http://search.twitter.com/search.json?callback=?&rpp=10&q=@BOPAChair",
			          function(data)
			          {
				          $.each(data.results, function(key, val)
				          {
					          html = "<br />";
					          html += prepTweet(val);
					          tweets.push(html);
				          });
				          getChairTweets();
			          });
		}

		function getChairTweets()
		{
			$.getJSON("http://search.twitter.com/search.json?callback=?&rpp=10&q=from:BOPAChair",
			          function(data)
			          {
				          $.each(data.results, function(key, val)
				          {
					          html = "<br />";
					          html += prepTweet(val);
					          tweets.push(html);
				          });
				          changeDisplay();
			          });
		}
	}

	function prepTweet(val)
	{
		var html = "";
		html += "<strong>Tweeted about ";
		var then = new Date(val.created_at);
		var now = new Date();
		var diff = (now - then) / 3600000;
		var s = "s";
		if (Math.floor(diff) == 1)
		{
			s = "";
		}
		var diffText = " hour" + s + " ago";
		if (diff < 1)
		{
			diff *= 60;
			if (Math.floor(diff) == 1)
			{
				s = "";
			}
			diffText = " minute" + s + " ago";
		}
		else if (diff > 23)
		{
			diff /= 24;
			if(Math.floor(diff) == 1){
				s = "";
			}
			diffText = " day" + s + " ago";
		}
		var withlinks = val.text.replace(/(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a target="_blank" href="$1">$1</a>');
		var withpeeps = withlinks.replace(/@([A-Z_0-9]*)/gi, '<a target="_blank" href="http://twitter.com/$1">@$1</a>');
		var withsearches = withpeeps.replace(/#([A-Z_0-9]*)/gi, '<a target="_blank" href="http://twitter.com/#!/search/$1">#$1</a>');

		html += (Math.floor(diff) + diffText + " by <a href='http://twitter.com/#!/" + val.from_user + "'>@" + val.from_user + "</a></strong>");
		html += "<hr />";
		html += (withsearches);
		return html;
	}

	function changeDisplay()
	{
		if (index == 0)
		{
			play = setInterval(changeDisplay, 6000);
			displayTweet();
		}
		else if (index < tweets.length)
		{
			$container.fadeOut(1000, function()
			{
				displayTweet();
			})
		}
		else
		{
			clearInterval(play);
			SUCCINCTCOMMS.twitterFeed();
		}

		function displayTweet()
		{
			$container.empty();
			$container.hide();
			$container.append(tweets[index]);
			$container.fadeIn(1000);
			index++;
		}
	}
};

(function($)
{
	$(function()
	  {
		  SUCCINCTCOMMS.twitterFeed();
	  });
}(jQuery));

