/***************************************************
	HP Game Alert
	* display alert for live games
	- Last update: 7/18/2006, HM
***************************************************/
var alerthp_obj = null;
var alerthp_interval = 10000;
var alerthp_dispCntr = 0;
var cust_cntr = 0;
var customAlertEnable = true;

var HPAlert = {

	xmlHttpReq_Alert : function() {
		
		var xmlhttpJ;

		if (window.XMLHttpRequest) {
			xmlhttpJ = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			xmlhttpJ = new ActiveXObject("Msxml2.XMLHTTP");
		}
		return xmlhttpJ;
	
	},
	
	loadJSON_HPAlert : function() {
	
		var alertLink = "";
		var rnum = (Math.round((Math.random()*900000)+1))
		alertLink = (customAlertEnable)?"/frags/wnbaHPCustomAlertData.html?"+rnum:"/scores/wnbaGameAlerts.jsp";
	
		try {
			var xmlhttpJ = this.xmlHttpReq_Alert();
			
			if (xmlhttpJ){
				xmlhttpJ.open("GET",alertLink,false);
				xmlhttpJ.send(null);
				if (xmlhttpJ.readyState==4) {
					alerthp_obj = eval('(' + xmlhttpJ.responseText + ')');
					//alert(xmlhttpJ.responseText );
					if (!customAlertEnable) {
						this.checkAlert();
					}
					else {
						this.customAlertDisplay();
					}
					//console.log(xmlhttpJ.getAllResponseHeaders());
				}
			}
		
			//setTimeout("HPAlert.loadJSON_HPAlert()", alerthp_interval);
		}
		catch(e) {
			window.status = e.message;
		}
		
	},
	
	checkAlert : function() {

		if (alerthp_obj["games"] == "none") {
			// increment interval
			if (alerthp_interval < 120000) alerthp_interval = alerthp_interval + 5000;
			document.getElementById("gameAlerts").style.display = "none";
		}
		else {
			// switch back to original interval
			alerthp_interval = 15000;
			document.getElementById("gameAlerts").style.display = "block";
			this.displayAlert();
		}
	
	},
	
	displayAlert : function() {
		//alert(alerthp_dispInterval);
		if (alerthp_dispCntr < alerthp_obj["games"].length) {
			document.getElementById("alertGame").innerHTML = alerthp_obj["games"][alerthp_dispCntr];
			alerthp_dispCntr++;
			setTimeout("HPAlert.displayAlert()", 10000);
		}
		else {
			alerthp_dispCntr = 0;
			setTimeout("HPAlert.loadJSON_HPAlert()", alerthp_interval);
			return;
		}
	
	},
	
	customAlertDisplay : function() {
		
		customAlertEnable = alerthp_obj["enable"]; 
		//window.status = "len = " + alerthp_obj["display"].length;
		//alert(customAlertEnable);
		//alert(alerthp_obj["display"].length);
		/*
		if (customAlertEnable) {
		
			if (!alerthp_obj["display"][cust_cntr]=="undefined") {
				document.getElementById("gameAlerts").style.display = "block";
				document.getElementById("alertGame").innerHTML = alerthp_obj["display"][cust_cntr];
			}
			cust_cntr++;
			cust_cntr = (cust_cntr > alerthp_obj["display"].length-1)?0:cust_cntr;
			setTimeout("HPAlert.customAlertDisplay()", 10000);
		}
		else {
			document.getElementById("gameAlerts").style.display = "none";
		}
		*/
		
		
		if (customAlertEnable) {
			if (cust_cntr < alerthp_obj["display"].length) {
				document.getElementById("gameAlerts").style.display = "block";
				document.getElementById("alertGame").innerHTML = alerthp_obj["display"][cust_cntr];
				cust_cntr++;
				setTimeout("HPAlert.customAlertDisplay()", 10000);
			}
			else {
				cust_cntr = 0;
				HPAlert.loadJSON_HPAlert();
				return;
			}
		}
		else {
			document.getElementById("gameAlerts").style.display = "none";
			HPAlert.loadJSON_HPAlert();
		}
		
		
	}
	
	

}


//HPAlert.loadJSON_HPAlert();