// JavaScript Document
var SecondsLeft = 0;

// For changing start time change this variables 
var startDays = 10;   // days
var startHours = 12;  // hours
var startMinutes = 15;// minutes
var startSeconds = 2; // seconds

function RunTimer(days,hours,minutes,seconds)
{
	SecondsLeft = days*86400+hours*3600+minutes*60+seconds;
	UpdateTimer(timer);
}
function SecondsToTimeString() // convert seconds to good string format
{
		var tempSeconds = SecondsLeft;
		var days = parseInt(tempSeconds / 86400,10);
		tempSeconds = tempSeconds % 86400;
		var hours = parseInt(tempSeconds / 3600,10);
		tempSeconds = tempSeconds % 3600;
		var minutes = parseInt(tempSeconds / 60,10);
		tempSeconds = tempSeconds % 60;
		var output = "";
		
		if(days > 0){
			if (days == 1)
				{output += days+" day";}
			else 
				{output += days+" days";}
		}
		
		output += (hours<10?"0":"")+hours+":";
		output += (minutes<10?"0":"")+minutes+":";
		output += (tempSeconds<10?"0":"")+tempSeconds;
		return output
}

function UpdateTimer()
{
 // current time is shown in the Element DIV with ID='Timer'
 document.getElementById('Timer').innerHTML =	SecondsToTimeString();
  if (SecondsLeft <= 0)
		return;
  SecondsLeft--;
  setTimeout('UpdateTimer()',1000);
}

var countDownCounter; 

function set_lotto_countdown(pCaptionDay, pCaptionDays,countDownCounter1)
{
	sCaptionDay = pCaptionDay;
	sCaptionDays = pCaptionDays;
	
	countDownCounter = countDownCounter1;
	
	resetDrawCountdown1(0);
}

function resetDrawCountdown1(offset)
{
	//alert('resetDrawCountdown1. countDownCounter: '+countDownCounter);
	for  (var index=1; index<=countDownCounter; index++) 
	{
		setTime(index, offset, parseInt(eval('time_Y'+index)), parseInt(eval('time_M'+index)), parseInt(eval('time_D'+index)), parseInt(eval('time_HH'+index)), parseInt(eval('time_MM'+index)));	
	}

	
	//setting the function timeout 1 second
	offset = parseInt(offset) + 1000;
	setTimeout("resetDrawCountdown1("+offset+")",1000);
}


function setTime(index, offset, closeYear, closeMonth, closeDay, closeHour, closeMinute, closeSecond)
{ 
	var dtClose = new Date(closeYear, parseInt(closeMonth) - 1 , closeDay, closeHour, closeMinute);
	var milliseconds1 = dtClose.getTime();
	var milliseconds2 = dtNow.getTime();
	var difference = milliseconds1 - (milliseconds2 + offset);
	var prevDifference = milliseconds1 - (milliseconds2 + offset - 1000); //the previous offset
	
	var d = Math.floor(difference / (1000 * 60 * 60 * 24));
	var h = Math.floor(difference / (1000 * 60 * 60)) - d * 24;
	var m = Math.floor(difference / (1000 * 60)) - d * 24 * 60 - h * 60;
	var s = Math.floor(difference / (1000)) - d * 24 * 60 * 60 - h * 60 * 60 - m * 60;
	
	var prevD = Math.floor(prevDifference / (1000 * 60 * 60 * 24));
	var prevH = Math.floor(prevDifference / (1000 * 60 * 60)) - d * 24;

	//setting the day's image or text
	var daysImage;
	var isNextChangeImage = false;
	
	if (d < 1)
	{
		daysImage = "";
		/*if (h >= 0 && h < 1) 
		{
			daysImage = "<img src='images/test.congalotto.com/closing_1.gif'>";
		}
		if (h >= 1 && h < 6) 
		{
			daysImage = "<img src='images/test.congalotto.com/closing_2.gif'>";
		}
		if (h >= 6 && h < 12) 
		{
			daysImage = "<img src='images/test.congalotto.com/closing_3.gif'>";
		}
		if (h >= 12 && h < 24) 
		{
			daysImage = "<img src='images/test.congalotto.com/closing_4.gif'>";
		}*/
	}
	else
	{
		
		if (d == 1)
			daysImage = d + " " + sCaptionDay;
		else
			daysImage = d + " " + sCaptionDays;
	}
	
	//adding leading '0' for 1 digit numbers
	if (h<10)
		h = "0"+h.toString();
	if (m<10)
		m = "0"+m.toString();
	if (s<10)
		s = "0"+s.toString();
	
	
	//changing the days/hours image only every change of hour or for the first time
	if (prevH!=h || prevD!=d || offset<=0)
	{

		setValue('td_lotto_countdown_days_'+index, daysImage);
	}
	var timeStr = h + "h " + m + "m " + s + "s";
	setValue('td_lotto_countdown_time_'+index, timeStr);
}


function setValue(id, value) 
{
	var obj;
	obj=document.getElementById(id);
	if (obj!=null) {
		obj.innerHTML = value;
	}
}

