/// Made by Ing. Karel Trcalek -> no copying alowed
function AJAX() {
	try {
		xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
		return xmlHttp;
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
			return xmlHttp;
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				return xmlHttp;
			} catch (e) {
				alert("Your browser does not support AJAX.");
				return false;
			}
		}
	}
}

// Timestamp for preventing IE caching the GET request (common function)

function fetch_unix_timestamp() {
	return parseInt(new Date().getTime().toString().substring(0, 10))
}

function LoadDateDifferences(timezone,countto) {	
	// Customise those settings
	//document.getElementById('sync').innerHTML = 'synchronizing..';
	var divid = "countdown";
	var url = "classes/ajax/date_diff.php";
	// Create xmlHttp
	var xmlHttp_two = AJAX();

	// No cache

	var timestamp = fetch_unix_timestamp();
	var nocacheurl = url + "?t=" + timestamp + "&timezone=" + timezone + "&countto=" + countto;

	// The code...

	xmlHttp_two.onreadystatechange = function() {
		if (xmlHttp_two.readyState == 4) {			
			var countdown =  xmlHttp_two.responseText;
			setCookie('date_diff',countdown,2);			
			setTimeout('LoadDateDifferences("'+timezone+'","'+countto+'")', 30000);
		}
	}
	xmlHttp_two.open("GET", nocacheurl, true);
	xmlHttp_two.send(null);
}


function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

//converting time difference from miliseconds to real day, hours, minutes, secs
function convert_to_time(secs)
{
	secs = parseInt(secs);	
	hh = secs / 3600;	
	hh = parseInt(hh);	
	mmt = secs - (hh * 3600);	
	mm = mmt / 60;	
	mm = parseInt(mm);	
	ss = mmt - (mm * 60);	
		
	if (hh > 23)	
	{	
	   dd = hh / 24;	
	   dd = parseInt(dd);	
	   hh = hh - (dd * 24);	
	} else { dd = 0; }	
		
	if (ss < 10) { ss = "0"+ss; }	
	if (mm < 10) { mm = "0"+mm; }	
	if (hh < 10) { hh = "0"+hh; }	
	if (dd == 0) { return (hh+":"+mm+":"+ss); }	
	else {	
		if (dd == 1) { return (dd+" den "+hh+":"+mm+":"+ss); }
		if ( (dd > 1) && (dd < 5) ) { return (dd+" dny "+hh+":"+mm+":"+ss); }
		if ( (dd > 5) || (dd == 5) ) { return (dd+" dnů "+hh+":"+mm+":"+ss); }
	}	
}

//method for countdown
function DoCountDown() {	
	countdown = getCookie('date_diff');	
	aco = countdown;
	//alert(convert_to_time(aco));
	if (countdown < 0)	
	{ 			
		//document.location.href = "<?=$_GET['data']?>";
		//window.location.reload();
		document.getElementById('countdown').innerHTML = '<span>RELEASED!</span>';
		// change text
		//document.getElementById('cd').innerHTML = "<?=$_GET['data']?>";
	}	
	else	
	{	
		document.getElementById('countdown').innerHTML = '<span>'+convert_to_time(countdown)+'</span>';
		setTimeout('DoCountDown()', 1000);					
	}		
	countdown = countdown - 1;	
	setCookie('date_diff',countdown,2);	
}


