// After the page first loads this javascript code takes over
// It manages everything from the parsing of updated XML to the switching of styles and images

// By default the lightStyle is used
var lightStyle=true;

// Initializes variables for detecting what browser is used
// Only IE and Firefox work 100%

// nodeVar is a variable that is used in the parsing of XML. IE and Firefox treat XML differently. IE counts only the opening tags of XML as nodes. Firefox counts both opening and closing tags as nodes.
var nodeVar = 0;
var ie = false;
var firefox = false;

// IE
if (window.ActiveXObject)
{
	ie = true;
	nodeVar = 2;
}
else if (document.implementation && document.implementation.createDocument)
{
	// firefox and Opera and others	
	firefox = true;
	nodeVar = 5;
}
else
{
	// Error message for unsupported browsers. This can be changed
	alert('Your browser cannot handle this script. Try Mozilla Firefox 2.0 or Microsoft Internet Explorer 6.0');
}

// In the HTML code there are two stylesheets set, a dark one and a light one.
// This toggles between them and also helps in the handling of the images
function changeSheets(whichSheet)
{
	whichSheet=whichSheet-1;
	if(document.styleSheets)
	{
		var c = document.styleSheets.length;
		
		if (lightStyle)
		{
			// change to dark style			
			changeDivImage("compassRoseBackground", "images/blank.gif");
			changeDivImage("compassRoseWind", "images/runwaysDark/blank.gif");
			changeDivImage("compassRoseRunway1", "images/runwaysDark/blank.gif");
			changeDivImage("compassRoseRunway2", "images/runwaysDark/blank.gif");
			changeDivImage("compassRoseRunway3", "images/runwaysDark/blank.gif");
			changeDivImage("compassRoseRunway4", "images/runwaysDark/blank.gif");
			changeDivImage("compassRoseRunway5", "images/runwaysDark/blank.gif");
			changeDivImage("compassRoseRunway6", "images/runwaysDark/blank.gif");
			
			document.styleSheets[0].disabled=true;
			document.styleSheets[1].disabled=false;
		
			changeDivImage("compassRoseBackground", "images/compassDark.jpg");
			lightStyle = false;
		}
		else
		{
			// change to light style
			changeDivImage("compassRoseBackground", "images/blank.gif");
			changeDivImage("compassRoseWind", "images/runwaysLight/blank.gif");
			changeDivImage("compassRoseRunway1", "images/runwaysLight/blank.gif");
			changeDivImage("compassRoseRunway2", "images/runwaysLight/blank.gif");
			changeDivImage("compassRoseRunway3", "images/runwaysLight/blank.gif");
			changeDivImage("compassRoseRunway4", "images/runwaysLight/blank.gif");
			changeDivImage("compassRoseRunway5", "images/runwaysLight/blank.gif");
			changeDivImage("compassRoseRunway6", "images/runwaysLight/blank.gif");
			
			document.styleSheets[1].disabled=true;
			document.styleSheets[0].disabled=false;
			
			changeDivImage("compassRoseBackground", "images/compassLight.jpg");
			lightStyle = true;
		}			
	}
	
	// Reload the XML
	loadXML();
}

// A quick function that takes a div's ID and an image as variables
// It sets the div's background to be the image passed
function changeDivImage(div, image)
{
	document.getElementById(div).style.backgroundImage = "url("+image+")";

}

// loadXML is called on page load
// It calls parseWeather and pasreWind asynchronously, passing them the locations of the XML files and the current time (in ms)
// as variables. The time is sent to ensure that each call is made to a different URL to help ensure that the browser does not just read its cached copy.
function loadXML()
{
	var url = 'xml/weatherdata.xml';		
	var pars = 'ms='+new Date().getTime();

	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: parseWeather
		});
	
	url = 'xml/winddata.xml';		
	pars = 'ms='+new Date().getTime();
	
	var windAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: parseWind
		});
		
	// Call loadXML again in 45 seconds
	var timer = setTimeout("loadXML()", 45000);
}


