/**
 * @author Artjom Kurapov
 * @since 29.08.11 13:44
 * @modified Agne Lund
 * @since 07.11.11 10:02
 */
var iLatestTweetID=0;
var aTweets = [];

function twitterCallback(aFetchedTweets) {
	aLastTweet = aFetchedTweets[0];
	var statusHTML = '';
	//var username = aLastTweet.user.screen_name;
	
	var iFetchedTweetCount = aFetchedTweets.length;
	
	for(var i = 0; i < iFetchedTweetCount; ++i) {
		
		var iTweetCount = aTweets.length;
		var bAlreadyInStack = false;
		for(var ii = 0; ii < iTweetCount; ++ii) {
			if(aFetchedTweets[i] == aTweets[ii].id) {
				bAlreadyInStack = true;
			}
		}
		
		if(bAlreadyInStack) {
			continue;
		}
		else {
			aFetchedTweets[i].text_formatted = aFetchedTweets[i].text
				.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,
					function(url) {
						return '<a href="' + url + '">' + url + '</a>';
					})
				.replace(/\B@([_a-z0-9]+)/ig, function(reply) {
					return  reply.charAt(0) + '<a href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
				});
			aTweets.push(aFetchedTweets[i]);
		}
		
	}
	
	
	aTweets = aTweets.sort(sortTweetById);
	TwitterTicker.setTweets(aTweets);
	/*
	statusHTML = aLastTweet.text
		.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,
			function(url) {
				return '<a href="' + url + '">' + url + '</a>';
			})
		.replace(/\B@([_a-z0-9]+)/ig, function(reply) {
			return  reply.charAt(0) + '<a href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
		});


	if(aLastTweet.id>iLatestTweetID){
		iLatestTweetID = aLastTweet.id;
		$('p.front-twitter').html(statusHTML).slideDown();
		$('.side-twitter p').html(statusHTML);
		$('.side-twitter').slideDown();
		$('.side-twitter div.a a').attr('href','http://twitter.com/#!/'+aLastTweet.user.screen_name);
	}
	*/
	//document.getElementById('latest_tweet').innerHTML = statusHTML.join('');
}


var TwitterTicker = {
	tweets : [],
	active_tweet_idx : 0,
	timeout : null,
	_polling : null
};

TwitterTicker.setTweets = function(aTweets) {
	TwitterTicker.tweets = aTweets;
}

TwitterTicker.init = function(aTweets) {
	TwitterTicker.setTweets(aTweets);
	if($('#twitter_scroll').size() == 0) {
		$('p.front-twitter').wrapInner('<span id="twitter_scroll" />');
		$('.side-twitter p').wrapInner('<span id="twitter_scroll" />');
	}
	TwitterTicker.start();
}

TwitterTicker.start = function() {
	if(TwitterTicker.tweets.length < 1) {
		TwitterTicker._polling = setTimeout(function() {TwitterTicker.start()}, 100);
		return;
	}
	$('#twitter_scroll').html(TwitterTicker.tweets[TwitterTicker.active_tweet_idx].text_formatted);
	if($('#twitter_scroll').parent().hasClass("front-twitter")){
		TwitterTicker.centerText();
	}
	$('p.front-twitter').slideDown();
	$('.side-twitter').slideDown();
	
	TwitterTicker.timeout = setTimeout(
		function() {
			TwitterTicker.next();
		}, 5000
	);
}
TwitterTicker.centerText = function(){
	$tmp = $("<div style='width:870px; font-size: 16px; position: absolute; left: -900px'></div>");
	$('body').append($tmp);
	$tmp.html(TwitterTicker.tweets[TwitterTicker.active_tweet_idx].text_formatted);
	if($tmp.height()>22){
		$('#twitter_scroll').parent().css({"padding-top":"12px","padding-bottom":"12px"});
	}
	else{
		$('#twitter_scroll').parent().css({"padding-top":"","padding-bottom":""});
	}
	$tmp.remove();
}
TwitterTicker.next = function() {
	if(TwitterTicker.active_tweet_idx + 1 >= TwitterTicker.tweets.length) {
		TwitterTicker.active_tweet_idx = 0;
	}
	else {
		TwitterTicker.active_tweet_idx++;
	}
	
	$('#twitter_scroll').fadeOut(400, function() {
		$('#twitter_scroll').html(TwitterTicker.tweets[TwitterTicker.active_tweet_idx].text_formatted);
		if($('#twitter_scroll').parent().hasClass("front-twitter")){
			TwitterTicker.centerText();
		}
		$('.side-twitter div.a a').attr('href','http://twitter.com/#!/'+TwitterTicker.tweets[TwitterTicker.active_tweet_idx].user.screen_name);
		$('#twitter_scroll').fadeIn(400, function(){
			TwitterTicker.timeout = setTimeout(function() {
				TwitterTicker.next();
				}, 5000
			);
		});
	});
}


function sortTweetById(a,b) {
	return b.id - a.id;
}



$(document).ready(function() {
	$.getJSON(js_site_url + 'static/RovioHelpers/get_tweets', function(data) {
		twitterCallback(data);
		TwitterTicker.init(aTweets);
	});
});

