var boxes = new Array();
var intervals = new Array();



function search_social()
{

	var d = new Date();
	var tzOffset = d.getTimezoneOffset()/60 * (-1);
	$.ajax({
	type: "POST",
	url: "./social_search.php",
	data: "tzo="+tzOffset+"&q="+escape($('#q').attr('value')),
	success: function(msg)
	{
		var id = intervals.shift();
		if(id)
		{
			clearInterval(id);
		}
		boxes = new Array();
		$('#twitter_container').empty();
		var tweets = $.evalJSON(msg);
		var previousChild;
		
		var tweetBox;
		for(var i=0; i<tweets.length; i++)
		{
			tweetBox = $(tweets[i]);
			boxes.push(tweetBox);
		}
		intervals.push(setInterval(add_box,600));
   	}});
}



function add_box()
{
	var tweetBox = boxes.shift();
	if(boxes.length == 0)
	{
		 $.blockUI({ message:  '<p><strong>Burp! ... Thank you for feeding your Twittgator</strong></p>', css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#00ACD7', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: .58,
            color: '#000' 
        } }); 
 		clearInterval(intervals.shift());
        setTimeout($.unblockUI, 2000); 
	}
	if(tweetBox)
	{
		tweetBox.hide();
		var tweetBoxes =  $('#twitter_container').children();
		if(tweetBoxes.length == 0)
		{
			$('#twitter_container').append(tweetBox);
		}
		else
		{
			previousChild = tweetBoxes[0];
			tweetBox.insertBefore(previousChild);
		}
		
		tweetBox.fadeIn(1000);
	}
}