// parseWind parses the winddata.xml file
function parseWind(originalRequest)
{
	var xmlDoc = originalRequest.responseXML;
	
	// Grab only the data that is in the ROAList
	var showElements = xmlDoc.getElementsByTagName("ROAList");
	
	// ROAInt and ROAString are used in the findKey function that searches both of those to find a key
	// Grab only that data that are ROAInts
	var ROAInt = showElements[0].getElementsByTagName("ROAInt");
	
	// Grab only that data that are ROAStrings
	var ROAString = showElements[0].getElementsByTagName("ROAString");
	
	var node = null;
	var active = null;
	
	var runwayDirectory = null;
	var windDirectory = null;
	
	// check to see which style is currently active so that we can load the correct images
	if (lightStyle)
	{
		runwayDirectory = "runwaysLight";
		windDirectory = "windsLight";
	}
	else
	{
		runwayDirectory = "runwaysDark";
		windDirectory = "windsDark";
	}
	
	// Read in the runways. This page is setup to support a total of 6 runways
	node = findKey(ROAInt, ROAString, "RUNWAY_DIRECTIONS");
	if(node.childNodes[nodeVar].childNodes.length)
	{	
		// Parse the runways. They are semi-colon delimited
		var i = 0;
		var runways = node.childNodes[nodeVar].childNodes[0].nodeValue;
		var runwayArray = runways.split(";");
		
		// While there are runways display them
		while (i < runwayArray.length)
		{	
			if(runwayArray[i].length == 2)
				runwayArray[i] = "0" + runwayArray[i];
			changeDivImage("compassRoseRunway"+(i+1), "images/"+runwayDirectory+"/"+runwayArray[i]+".gif");
			i++;
		}
		
		// blank out the rest of the runways
		for (i; i < 6; i++)
		{
			changeDivImage("compassRoseRunway"+(i+1), "images/"+runwayDirectory+"/blank.gif");
		}
	}
	else
	{
		// blank out the runways
		for (var j=0; j < 6; j++)
		{
			changeDivImage("compassRoseRunway"+(j+1), "images/"+runwayDirectory+"/blank.gif");
		}
	
	}
	
	// Read in the wind direction
	node = findKey(ROAInt, ROAString, "WIND_DIR");
	if(node.childNodes[nodeVar].childNodes.length)	
	{
		var windDirection = node.childNodes[nodeVar].childNodes[0].nodeValue;
		if (windDirection == "000")
		{
			// calm, so set the image to calm
			changeDivImage("compassRoseWind", "images/"+windDirectory+"/calm.gif");
		}
		else
		{
			changeDivImage("compassRoseWind", "images/"+windDirectory+"/"+windDirection+".gif");
		}
	}
	
}

