// fesztival elso napjanak datuma
var month = '8';
var day = '6';
// day of week sun=1 sat=7 or 0 for whatever day it falls on
var dow = 0;
// hany oratol valtozzon (pl a rendezveny elso napjan hajnali kettokor mar aznap van, elotte "meg 1 nap")
var hour = 1;
var tz = 2;
// offset in hours from UTC to your timezone
var lab = 'cd';
// id of the entry on the page where the counter is to be inserted

var elonapok=2;
var fesztivalnapok=6;

function start() {
	displayCountdown(setCountdown(month, day, hour, tz), lab);
}
var pageLoaded = 0;
xGUI.DOM.Window.CallWhenLoaded(start);
/*
window.onload = function() {
	start();
}
*/

function setCountdown(month, day, hour, tz) {
	var m = month;
	if (month == '*') m = 0;
	var c = setC(m, day, hour, tz);
	if (month == '*' && c < 0) c = setC('*', day, hour, tz);
	return c;
}
function setC(month, day, hour, tz) {
	var toDate = new Date();
	
	if (month == '*') toDate.setUTCMonth(toDate.getUTCMonth() + 1);
	else if (month > 0) {
		if (month <= toDate.getUTCMonth()) toDate.setUTCFullYear(toDate.getUTCFullYear() + 1);
		toDate.setUTCMonth(month - 1);
	}
	
	if (day.substr(0, 1) == '+') {
		var day1 = parseInt(day.substr(1));
		toDate.setUTCDate(toDate.getUTCDate() + day1);
	} else {
		toDate.setUTCDate(day);
	}
	
	if (dow > 0) toDate.setUTCDate(toDate.getUTCDate() + (dow - 1 - toDate.getUTCDay()) % 7);
	toDate.setUTCHours(hour);
	toDate.setUTCMinutes(0 - (tz * 60));
	toDate.setUTCSeconds(0);
	var fromDate = new Date();
	//fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
	var diffDate = new Date(0);
	diffDate.setMilliseconds(toDate - fromDate);

	// window.console.log(fromDate);
	// window.console.log(toDate);

	return Math.floor(diffDate.valueOf() / 1000);
}

function displayCountdown(countdn, cd) {
	//window.console.log(countdn);
	if(countdn<-(fesztivalnapok*86400)){
//		window.console.log('vege');
		document.getElementById('countdown').style.display='none';
		document.getElementById('counting').style.display='none';
 	}else if (countdn < (elonapok*86400)){
//		window.console.log('counting');
		document.getElementById('countdown').style.display='none';
		document.getElementById('counting').style.display='block';
		
		var days = Math.floor(countdn/86400) *-1;
		
		document.getElementById('cdDuring').innerHTML=String(days)+'.';
	}else {
//		window.console.log('meg countdown');
		document.getElementById('countdown').style.display='block';
		document.getElementById('counting').style.display='none';
		
		var secs = countdn % 60;
		if (secs < 10) secs = '0' + secs;
		var countdn1 = (countdn - secs) / 60;
		var mins = countdn1 % 60;
		if (mins < 10) mins = '0' + mins;
		countdn1 = (countdn1 - mins) / 60;		
		var hours = countdn1 % 24;
		var days = (countdn1 - hours) / 24;
		// visszaszamlalas alatt, ha csak 0.x nap van hatra, az meg a kovetkezo nap, tehat egy nap van
		days++;
		document.getElementById(cd).innerHTML = days;
		setTimeout('displayCountdown(' + (countdn - 1) + ',\'' + cd + '\');', 1800000);
	}
}
