// setup variables for map
var FileName = "http://www.vafinancials.com/maps/ndata.php";		// xml data file
var ImagesLocation = "http://www.vafinancials.com/maps/images/";	// images directory
var RedImgs;														
var UPDATERATE = 30; 												//update rate set to 30 seconds
var c = UPDATERATE-1;
var bgclr = "";
var barclr = "";
var circlr = "";
var cirrad = "";
var dcir = 0;
var atccir = "";
var x;
var xm;
var map;
var allrows = "";
var allcrows = "";
var ctable = "";
var map = "";

//setup arrays for marker data
var prow = [];
var crow = [];
var gmarks = [];
var phtml = [];
var vapres = [];
var csigns = [];
var acids = [];
var dicaos = [];
var dlats = [];
var dlons = [];
var aicaos = [];
var alats = [];
var alons = [];
var hdgs = [];
var gspds = [];
var altims = [];
var fperc = [];
var status = [];
var lats = [];
var lons = [];
var cids = [];
var freqs = [];
var servs = [];
var rats = [];
var tnpds = [];
var facs = [];
var ptyps = [];
var rmks = [];
var rtes = [];
var ctyps = [];

var i = 0;
var xi = 0;
var rclr = "bdat";

var displayedMarkers = new Array();

//timer to refresh marker data set by UPDATERATE
function timedCount() {
	c = c + 1;
	if(c == (UPDATERATE - 1)){
		window.document.getElementById("nupdate").innerHTML =  "LOADING MAP DATA.........." ;
	}
	if(c == UPDATERATE) {
		loadMapData(FileName);
		c = 0;
	};
	x = UPDATERATE-c;
	window.document.getElementById("nupdate").innerHTML =  "MAP WILL REFRESH IN " + String(x) + " SECONDS" ;
	t=setTimeout("timedCount()",1000);
};

//removeMarkers :: removes the map overlays except the weather overlay
function removeMarkers(map, arrayName)
{
	// Get the map data
	try {
		var markers = eval(arrayName);
		if (!markers) return false;
	} catch (err) {
		return false;
	}
		
	// Remove the map data, either an array or a single element
	if (isNaN(markers.length))
		map.removeOverlay(markers);
	else if (markers.length > 0) {
		for (var x = 0; x < markers.length; x++)
			map.removeOverlay(markers[x]);
	}
			
	displayedMarkers[arrayName] = false;
	return true;
}

// loadMap :: loads the map and sets up the controls
function loadMap(){
	if (GBrowserIsCompatible()) {        
    	map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
    	map.setCenter(new GLatLng(0.000000, 0.000000), 2);
    	var sat = map.getMapTypes()[1]; 
		map.setMapType(sat);
    	map.enableDoubleClickZoom();
		map.enableContinuousZoom();
    	timedCount();
    }
    window.document.getElementById("ctab").style.visbility = "hidden";
}