// parseWind parses the weatherdata.xml file
function parseWeather(originalRequest)
{
	var xmlDoc = originalRequest.responseXML;
	var showElements = xmlDoc.getElementsByTagName("ROAList");

	var ROAInt = showElements[0].getElementsByTagName("ROAInt");
	var ROAString = showElements[0].getElementsByTagName("ROAString");
	
	var node = null;
	var active = null;
	
	// Most of these follow the same format. Find the node. Check to see if there's data. If there's data display it. No error checking is done, whatsoever, other than that the XML file validates against the schema.
	node = findKey(ROAInt, ROAString, "AIRPORT_ELEVATION");
	if(node.childNodes[nodeVar].childNodes.length)
		$('AIRPORT_ELEVATION').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
	
	node = findKey(ROAInt, ROAString, "AIRPORT_IDENTIFIER");
	if(node.childNodes[nodeVar].childNodes.length)
		$('AIRPORT_IDENTIFIER').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;

	node = findKey(ROAInt, ROAString, "AIRPORT_NAME");
	if(node.childNodes[nodeVar].childNodes.length)
	{
		var airportName = node.childNodes[nodeVar].childNodes[0].nodeValue;
		if (airportName.length > 40)
		{
		    // set the airportname's style 		    
		    document.getElementById('airportNameTextWrapper').style.fontSize = "14px";		    
		}
		else if (airportName.length > 30)
		{
		    // set the airportname's style 		    
		    document.getElementById('airportNameTextWrapper').style.fontSize = "18px";		    
		}		
		else
			document.getElementById('airportNameTextWrapper').style.fontSize = "22px";		    		

		$('AIRPORT_NAME').innerHTML = airportName;
	}
	
	node = findKey(ROAInt, ROAString, "ALTIMETER");
	if(node.childNodes[nodeVar].childNodes.length)
		$('ALTIMETER').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
	
	node = findKey(ROAInt, ROAString, "AWOS_DATE");
	if(node.childNodes[nodeVar].childNodes.length)
		$('AWOS_DATE').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;

	node = findKey(ROAInt, ROAString, "AWOS_TIME");
	if(node.childNodes[nodeVar].childNodes.length)
		$('AWOS_TIME').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;

	node = findKey(ROAInt, ROAString, "CEILING");
	if(node.childNodes[nodeVar].childNodes.length)
		$('CEILING').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
				
	node = findKey(ROAInt, ROAString, "DENSITY_ALT");
	if(node.childNodes[nodeVar].childNodes.length)
		$('DENSITY_ALT').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;		
	else
		$('DENSITY_ALT').innerHTML = "";

	node = findKey(ROAInt, ROAString, "DEWPOINT");
	if(node.childNodes[nodeVar].childNodes.length)
		$('DEWPOINT').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue.replace(" ","&nbsp;");

	node = findKey(ROAInt, ROAString, "METAR");
	if(node.childNodes[nodeVar].childNodes.length)
	{
		var metar = node.childNodes[nodeVar].childNodes[0].nodeValue;
		// set the METAR font if line is too long
		if (metar.length > 100)
		{			
			document.getElementById('METAR').style.fontSize = "10px";	
		}
		else if (metar.length > 80)
		{
			document.getElementById('METAR').style.fontSize = "14px";		    			
		}
		else
			document.getElementById('METAR').style.fontSize = "18px";			
	
		$('METAR').innerHTML = metar
	}
	
	// if not active grey out - not active when AWOS not equipeed with sensor
	active = findKey(ROAInt, ROAString, "PRESENT_WX.ACTIVE");
	if (active.childNodes[nodeVar].childNodes[0].nodeValue == 1)
	{		
		$('presentLabel').className = "bottomDataLabel";
		$('presentWrapper').className = "bottomDataWrapper";		
	}
	else
	{
		$('presentLabel').className = "bottomDataLabelDisabled";
		$('presentWrapper').className = "bottomDataWrapperDisabled";		
	}

	node = findKey(ROAInt, ROAString, "PRESENT_WX");
	if(node.childNodes[nodeVar].childNodes.length)
		$('PRESENT_WX').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
	else
		$('PRESENT_WX').innerHTML = "";
	
	
	// We don't want to update the remarks - we want to keep the disclaimer
	//node = findKey(ROAInt, ROAString, "REMARKS");
	//if(node.childNodes[nodeVar].childNodes.length)
		//$('REMARKS').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;

	node = findKey(ROAInt, ROAString, "RUNWAY_SURFACE");
	if(node.childNodes[nodeVar].childNodes.length)
		$('RUNWAY_SURFACE').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
		
	active = findKey(ROAInt, ROAString, "RUNWAY_SURFACE.ACTIVE");
	if (active.childNodes[nodeVar].childNodes[0].nodeValue == 1)
	{		
		$('runwayLabel').className = "sensorDataLabel";
		$('runwayWrapper').className = "sensorDataWrapper";		
	}
	else
	{
		$('runwayLabel').className = "sensorDataLabelDisabled";
		$('runwayWrapper').className = "sensorDataWrapperDisabled";		
	}
		
	node = findKey(ROAInt, ROAString, "RUNWAY_SURFACE");
	if(node.childNodes[nodeVar].childNodes.length)	
		$('RUNWAY_SURFACE').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
	else
		$('RUNWAY_SURFACE').innerHTML = "";
	
	node = findKey(ROAInt, ROAString, "TEMPERATURE");
	if(node.childNodes[nodeVar].childNodes.length)
		$('TEMPERATURE').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue.replace(" ","&nbsp;");
		
	active = findKey(ROAInt, ROAString, "THUNDER_TEXT.ACTIVE");
	if (active.childNodes[nodeVar].childNodes[0].nodeValue == 1)
	{		
		$('lightningLabel').className = "bottomDataLabel";
		$('lightningWrapper').className = "bottomDataWrapper";		
	}
	else
	{
		$('lightningLabel').className = "bottomDataLabelDisabled";
		$('lightningWrapper').className = "bottomDataWrapperDisabled";		
	}
	
	node = findKey(ROAInt, ROAString, "THUNDER_TEXT");
	if(node.childNodes[nodeVar].childNodes.length)	
		$('THUNDER_TEXT').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
	else
		$('THUNDER_TEXT').innerHTML = "";
	
	node = findKey(ROAInt, ROAString, "VISIBILITY");
	if(node.childNodes[nodeVar].childNodes.length)
		$('VISIBILITY').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
	
	node = findKey(ROAInt, ROAString, "WIND");
	if(node.childNodes[nodeVar].childNodes.length)
		$('WIND').innerHTML = node.childNodes[nodeVar].childNodes[0].nodeValue;
	
}

// This function takes in two XML nodes, ROAInt and ROAString, and searches them for a node with the node name of ley.
function findKey(ROAInt, ROAString, key)
{
	var node = null; 
	
	var foundInROAInt = false;
	var foundInROAString = false;
	var str = null;
	
	// Again, ie and Firefox like to reference things differently to keep us on our toes!
	if (ie)
	{
		// set key node 
		keyNumber = 0;
	}
	else if (firefox)
	{
		// set key node 
		keyNumber = 1;
	}
	
	// Search through the ROAInts
	for (var x=0; x<ROAInt.length; x++)
	{
		if (ROAInt[x].hasChildNodes() && ROAInt[x].childNodes[keyNumber].hasChildNodes())
		{
			if (key == ROAInt[x].childNodes[keyNumber].childNodes[0].nodeValue)
			{
				foundInROAInt = true;
				node = ROAInt[x];
				return node;
				break;
			}
		}
	}
	
	// It has not be found in ROAInt, so continue searching in ROAString
	if (!foundInROAInt)
	{
		for (var y=0; y<ROAString.length; y++)
		{
			if (ROAString[y].hasChildNodes() && ROAString[y].childNodes[keyNumber].hasChildNodes()) 
			{
				if (key == ROAString[y].childNodes[keyNumber].childNodes[0].nodeValue)
				{
					foundInROAString = true;
					node = ROAString[y];
					return node;
					break;
				}
			}
		}
	}
	
	return node;
}


