/*function spillesNaa() {
	var xmlhttp;
	
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			document.getElementById("direttore").innerHTML=xmlhttp.responseText;
		}
	}
xmlhttp.open("GET","http://www.radioharstad.no/dirEttore_siste_php.php",true);
xmlhttp.send(null);
}
*/

function formaterTid(secs) {
	var hours = Math.floor(secs / (60 * 60));

	var divisor_for_minutes = secs % (60 * 60);
	var minutes = Math.floor(divisor_for_minutes / 60);

	var divisor_for_seconds = divisor_for_minutes % 60;
	var seconds = Math.ceil(divisor_for_seconds);

	var obj = {
		"h": hours,
		"m": minutes,
		"s": seconds
	};
	if( obj.s < 10 ) {
		obj.s = "0" + obj.s;
	}
	time = obj.m + ":" + obj.s;
	return time;
}

function spillesNaa() {
	$.getJSON('http://www.radioharstad.no/dirEttore_siste_php.php', '', function(json) {
		if( !json || json.spilt === null || json.spilt === '' || json === null ) {
			setTimeout("spillesNaa()", 1000);
		} else {
			secs = json.spilt;
			$('#artist').html(json.artist);
			$('#tittel').html(json.tittel);
			$('#spilt').html(formaterTid(json.spilt));
			$('#lengde').html(formaterTid(json.lengde));
			tellNed(json.spilt);
		}
	});
}

function tellNed(spilt){
	if( spilt < 2 ) {
		setTimeout("spillesNaa()", 3000);
		return 0;
	} else {
		spilt = spilt - 1;
		$('#spilt').html(formaterTid(spilt));
		setTimeout("tellNed(" + spilt + ")", 1000);
	}
}