// createMarker :: creates all the markers and html for the pop up windows
function createMarker(point, index, pophtml, hdg, va, cs, ac, de, ar, gs, al, pf, st, plat, plon, cid, fre, ser, rat, tpn, fac, ptp, rmk, rte, typ) {
	var icon = new GIcon();
	var nhdg = Number(hdg);
	
	if(ser == "na")
	{	
		RedImgs = "orange/";
	}else{
		RedImgs = "green/";
	}
	
	if(typ == "ATC"){
		icon.image = ImagesLocation + "radar.png";
		icon.iconSize = new GSize(15, 15);
		icon.iconAnchor = new GPoint(7, 7);
		icon.infoWindowAnchor = new GPoint(7, 12);
	}else if(typ == "GEN"){
		icon.image = ImagesLocation + "generic.png";
		icon.iconSize = new GSize(9, 9);
		icon.iconAnchor = new GPoint(4, 4);
		icon.infoWindowAnchor = new GPoint(4, 7);
	}else{
		if(nhdg >= 355){
			icon.image = ImagesLocation + RedImgs + "355359.png";
		}else if(nhdg >= 350){
			icon.image = ImagesLocation + RedImgs + "350354.png";
		}else if(nhdg >= 345){
			icon.image = ImagesLocation + RedImgs + "345349.png";
		}else if(nhdg >= 340){
			icon.image = ImagesLocation + RedImgs + "340344.png";
		}else if(nhdg >= 335){
			icon.image = ImagesLocation + RedImgs + "335339.png";
		}else if(nhdg >= 330){
			icon.image = ImagesLocation + RedImgs + "330334.png";
		}else if(nhdg >= 325){
			icon.image = ImagesLocation + RedImgs + "325329.png";
		}else if(nhdg >= 320){
			icon.image = ImagesLocation + RedImgs + "320324.png";
		}else if(nhdg >= 315){
			icon.image = ImagesLocation + RedImgs + "315319.png";
		}else if(nhdg >= 310){
			icon.image = ImagesLocation + RedImgs + "310314.png";
		}else if(nhdg >= 305){
			icon.image = ImagesLocation + RedImgs + "305309.png";
		}else if(nhdg >= 300){
			icon.image = ImagesLocation + RedImgs + "300304.png";
		}else if(nhdg >= 295){
			icon.image = ImagesLocation + RedImgs + "295299.png";
		}else if(nhdg >= 290){
			icon.image = ImagesLocation + RedImgs + "290294.png";
		}else if(nhdg >= 285){
			icon.image = ImagesLocation + RedImgs + "285289.png";
		}else if(nhdg >= 280){
			icon.image = ImagesLocation + RedImgs + "280284.png";
		}else if(nhdg >= 275){
			icon.image = ImagesLocation + RedImgs + "275279.png";
		}else if(nhdg >= 270){
			icon.image = ImagesLocation + RedImgs + "270274.png";
		}else if(nhdg >= 265){
			icon.image = ImagesLocation + RedImgs + "265269.png";
		}else if(nhdg >= 260){
			icon.image = ImagesLocation + RedImgs + "260264.png";
		}else if(nhdg >= 255){
			icon.image = ImagesLocation + RedImgs + "255259.png";
		}else if(nhdg >= 250){
			icon.image = ImagesLocation + RedImgs + "250254.png";
		}else if(nhdg >= 245){
			icon.image = ImagesLocation + RedImgs + "245249.png";
		}else if(nhdg >= 240){
			icon.image = ImagesLocation + RedImgs + "240244.png";
		}else if(nhdg >= 235){
			icon.image = ImagesLocation + RedImgs + "235239.png";
		}else if(nhdg >= 230){
			icon.image = ImagesLocation + RedImgs + "230234.png";
		}else if(nhdg >= 225){
			icon.image = ImagesLocation + RedImgs + "225229.png";
		}else if(nhdg >= 220){
			icon.image = ImagesLocation + RedImgs + "220224.png";
		}else if(nhdg >= 215){
			icon.image = ImagesLocation + RedImgs + "215219.png";
		}else if(nhdg >= 210){
			icon.image = ImagesLocation + RedImgs + "210214.png";
		}else if(nhdg >= 205){
			icon.image = ImagesLocation + RedImgs + "205209.png";
		}else if(nhdg >= 200){
			icon.image = ImagesLocation + RedImgs + "200204.png";
		}else if(nhdg >= 195){
			icon.image = ImagesLocation + RedImgs + "195199.png";
		}else if(nhdg >= 190){
			icon.image = ImagesLocation + RedImgs + "190194.png";
		}else if(nhdg >= 185){
			icon.image = ImagesLocation + RedImgs + "185189.png";
		}else if(nhdg >= 180){
			icon.image = ImagesLocation + RedImgs + "180184.png";
		}else if(nhdg >= 175){
			icon.image = ImagesLocation + RedImgs + "175179.png";
		}else if(nhdg >= 170){
			icon.image = ImagesLocation + RedImgs + "170174.png";
		}else if(nhdg >= 165){
			icon.image = ImagesLocation + RedImgs + "165169.png";
		}else if(nhdg >= 160){
			icon.image = ImagesLocation + RedImgs + "160164.png";
		}else if(nhdg >= 155){
			icon.image = ImagesLocation + RedImgs + "155159.png";
		}else if(nhdg >= 150){
			icon.image = ImagesLocation + RedImgs + "150154.png";
		}else if(nhdg >= 145){
			icon.image = ImagesLocation + RedImgs + "145149.png";
		}else if(nhdg >= 140){
			icon.image = ImagesLocation + RedImgs + "140144.png";
		}else if(nhdg >= 135){
			icon.image = ImagesLocation + RedImgs + "135139.png";
		}else if(nhdg >= 130){
			icon.image = ImagesLocation + RedImgs + "130134.png";
		}else if(nhdg >= 125){
			icon.image = ImagesLocation + RedImgs + "125129.png";
		}else if(nhdg >= 120){
			icon.image = ImagesLocation + RedImgs + "120124.png";
		}else if(nhdg >= 115){
			icon.image = ImagesLocation + RedImgs + "115119.png";
		}else if(nhdg >= 110){
			icon.image = ImagesLocation + RedImgs + "110114.png";
		}else if(nhdg >= 105){
			icon.image = ImagesLocation + RedImgs + "105109.png";
		}else if(nhdg >= 100){
			icon.image = ImagesLocation + RedImgs + "100104.png";
		}else if(nhdg >= 95){
			icon.image = ImagesLocation + RedImgs + "095099.png";
		}else if(nhdg >= 90){
			icon.image = ImagesLocation + RedImgs + "090094.png";
		}else if(nhdg >= 85){
			icon.image = ImagesLocation + RedImgs + "085089.png";
		}else if(nhdg >= 80){
			icon.image = ImagesLocation + RedImgs + "080084.png";
		}else if(nhdg >= 75){
			icon.image = ImagesLocation + RedImgs + "075079.png";
		}else if(nhdg >= 70){
			icon.image = ImagesLocation + RedImgs + "070074.png";
		}else if(nhdg >= 65){
			icon.image = ImagesLocation + RedImgs + "065069.png";
		}else if(nhdg >= 60){
			icon.image = ImagesLocation + RedImgs + "060064.png";
		}else if(nhdg >= 55){
			icon.image = ImagesLocation + RedImgs + "055059.png";
		}else if(nhdg >= 50){
			icon.image = ImagesLocation + RedImgs + "050054.png";
		}else if(nhdg >= 45){
			icon.image = ImagesLocation + RedImgs + "045049.png";
		}else if(nhdg >= 40){
			icon.image = ImagesLocation + RedImgs + "040044.png";
		}else if(nhdg >= 35){
			icon.image = ImagesLocation + RedImgs + "035039.png";
		}else if(nhdg >= 30){
			icon.image = ImagesLocation + RedImgs + "030034.png";
		}else if(nhdg >= 25){
			icon.image = ImagesLocation + RedImgs + "025029.png";
		}else if(nhdg >= 20){
			icon.image = ImagesLocation + RedImgs + "020024.png";
		}else if(nhdg >= 15){
			icon.image = ImagesLocation + RedImgs + "015019.png";
		}else if(nhdg >= 10){
			icon.image = ImagesLocation + RedImgs + "010014.png";
		}else if(nhdg >= 5){
			icon.image = ImagesLocation + RedImgs + "005009.png";
		}else if(nhdg >= 0){
			icon.image = ImagesLocation + RedImgs + "000004.png";
		}
		
		icon.iconSize = new GSize(19, 19);
		icon.iconAnchor = new GPoint(9, 9);
		icon.infoWindowAnchor = new GPoint(9, 16);
	}
		
	var marker = new GMarker(point, icon);
		
	GEvent.addListener(marker, "click", function() { 
		marker.openInfoWindowHtml(pophtml);
	});

	gmarks[xm] = marker;
	phtmls[xm] = pophtml;
	vapres[xm] = va;
	csigns[xm] = cs;
	acids[xm] = ac;
	dicaos[xm] = de;
	aicaos[xm] = ar;
	hdgs[xm] = nhdg;
	gspds[xm] = gs;
	altims[xm] = al;
	fperc[xm] = pf;
	status[xm] = st;		
	lats[xm] = plat;
	lons[xm] = plon;
	cids[xm] = cid;
	freqs[xm] = fre;
	servs[xm] = ser;
	rats[xm] = rat;
	tnpds[xm] = tpn;
	facs[xm] = fac;
	ptyps[xm] = ptp;
	rmks[xm] = rmk;
	rtes[xm] = rte;
	ctyps[xm] = typ;
	displayedMarkers[xm] = marker;
		
	xm++;
	return marker;
}

