﻿// global vars
var isIE = false;			
var req = null;
var currentRequest = 0;
var loadTimerId = null;			
var connectTimerId = null;
var needRefresh = false;	
var errorMessage = "An AxisTV internal error has " +
	"been reported.  The details of the error have been logged to " +
	"the Application Event Log.  This page may not behave correctly " +
	"because of the error.";			
//functions
//initialize xmlhttprequest
function initReq ()
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) 
	{
		req = new XMLHttpRequest ();				
	} 
	else // branch for IE/Windows ActiveX version
	{
		if (window.ActiveXObject) 
		{
			isIE = true;
			req = new ActiveXObject ("Microsoft.XMLHTTP");
		}
	}	
	if (req) 
	{
		req.onreadystatechange = processReqChange;
	}			
}

function loadDoc () 
{    
	try 
	{	
		if (needRefresh == true)
		{
			window.clearTimeout (connectTimerId);
			connectTimerId = null;
			stop ();								
			window.location.reload (true);						
			return;
		}						
		loadXMLDoc ("Info.aspx?bulletin=" + currentRequest); 						
	}
	catch (e) 
	{
		stop ();
		//var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		//alert ("Unable to get XML data:\n" + msg);	
		//validationError.innerText=errorMessage;	
		setTimeout("window.location.reload (true)",5000);			
	}      
}			

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc (url) 
{		
	try
	{							
		if (!callInProgress (req))
		{								
			req.onreadystatechange = processReqChange;
			req.open ("GET", url, true);
			if (!connectTimerId)
			{							
				connectTimerId = window.setInterval ("failToConnect ()",timeoutInterval);//allows timeout to complete a request
			}
			if (!isIE) // branch for native XMLHttpRequest object
			{
				req.send (null);
			}
			else// branch for IE
			{
				req.send ();	
			}					
		}
	}
	catch (e)
	{
		window.clearTimeout (connectTimerId);
		connectTimerId = null;
		stop ();
		//var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		//alert ("Could not load Xml doc:\n" + msg);
		//validationError.innerText = errorMessage;
		setTimeout("window.location.reload (true)",5000);
	}				
}			

// handle onreadystatechange event of req object
function processReqChange () 
{				
	if (req.readyState == 4) 
	{
		// only if "OK"
		if (req.status == 200) 
		{   
			window.clearTimeout (connectTimerId);
			connectTimerId = null;       
			showDetail ();							
			req.abort ();
			if (typeof playlistItems == 'undefined')
	        {
		        stop ();
		        return;
	        }
			//update current request
			if (currentRequest < playlistItems-1)
			{							
				currentRequest ++;
			}
			else
			{
				needRefresh = true;
				currentRequest = 0;							
			}					
		} 
		else 
		{
			window.clearTimeout(connectTimerId);
			connectTimerId = null;
			stop ();
			//alert ("There was a problem retrieving the XML data:\n" + req.statusText);	
			//validationError.innerText = errorMessage;	
			setTimeout("window.location.reload (true)",5000);				
		}				
	}
}			

// display details retrieved from XML document
//Format of the xml doc as of 03.22.06
/*
<?xml version="1.0" encoding="utf-8" ?> 
<bulletinInfo>
<img src="filepath" width ="wvalue" height="hvalue"/> 
</bulletinInfo>
*/
// OR Format of the xml doc as of 04.27.06
/*
<?xml version="1.0" encoding="utf-8" ?> 
<bulletinInfo>
False
</bulletinInfo>
*/
function showDetail () 
{
	var item, content, div;
	try
	{
		item = req.responseXML.getElementsByTagName ("bulletinInfo")[0];					        
		content = item.childNodes[0].xml; 					
		if(content.toLowerCase () == 'false')//failed to fetch a new image
		{					
			return; //do not change image
		}      
		div = document.getElementById ("details"); 					    
		div.innerHTML = content;			
		document.getElementById ('controlsId').style.visibility = 'visible';				
	}  
	catch (e)
	{
		window.clearTimeout (connectTimerId);
		connectTimerId = null;
		stop ();
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		if (msg == "Object required")//session expired
		{						
			window.location.reload (true);
		}
		else
		{
			//alert ("Could not process request:\n" + msg);
			//validationError.innerText = errorMessage;
			setTimeout("window.location.reload (true)",5000);
		}
	}
}

function play ()
{
	if (typeof playlistItems == 'undefined')
	{
		stop ();
		setTimeout("window.location.reload (true)",5000);
		return;
	}
	if (!loadTimerId)
	{
		loadDoc ();//send first request immediately
		loadTimerId = window.setInterval ("loadDoc ()",pollInterval);
	}
}

function stop ()
{
	window.clearTimeout (loadTimerId);
	if (req)
	{
		if(callInProgress (req))
		{
			req.abort ();
		}
	}
	loadTimerId = null;
}

function prev ()
{		
	stop ();	
	if (typeof playlistItems == 'undefined')
	{		
	    setTimeout("window.location.reload (true)",5000);
		return;
	}				
	if (currentRequest == 0)
	{
		currentRequest = playlistItems - 2; //2 because it was already incremented from prev request
	}
	else
	{
		currentRequest -= 2;	
	}
	if (currentRequest < 0)
	{
		currentRequest = playlistItems-1;
	}				
	loadDoc ();							
}

function next ()
{
	stop ();
	loadDoc ();
}

function callInProgress (req) 
{
	switch (req.readyState) 
	{
		case 1, 2, 3:
			return (true);
		break;
		// Case 4 and 0
		default:
			return (false);
		break;
	}
}
function failToConnect ()
{
	stop ();
	window.clearTimeout (connectTimerId);
	connectTimerId = null;
	//alert ("Failed to connect.\nPlease make sure your AxisTV is running\nand try your request again.");
	//validationError.innerText = errorMessage;
	setTimeout("window.location.reload (true)",5000);
}
