﻿var ADIx = 0;
var displayAirportDelaysIndexIntervalID;

function displayAirportDelaysIndex()
{		
     if (window.XMLHttpRequest) { // Mozilla, Safari, ...
         airportDelaysIndexXML = new XMLHttpRequest();
     }else if (window.ActiveXObject) { // IE
         airportDelaysIndexXML = new ActiveXObject("Microsoft.XMLHTTP");
     }
      
	 airportDelaysIndexXML.onreadystatechange = function()
	 {
        if(airportDelaysIndexXML.readyState == 4)
        {
				airportDelaysIndexXMLData = airportDelaysIndexXML.responseXML.getElementsByTagName('airport');

				scrollAirportDelaysIndex("");
			 
				if (airportDelaysIndexXMLData.length > 1)
					displayAirportDelaysIndexIntervalID = setInterval("scrollAirportDelaysIndex('')", 5000); // 5 seconds between tickets
		}
	 }

     airportDelaysIndexXML.open("GET", XMLFilePath + '?t=' + new Date().getTime());
     airportDelaysIndexXML.send(null);
    
     setTimeout("displayAirportDelaysIndexUpdate()", 300000); // 5 minutes between updates
}

function scrollAirportDelaysIndex(update){
	
	setTimeout("setOpacity(9)", 0);
	setTimeout("setOpacity(8)", 25);
	setTimeout("setOpacity(7)", 50);
	setTimeout("setOpacity(6)", 75);
	setTimeout("setOpacity(5)", 100);
	setTimeout("setOpacity(4)", 125);
	setTimeout("setOpacity(3)", 150);
	setTimeout("setOpacity(2)", 175);
	setTimeout("setOpacity(1)", 200);
	setTimeout("setOpacity(0)", 225);
		
	setTimeout("scrollAirportDelaysIndexSwap('" + update + "')", 230);

	setTimeout("setOpacity(1)", 250);
	setTimeout("setOpacity(2)", 275);
	setTimeout("setOpacity(3)", 300);
	setTimeout("setOpacity(4)", 325);
	setTimeout("setOpacity(5)", 350);
	setTimeout("setOpacity(6)", 375);
	setTimeout("setOpacity(7)", 400);
	setTimeout("setOpacity(8)", 425);
	setTimeout("setOpacity(9)", 450);
	setTimeout("setOpacity(10)", 475);
}

function setOpacity(value)
{		
	document.getElementById("airportDelaysContent").style.opacity = value/10;
	document.getElementById("airportDelaysContent").style.filter = "alpha(opacity=" + (value * 10) + ")";
}

function scrollAirportDelaysIndexSwap(update)
{
	aDXMLDoc = airportDelaysIndexXMLData;
	
	if (ADIx >= aDXMLDoc.length)
		ADIx = 0;
	
	if (aDXMLDoc.length == 1)
		counter = "";
	else
		counter = "[" + (ADIx + 1) + " of " + aDXMLDoc.length + "]";

	if (update != "")
	{
		innerHTMLData = update;
		document.getElementById("airportDelaysCounter").innerHTML = "";
	}
	else
	{
    	if (document.getElementById("airportDelaysContent").textContent) { // Mozilla, Safari, ...
    		innerHTMLData = aDXMLDoc[ADIx].textContent;
    	} else {  // IE
    		innerHTMLData = aDXMLDoc[ADIx].text;
    	}
    	ADIx++;
    	
    	if (TicketURL != "" && innerHTMLData.indexOf("-Error") == -1)
    		innerHTMLData = "<a href='" + TicketURL + "'>" + innerHTMLData + "</a>";

		innerHTMLData = innerHTMLData.replace("-Error","");
    	
    	document.getElementById("airportDelaysCounter").innerHTML = counter;		
	}
	
	document.getElementById("airportDelaysContent").innerHTML = innerHTMLData;
}

function displayAirportDelaysIndexUpdate()
{
	clearInterval(displayAirportDelaysIndexIntervalID);
	ADIx = 0;
	scrollAirportDelaysIndex("<b>Updating...</b>");
	setTimeout("displayAirportDelaysIndex()", 2000);
}