/*

nowplaying.js

*/

//debug
var nprefreshdelay = 30*1000;//40*1000; // check every 30 seconds

npxhttp = new XMLHttpRequest();
npxhttp.onreadystatechange = function() {
	if(npxhttp.readyState == 4 ) {
		var response = npxhttp.responseText;
		if(response == "0") {
			prevShowID = -1;
			hidenp();
			setTimeout("checkNowPlaying()", nprefreshdelay);
			return;
		}
		try {
			temp = response.split(",");
			showid = temp[0];
			showname = temp[1];
			url = temp[2];
			width = temp[3];
			height = temp[4];
		} catch(e) {
			hidenp();
			setTimeout("checkNowPlaying()", nprefreshdelay);
			alert(e);// debug
			return;
		}
		if(prevShowID != showid || prevShowID == -1) {
			prevShowID = showid;
			checkInnerHTMLDefined();
			$("#nowplaying").hide(npIsHidden?0:npJQueryAnimationSpeed, function(){
				document.getElementById('nowplayingshowname').innerHTML = showname;
				document.getElementById('nowplayingimg').src = url;
				document.getElementById('nowplayingimg').width = width;
				document.getElementById('nowplayingimg').height = height;
				$("#nowplaying").show(npJQueryAnimationSpeed);
				npIsHidden = false;
				}
			);
		}
		// it shouldnt link to the show, it should link to the now playing page
		//document.getElementById('nowplayinglink').href = "viewshow?req="+showid+"S2010";
		setTimeout("checkNowPlaying()", nprefreshdelay);
	}
}

function checkNowPlaying() {
	npxhttp.open("GET", "nowplaying.php", true);
	npxhttp.send(null);
}

prevShowID = -1;
npIsHidden = true;
npJQueryAnimationSpeed = "slow";

function hidenp() {
	$("#nowplaying").hide(npJQueryAnimationSpeed);
	npIsHidden = true;
}

function shownp() {
	$("#nowplaying").show(npJQueryAnimationSpeed);
	checkInnerHTMLDefined();
	npIsHidden = false;
}

npInnerHTMLDefined = false;
function checkInnerHTMLDefined() {
	if(npInnerHTMLDefined) return;
	document.getElementById("nowplaying").innerHTML = document.getElementById("nowplayinginnerhtml").innerHTML;
	document.getElementById("nowplaying").style.margin = document.getElementById("nowplayinginnerhtml").style.margin;
	document.getElementById("nowplaying").style.padding = document.getElementById("nowplayinginnerhtml").style.padding;
	document.getElementById("nowplayinginnerhtml").innerHTML = "";
	npInnerHTMLDefined = true;
}


