
var cs = 0;
var cm = 0;
var ch = 0;

var arrSeconds = new Array();
var arrMinutes = new Array();
var arrHours   = new Array();

var currentSeconds;
var currentMinutes;
var currentHours;
var currentTime	;
var t1, t2, t3;

function updateSeconds()
{	
	$.ajax({
			url : "seconds.php", 
			success : function( results ) {
			$(results).find("div.seconds").each(function(i)
			{		
				var div_id_seconds = $(this).attr("id");
				if( $("#twitter-update-seconds div#"+div_id_seconds).length > 0 )
				{
					$("#twitter-update-seconds").empty();
					updateSeconds();
				}
				if( $("#twitter-update-seconds div#"+div_id_seconds).length == 0 )
				{
					clearTimeout(t1);
					$("#twitter-update-seconds").append(this);			
					if(cs > 0) $("#twitter-update-seconds div#"+currentSeconds).animate({ opacity: 0, height: 0 }, 1500, function() { $(this).hide(); t1 = setTimeout("updateSeconds()", 1000); });
					currentSeconds = div_id_seconds;
					cs++;			
				}
				
			});
   		}
	});		
}

function updateMinutes()
{
	$.ajax({
			url : "minutes.php", 
			success : function( results ) {
			$(results).find("div.minutes").each(function(i)
			{			
				var div_id_minutes = $(this).attr("id");
				if($("#twitter-update-minutes div#"+div_id_minutes).lengthh > 0 )
				{
					$("#twitter-update-minutes").empty();
					updateMinutes();
				}		
				if( $("#twitter-update-minutes div#"+div_id_minutes).length == 0 )
				{
					clearTimeout(t2);
					$("#twitter-update-minutes").append(this);			
					if(cm > 0) $("#twitter-update-minutes div#"+currentMinutes).animate({ opacity: 0, height: 0 }, 1500, function() { $(this).hide(); t2 = setTimeout("updateMinutes()", 5000); });
					currentMinutes = div_id_minutes;
					cm++;			
				}
			});
   		}
	});		
}

function updateHours()
{
	$.ajax({
			url : "hours.php?field="+getCurrentHour(),
			success : function( results ) {
			$(results).find("div.hours").each(function(i)
			{
				var div_id_hours = $(this).attr("id");	
				if( $("#twitter-update-hours div#"+div_id_hours).length > 0 )
				{
					$("#twitter-update-hours").empty();
					updateHours();
				}
				if( $("#twitter-update-hours div#"+div_id_hours).length == 0 )
				{
					clearTimeout(t3);
					$("#twitter-update-hours").append(this);			
					if(ch > 0) $("#twitter-update-hours div#"+currentHours).animate({ opacity: 0, height: 0 }, 1500, function() { $(this).hide(); t3 = setTimeout("updateHours()", 5000); });
					currentHours = div_id_hours;
					ch++;
				}
			});
   		}
	});		
}

function switchCSS($value)
{
	document.getElementById('css').href = $value;
	if($value == 'css/light.css')
	{
		document.getElementById('sun').style.display = 'none';
		document.getElementById('moon').style.display = 'block';
	}
		else
	{
		document.getElementById('sun').style.display = 'block';
		document.getElementById('moon').style.display = 'none';
	}
}

function checkTime()
{
	currentTime = getCurrentHour();
	if (currentTime >= 18 || currentTime <=4) {
		switchCSS('css/dark.css');
	}
		else
	{
		switchCSS('css/light.css');
	}
}

function getCurrentHour()
{
	var now = new Date();
	var actualTime = now.getHours();
	if(actualTime<=9) actualTime = "0"+actualTime;
	return actualTime;
}

$(document).ready(function()
{	
	currentTime = getCurrentHour();
	checkTime();
	updateSeconds();
	updateMinutes();
	updateHours();
	setTimeout("updateSeconds()", 1000);
	setTimeout("updateMinutes()", 1000);
	setTimeout("updateHours()",   1000);
	setInterval("checkTime()", 60000);
});