/*
 * jTwitter 1.1.1 - Twitter API abstraction plugin for jQuery
 */
(function(b){b.extend({jTwitter:function(d,a,c){if(!(d=="undefined"||a=="undefined")){if(b.isFunction(a)){c=a;a=5}b.getJSON("http://twitter.com/status/user_timeline/"+d+".json?count="+a+"&callback=?",function(e){b.isFunction(c)&&c.call(this,e)})}}})})(jQuery);

var tweets = Array();

var days = 
{
	Mon : 0,
	Tue : 1,
	Wed : 2, 
	Thu : 3,
	Fri : 4,
	Sat : 5,
	Sun : 6
}

var months = 
{
	Jan : 0,
	Feb : 1,
	Mar : 2, 
	Apr : 3,
	May : 4,
	Jun : 5,
	Jul : 6,
	Aug : 7,
	Sep : 8,
	Oct : 9,
	Nov : 10,
	Dec : 11
}

function twStringToDate( date ){
	var regxTime = /(\d{2}:\d{2}:\d{2})/gi;
	aux = date.split(" ");
	time = aux[3].match(regxTime)[0].toString().split(":");
	//return(aux[5]+months[aux[1]]+aux[2]+aux[3].replace(":","").replace(":",""))
	return new Date(aux[5], months[aux[1]], aux[2], time[0], time[1], time[2]);
}

function sortByDate(data){
	
	for(i=0; i<data.length; i++)
	{
		temp = data[i]
		for(j=i+1; j<data.length; j++){		
			//var d1 = new Date(data[i].created_at.toString());
			//var d2 = new Date(data[j].created_at.toString());
			var d1 = twStringToDate(data[i].created_at);
			var d2 = twStringToDate(data[j].created_at);
			if( d1 < d2 ){
				data[i] = data[j];
				data[j] = temp;
			}
		}
	}	
}

function printTweets(tweets){
	var urlregex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
    var userregex = /(\@([-A-Z0-9+&\/%?=~_|!:,.;]*))/gi;
    var hashtagregex = /(\#([-A-Z0-9+&\/%?=~_|!:,.;]*))/gi;
	jQuery('#first_tweet').empty();
	jQuery('#rest_tweets').empty();
	
	for(i=0; i<4; i++){		
		in_reply = "";
		if(tweets[i].in_reply_to_screen_name != null){ in_reply = "in reply to "+tweets[i].in_reply_to_screen_name; }
		if(i==0){
			date = twStringToDate(tweets[i].created_at);
			date = ' '+date.toGMTString();
		}
		else{date=""}
		
		html = '<div class="post">'
			  +'<div class="txt"> '+tweets[i].text.replace(urlregex,"<a class='url' href='$1' rel='nofollow'>$1</a>").replace(userregex,"<a class='user' href='http://twitter.com/$2' rel='nofollow'>$1</a>").replace(hashtagregex,"<a class='hashtag' href='http://twitter.com/#search?q=$1' rel='nofollow'>$1</a>")+'</div>'
			  +'</div>'
			  +'<div class="source">from '+tweets[i].user.screen_name+','+ date +'  via '+tweets[i].source+' '+in_reply+'</div>'
			  +'<div class="actions"><a class="reply" href="http://twitter.com/home?status=@'+tweets[i].user.screen_name+' &amp;in_reply_to_status_id=" + json[i].id + "&amp;in_reply_to='+tweets[i].user.screen_name+'" rel="nofollow" target="_blank">Responder</a> <a class="retweet" href="http://twitter.com/'+tweets[i].user.screen_name+'" rel="nofollow" title="'+tweets[i].text+'" target="_blank"></a></div>'
			  	
		if(i==0){ jQuery('#first_tweet').append(html);}
		else{ jQuery('#rest_tweets').append('<li>'+html+'</li>'); }
	}
	jQuery('#rest_tweets li').last().addClass('last');
	startRetweet();
	jQuery('#cnt_tw').addClass('loaded');
}


function checkLimit(){
	//Limit exceded
	if (!jQuery('#cnt_tw').hasClass('loaded')){
		jQuery('#cnt_tw').html('<div class="msg"><p></p><h3><a href="http://twitter.com/bspress">BSpress</a></h3><h3><a href="http://twitter.com/bancosabadell">Banco Sabadell</a></h3></div>');
	}
}

jQuery(document).ready(function(){ 

	jQuery.jTwitter('bspress', 4, function(data){				
		jQuery.each(data, function(i, post){ tweets.push(post); });
	});	
	jQuery.jTwitter('bancosabadell', 4, function(data){		
		jQuery.each(data, function(i, post){	tweets.push(post); });	
		sortByDate(tweets);	
		printTweets(tweets);
	});
	
	setTimeout('checkLimit()', 20000);

});
//END jTwitter

