// JavaScript Document
var interval_update;
var imageInterval;
var num_record;
var numItem = 18;
var currentItem = 1;
var itemVisibili = 3;
var cont = 0;

interval_update  = window.setInterval("ricerca();", 40000);
function ricerca()
{
	
	var dataString = "chiave="+ $(".change_keyword").html() +"&volte=" + cont; 
	
	$.ajax({
		type: "POST",
		url: "component/tweet.php",
		cache: false,
		data: dataString,
		success: function(msg){
			if(msg == "notozie non disponibili al momento ")
			{
				alert(msg);
				$('.tweet_container').html("");
				$('.tweet_container').html(msg);
				cont = 0;
			}
			
			if(cont != 0)
			{
				$('.tweet_container').append(msg);
			}
			else
			{
				$('.tweet_container').html("");
				$('.tweet_container').html(msg);
			}			
			cont++;
		}
		
	});
	
	if(cont == 0)
	{
		imageInterval = window.setInterval("changeItem();", 4000);
	}
}

function changeItem()
{
	//per effetto fadeIn fadeOut
	/*
	$(".item_" + currentItem).fadeOut("slow", function() {

		$(".item_" + (currentItem + 3) ).fadeIn("slow", function() {
				currentItem++;	
		});
	});
	*/
	
	$(".item_" + currentItem).stop().animate({
			//border: '0px',
			marginTop: '-141px'
			}, 450);
	
	currentItem++;
}

$(document).ready(function(){
var keyCorrente = $(".change_keyword").html();
var l = parseInt($(".change_keyword").width());
if(l < 70)
{
	var margine = 70 - l;
	$('.change_keyword').css("marginRight" , margine);
	
}
$('.keyword').click(function (e)
{	
	/*
	if($(e.target).attr("id") == keyCorrente)
	{
		return;
	}
	*/
	
	var l = parseInt($('.'+$(e.target).attr("id")).width());
	if(l < 70)
	{
		var margine = 70 - l;
		$('.change_keyword').css("marginRight" , margine);
		
	}
	else
	{
		$('.change_keyword').css("marginRight" , "0");
	}
	
	$('.change_keyword').html($(e.target).attr("title"));
	$('.change_keyword').attr("title", $(e.target).attr("title"));
	
	$('.'+$(e.target).attr("id")).html(keyCorrente);
	$('.'+$(e.target).attr("id")).attr("title", keyCorrente);
	
	//alert($(e.target).attr("id"));
	//$('.keyword_active').removeClass("keyword_active");
	//$('.'+$(e.target).attr("id")).addClass("keyword_active");
	//$('.change_keyword').attr("title", $(e.target).attr("title"));
	$('.tweet_container').html("<div class=\"loader\"><img src=\""+ sessionBase +"images/ajax-loader.gif\" height=\"16\" width=\"16\" border=\"0\" /></div>");
	clearInterval(imageInterval);
	clearInterval(interval_update);
	cont = 0;
	
	currentItem = 1;
	keyCorrente = $('.change_keyword').attr("title");
	//keyCorrente = $(e.target).attr("title");
	ricerca();
	interval_update  = window.setInterval("ricerca();", 65000);
});

});