function resetAllVars(){
	prow = [];
	gmarks = [];
	phtmls = [];
	vpres = [];
	csigns = [];
	acids = [];
	dicaos = [];
	aicaos = [];
	hdgs = [];
	gspds = [];
	altims = [];
	fperc = [];
	status = [];
	lats = [];
	lons = [];
	cids = [];
	freqs = [];
	servs = [];
	rats = [];
	tnpds = [];
	facs = [];
	ptyps = [];
	rmks = [];
	rtes = [];
	ctyps = [];

	x = 0;
	xm = 0;
	i = 0;
}

// tclick :: This function picks up the click and opens the corresponding info window and centers the map on the marker
function tclick(i) {
	gmarks[i].openInfoWindowHtml(phtmls[i]);
	map.setCenter(new GLatLng(lats[i], lons[i]));
};

// loadMapData :: refresh the markers and data for the map and table
function loadMapData(fName){

	removeMarkers(map, 'displayedMarkers');
	resetAllVars();
		
	GDownloadUrl(fName, function(data, responseCode) {  
	var xml = GXml.parse(data);  
	var markers = xml.documentElement.getElementsByTagName("marker");
			
	//sets up the markers and the table rows 
	for (var i = 0; i < markers.length; i++) {    
		var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lon")));
		var nwkimg = "";
				
		if(markers[i].getAttribute("sta") == "Climbing"){
			bgclr = "#FADADA";
			barclr = "#D43C3C";
		}else if(markers[i].getAttribute("sta") == "Cruising"){
			bgclr = "#FFEED5";
			barclr = "#CC7A00";
		}else if(markers[i].getAttribute("sta") == "Decending"){
			bgclr = "#C2F0C2";
			barclr = "#009600";
		}else{
			bgclr = "#FFFFFF";
			barclr = "#333333";
		}
		
		if(markers[i].getAttribute("ser") != "na"){
			nwkimg = "<img border='0' src='images/network.png' width='180' height='28'>";
		}
		
		if(markers[i].getAttribute("typ") == "ATC"){
			pophtml = "<table border='0' width='300' id='table5' cellspacing='1' cellpadding='0' bgcolor='#000000'>";
			pophtml += "<tr><td colspan='2' class='lbl'><img border='0' src='images/controller.png' width='120' height='28'></td></tr>";
			pophtml += "<tr><td class='lbl'>STATION:</td><td class='pdat'>" + markers[i].getAttribute("cs") + "</td></tr>";
			pophtml += "<tr><td class='lbl'>NAME:</td><td class='pdat'>" + markers[i].getAttribute("cid") + "</td></tr>";
			pophtml += "<tr><td class='lbl'>FACILITY:</td><td class='pdat'>" + markers[i].getAttribute("fac") + "</td></tr>";
			pophtml += "<tr><td class='lbl'>FREQUENCY:</td><td class='pdat'>" + markers[i].getAttribute("fre") + "</td></tr>";
			pophtml += "<tr><td class='lbl'>LEVEL:</td><td class='pdat'>" + markers[i].getAttribute("rat") + "</td></tr>";
			pophtml += "</table>";
		}else{		
			pophtml = "<table border='0' width='300' id='table4' cellspacing='1' cellpadding='0' style='border: 1px solid #000000' bgcolor='#000000'>";
			pophtml += "<tr><td colspan='4' style='text-align: left' bgcolor='#C0C0C0'><img border='0' src='images/" + markers[i].getAttribute("vai") + ".png' width='120' height='28'>" + nwkimg + "</td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>pilot:</td><td class='pdat' width='75'>" + markers[i].getAttribute("cs") + "</td>";
			pophtml += "<td class='lbl' width='75'>hdg:</td><td class='pdat' width='75'>" + markers[i].getAttribute("hdg") + "</td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>aircraft:</td><td class='pdat' width='75'>" + markers[i].getAttribute("acid") + "</td>";
			pophtml += "<td class='lbl' width='75'>altitude:</td><td class='pdat' width='75'>" + markers[i].getAttribute("alt") + "</td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>depart:</td><td class='pdat' width='75'>" + markers[i].getAttribute("deicao") + "</td>";
			pophtml += "<td class='lbl' width='75'>ground spd:</td><td class='pdat' width='75'>" + markers[i].getAttribute("gs") + "</td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>arrive:</td><td class='pdat' width='75'>" + markers[i].getAttribute("aricao") + "</td>";
			pophtml += "<td class='lbl' width='75'>transponder:</td><td class='pdat' width='75'>" + markers[i].getAttribute("tpn") + "</td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>server:</td><td class='pdat' width='75'>" + markers[i].getAttribute("ser") + "</td>";
			pophtml += "<td class='lbl' width='75'>frequency:</td><td class='pdat' width='75'>" + markers[i].getAttribute("fre") + "</td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>flight status:</td><td class='pdat' colspan='3'><div align='center'>";
			pophtml += "<table border='0' width='100%' id='table5' cellspacing='1' cellpadding='0'><tr><td class='psta' width='50%'>" + markers[i].getAttribute("sta") + "</td>";
			pophtml += "<td class='psta' bgcolor='#C0C0C0'><div align='left'>";
			pophtml += "<table border='0' width='" + markers[i].getAttribute("per") + "%' id='table6' cellspacing='0' cellpadding='0' bgcolor='" + bgclr + "'><tr><td class='psta'>" + markers[i].getAttribute("per") + "%</td>";
			pophtml += "</tr></table></div></td></tr></table></div></td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>route:</td><td class='pdat' colspan='3'>" + markers[i].getAttribute("rte") + "</td></tr>";
			pophtml += "<tr><td class='lbl' width='75'>remarks:</td><td class='pdat' colspan='3'>" + markers[i].getAttribute("rmk") + "</td></tr></table>";
		}
				
		var marker = createMarker(
			point, 
			i, 
			pophtml,
			markers[i].getAttribute("hdg"),
			markers[i].getAttribute("va"), 
			markers[i].getAttribute("cs"), 
			markers[i].getAttribute("acid"), 
			markers[i].getAttribute("deicao"),
			markers[i].getAttribute("aricao"), 
			markers[i].getAttribute("gs"), 
			markers[i].getAttribute("alt"),
			markers[i].getAttribute("per"),
			markers[i].getAttribute("sta"),
			markers[i].getAttribute("lat"),
			markers[i].getAttribute("lon"),
			markers[i].getAttribute("cid"),
			markers[i].getAttribute("fre"),
			markers[i].getAttribute("ser"),
			markers[i].getAttribute("rat"),
			markers[i].getAttribute("tpn"),
			markers[i].getAttribute("fac"),
			markers[i].getAttribute("ptp"),
			markers[i].getAttribute("rmk"),
			markers[i].getAttribute("rte"),
			markers[i].getAttribute("typ")
		);
				
		map.addOverlay(marker);
		
		if(markers[i].getAttribute("fac") == "CLR"){
			circlr = "#33CCFF";
			cirrad = 5;
		}else if(markers[i].getAttribute("fac") == "TWR"){
			circlr = "#CC99FF";
			cirrad = 30;
		}else if(markers[i].getAttribute("fac") == "APP"){
			circlr = "#FFCC99";
			cirrad = 80;
		}else if(markers[i].getAttribute("fac") == "CTR"){
			circlr = "#33CC33";
			cirrad = 400;
		}
		
		if(rclr == "bdat"){
			rclr = "adat";
		}else{
			rclr = "bdat";
		}
				
		if(markers[i].getAttribute("typ") == "ATC"){		
			crow[i] = "<tr class='" + rclr + "' onmouseover=\"this.className='odat'\" onmouseout=\"this.className='" + rclr + "'\" onclick='tclick(" + i + ")'>";
			crow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("cs") + "</td>";
			crow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("fac") + "</td>";
			crow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("fre") + "</td>";
			crow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("rat") + "</td>";
			
			allcrows += crow[i];
		}else{		
			prow[i] = "<tr class='" + rclr + "' onmouseover=\"this.className='odat'\" onmouseout=\"this.className='" + rclr + "'\" onclick='tclick(" + i + ")'>";
			prow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("va") + "</td>";
			prow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("cs") + "</td>";
			prow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("acid") + "</td>";
			prow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("deicao") + "</td>";
			prow[i] += "<td class='rdat' align='left'>" + markers[i].getAttribute("aricao") + "</td>";
			prow[i] += "<td class='rdat' align='right'>" + markers[i].getAttribute("hdg") + "</td>";
			prow[i] += "<td class='rdat' align='right'>" + markers[i].getAttribute("gs") + "</td>";
			prow[i] += "<td class='rdat' align='right'>" + markers[i].getAttribute("alt") + "</td>";
			prow[i] += "<td class='rdat' align='right'>" + markers[i].getAttribute("sta") + "</td>";
			prow[i] += "</tr>";
		
			allrows += prow[i];
		}
	}
		
	var ctable = "<table border='0' width='100%' id='table2' cellspacing='0' cellpadding='0'>";
	ctable += "<tr><td colspan='4' class='chdr'>controller information</td></tr>";
	ctable += "<tr><td class='dhdr'>station</td><td class='dhdr'>facility</td><td class='dhdr'>frequency</td><td class='dhdr'>rating</td>";

	var ptable = "<table border='0' width='100%' id='table2' cellspacing='0' cellpadding='0'>";
	ptable += "<tr><td colspan='9' class='chdr'>pilot information</td></tr>";
	ptable += "<tr><td class='dhdr'>va</td><td class='dhdr'>callsign</td><td class='dhdr'>ac</td><td class='dhdr'>depart</td>";
	ptable += "<td class='dhdr'>arrive</td><td class='dhdr'>hdg</td><td class='dhdr'>gs</td><td class='dhdr'>alt</td><td class='dhdr'>status</td></tr>";
			
	window.document.getElementById("ptab").innerHTML = ptable + allrows + "</table>";
	window.document.getElementById("ctab").innerHTML = ctable + allcrows + "</table>";

	ptable = "";
	allrows = "";
	ctable = "";
	allcrows = "";
			
	});

}

function hideTab(wtd) {
	if(wtd == 0){
		window.document.getElementById("ctab").style.visibility = "hidden";
		window.document.getElementById("ptab").style.visibility = "visible";
	}else{
		window.document.getElementById("ctab").style.visibility = "visible";
		window.document.getElementById("ptab").style.visibility = "hidden";
	}
}