// JavaScript Document
// DESENVOLVED BY VAFINANCIALS MODS BY HÍGOR FONSECA

//setup all vars for data processing
	var FileName = "markers.php";
	var ImagesLocation = "http://www.golvirtual.org/images/";
	var RedImgs;
	var UPDATERATE = 90; //update rate set to 30 seconds
	var c = UPDATERATE-1;
	var bgclr = "";
	var barclr = "";
	var rcls = "rowb";
	var x;
	var xm;
	var map;
	var allrows = "";
	var img = "";
	//var ptable = "";
	var prow = [];
	var gmarks = [];
	var phtmls = [];
	var vpres = [];
	var imgs = [];
	var csigns = [];
	var tails = [];
	var acids = [];
	var aclds = [];
	var flnms = [];
	var deics = [];
	var arics = [];
	var souls = [];
	var cargs = [];
	var ftims = [];
	var lats = [];
	var lons = [];
	var hdgs = [];
	var attds = [];
	var iasps = [];
	var tasps = [];
	var gspds = [];
	var machs = [];
	var emils = [];
	var mflws = [];
	var status = [];
	var i = 0;
	var xi = 0;
	var displayedMarkers = new Array();
	
	//timer to refresh marker data
	function timedCount() {
		c = c + 1;
		if(c == UPDATERATE) {
			loadMapData(FileName);
			c = 0;
		};
		x = UPDATERATE-c;
	   	window.document.getElementById("nupdate").innerHTML =  "ATUALIZAÇÃO EM " + String(x) + " SEGUNDOS." ;
	   	t=setTimeout("timedCount()",1000);
	};
	
	//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;
	}
	
	//loads the map and sets up the controls
	function loadMap(){
		if (GBrowserIsCompatible()) {        
    		map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(-16.88, -54.22), 4);
			map.addControl(new GMapTypeControl());
			map.setUIToDefault();
			map.enableDoubleClickZoom();
			map.enableContinuousZoom();
			map.setUIToDefault();
			map.setMapType(G_PHYSICAL_MAP);
    		timedCount();
			
			/*
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
    		map.setCenter(new GLatLng(-16.88, -54.22), 4);
    		map.enableDoubleClickZoom();
			map.enableContinuousZoom();
			map.setUIToDefault();
			map.setMapType(G_HYBRID_MAP);
    		timedCount();
			*/
    	}
    }
	
	//creates all the markers and html for the pop up windows
	function createMarker(point, index, pophtml, vp, im, cs, tn, ac, ar, fn, di, ai, so, ca, ft, lt, ln, hd, al, ia, ta, gs, ma, em, mf, st) {
		var icon = new GIcon();
		var nhdg = Number(hd);
		var myalt = Number(al);
		
		//decide which color to use for the icon
		//if(st == "Climbing"){
		//	RedImgs = "red/";
		//}else if(st == "Decending"){
		//	RedImgs = "green/";
		//}else if(st == "Cruising"){
		//	RedImgs = "orange/";
		//}else{
		//	RedImgs = "black/";
		//}
		
		RedImgs = "map/";
		//decide which pic to use for the icon
		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);
			drawroute(markers[i].getAttribute("dt"),markers[i].getAttribute("dn"),markers[i].getAttribute("lt"),markers[i].getAttribute("ln"),markers[i].getAttribute("at"),markers[i].getAttribute("an"));
		});

		gmarks[xm] = marker;
		phtmls[xm] = pophtml;
		vpres[xm] = vp;
		imgs[xm] = im;
		csigns[xm] = cs;
		tails[xm] = tn;
		acids[xm] = ac;
		aclds[xm] = ar;
		flnms[xm] = fn;
		deics[xm] = di;
		arics[xm] = ai;
		souls[xm] = so;
		cargs[xm] = ca;
		ftims[xm] = ft;
		lats[xm] = lt;
		lons[xm] = ln;
		hdgs[xm] = hd;
		attds[xm] = al;
		iasps[xm] = ia;
		tasps[xm] = ta;
		gspds[xm] = gs;
		machs[xm] = ma;
		emils[xm] = em;
		mflws[xm] = mf;
		status[xm] = st;
		displayedMarkers[xm] = marker;
		xm++;
		return marker;
	}
	
	function resetAllVars(){
		vpres = [];
		imgs = [];
		csigns = [];
		tails = [];
		acids = [];
		aclds = [];
		flnms = [];
		deics = [];
		arics = [];
		souls = [];
		cargs = [];
		ftims = [];
		lats = [];
		lons = [];
		hdgs = [];
		attds = [];
		iasps = [];
		tasps = [];
		gspds = [];
		machs = [];
		emils = [];
		mflws = [];
		status = [];
		x = 0;
		xm = 0;
		i = 0;
	}
	
	// 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]));
	};
	
	//refresh the markers and data for the map and table
	function loadMapData(fName){
		//map.clearOverlays();
		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("lt")), parseFloat(markers[i].getAttribute("ln")));
				var tmiles = 0;
				var mflwn = 0;
				var mdest = 0;
				var pdis = "";
				
				tmiles = markers[i].getAttribute("em");
				mdest = tmiles - eval(markers[i].getAttribute("mf"));
				mflwn = tmiles - mdest;
				if(markers[i].getAttribute("st") != "1"){
					if(mflwn > 0){
						pflwn = 100 - Math.round((mdest / tmiles) * 100);
						if(pflwn > 10){
							pdis = pflwn + "%";
						}
						if(pflwn > 100){
							pdis = "100%";
						}
					}else{
						pflwn = 0;
						pdis = "";
					}
				}else{
					pflwn = 0;
					pdis = "";
				}
				
				var stat = markers[i].getAttribute("st");
				var fltstat = "";
				
				if(stat == "0"){
					bgclr = "#FADADA";
					barclr = "#D43C3C";
					fltstat = "Desembarque";
				}else if(stat == "1"){
					bgclr = "#FADADA";
					barclr = "#D43C3C";
					fltstat = "Embarque";
				}else if(stat == "2"){
					bgclr = "#FFEED5";
					barclr = "#CC7A00";
					fltstat = "Taxi para a Pista";
				}else if(stat == "3"){
					bgclr = "#C2F0C2";
					barclr = "#009600";
					fltstat = "Decolando";
				}else if(stat == "4"){
					bgclr = "#C2F0C2";
					barclr = "#009600";
					fltstat = "Em Ascensão";
				}else if(stat == "5"){
					bgclr = "#C2F0C2";
					barclr = "#009600";
					fltstat = "Em Cruzeiro";
				}else if(stat == "6"){
					bgclr = "#C2F0C2";
					barclr = "#009600";
					fltstat = "Em Descida";
				}else if(stat == "7"){
					bgclr = "#C2F0C2";
					barclr = "#009600";
					fltstat = "Pousando";
				}else if(stat == "8"){
					bgclr = "#FFFFFF";
					barclr = "#333333";
					fltstat = "Taxi para o Gate";
				}else if(stat == "9"){
					bgclr = "#FADADA";
					barclr = "#D43C3C";
					fltstat = "No Gate";
				}
				
				if(markers[i].getAttribute("im") == "0"){
					img = "";
				}else{
					img = "http://www.golvirtual.org/" + markers[i].getAttribute("im");
				}
				
				//popup html code
				pophtml = "<table cellpadding=\"0\" width=\"300\" border=\"0\">";
				pophtml += "<tr class=\"phdr\"><td width=\"128\" valign=\"top\"><img src=\"" + img + "\" width=\"56\" height=\"25\"></td>";
    			pophtml += "<td width=\"162\" align=\"right\" valign=\"bottom\">" + markers[i].getAttribute("pn") + "&nbsp;&nbsp;</td></tr>";
				pophtml += "<tr><td colspan=\"2\"><table cellpadding=\"0\" width=\"100%\" border=\"0\">";
      			pophtml += "<tr><td width=\"23%\" class=\"pra\">Aeronave:</td><td colspan=\"3\" class=\"prb\">" + markers[i].getAttribute("ar") + "&nbsp;&nbsp;</td></tr>";        		
				pophtml += "<tr><td width=\"23%\" class=\"pra\">Voo:</td><td width=\"27%\" class=\"prb\">" + markers[i].getAttribute("fn") + " &nbsp;&nbsp;</td>";
				pophtml += "<td class=\"pra\">Origem/Destino:</td><td class=\"prb\">" + markers[i].getAttribute("ia") + "&nbsp;&nbsp;</td></tr>";
      			pophtml += "<tr><td class=\"pra\">Altitude:</td><td class=\"prb\">" + markers[i].getAttribute("al") + " ft&nbsp;&nbsp;</td>";
				pophtml += "<td class=\"pra\">Proa:</td><td class=\"prb\">" + markers[i].getAttribute("hd") + "&nbsp;&nbsp;</td></tr>";
				pophtml += "<tr><td class=\"pra\">Matrícula:</td><td class=\"prb\">" + markers[i].getAttribute("ma") + "&nbsp;&nbsp;</td>";
        		pophtml += "<td class=\"pra\">GS:</td><td class=\"prb\">" + markers[i].getAttribute("gs") + " kts&nbsp;&nbsp;</td></tr>";
				pophtml += "<tr><td class=\"pra\">EET:</td><td class=\"prb\">" + markers[i].getAttribute("ft") + " hrs&nbsp;&nbsp;</td>";      			
      			pophtml += "<td class=\"pra\">POB:</td><td width=\"27%\" class=\"prb\">" + markers[i].getAttribute("so") + "&nbsp;&nbsp;</td></tr>";        		
      			pophtml += "<tr><td height=\"18\" colspan=\"2\" class=\"pra\">NM Voadas:</td><td colspan=\"2\" class=\"prb\">" + markers[i].getAttribute("mf") + "&nbsp;&nbsp;</td></tr>";
      			pophtml += "<tr><td height=\"18\" colspan=\"2\" class=\"pra\">NM Restantes:</td><td colspan=\"2\" class=\"prb\">" + mdest + "&nbsp;&nbsp;</td></tr>";
      			pophtml += "<tr><td class=\"pra\">Situação:</td><td colspan=\"3\" class=\"prb\">" + fltstat + "&nbsp;&nbsp;</td></tr>";
    			pophtml += "</table></td></tr></table>";
				
				var marker = createMarker(
					point, 
					i, 
					pophtml,
					markers[i].getAttribute("vp"),
					markers[i].getAttribute("im"),
					markers[i].getAttribute("cs"), 
					markers[i].getAttribute("tn"), 
					markers[i].getAttribute("ac"), 
					markers[i].getAttribute("ar"),
					markers[i].getAttribute("fn"), 
					markers[i].getAttribute("di"), 
					markers[i].getAttribute("ai"),
					markers[i].getAttribute("so"),
					markers[i].getAttribute("ca"),
					markers[i].getAttribute("ft"),
					markers[i].getAttribute("lt"),
					markers[i].getAttribute("ln"),
					markers[i].getAttribute("hd"),
					markers[i].getAttribute("al"),
					markers[i].getAttribute("ia"),
					markers[i].getAttribute("ta"),
					markers[i].getAttribute("gs"),
					markers[i].getAttribute("ma"),
					markers[i].getAttribute("em"),
					markers[i].getAttribute("mf"),
					markers[i].getAttribute("st")
				);
				
					//MINHA
					function drawroute(latfrom,lonfrom,latactual,lonactual,latto,lonto)
					{
					var waypointsg = new Array();
					var f = new GLatLng(latfrom,lonfrom);
					waypointsg.push(f);
					var a = new GLatLng(latactual,lonactual);
					waypointsg.push(a);
					var waypointsr = new Array();
					var a = new GLatLng(latactual,lonactual);
					waypointsr.push(a);
					var t = new GLatLng(latto,lonto);
					waypointsr.push(t);
					var polyOptions = {geodesic:true};
					routeWaypointsg = new GPolyline(waypointsg, '#FF9900', 3, 1.0, polyOptions);
					routeWaypointsr = new GPolyline(waypointsr, '#FFFFFF', 3, 1.0, polyOptions);
					map.addOverlay(routeWaypointsg);
					map.addOverlay(routeWaypointsr);
					}
					////////
				
				map.addOverlay(marker);
				
				if(rcls == "rowb"){
					rcls = "rowa";
				}else{
					rcls = "rowb";
				}
				
				prow[i] = "<tr class=\"" + rcls + "\" onmouseover=\"this.className='hvr'\" onmouseout=\"this.className='" + rcls + "'\" onclick=\"tclick(" + i + ")\">";
      			/*prow[i] += "<td width=\"8%\" align=\"left\">" + markers[i].getAttribute("tn") + " (" + markers[i].getAttribute("ac") + ")</td>";*/
      			prow[i] += "<td width=\"20%\" align=\"left\">" + markers[i].getAttribute("pn") + "</td>";
      			prow[i] += "<td width=\"10%\" align=\"left\">" + markers[i].getAttribute("di") + "</td>";
      			prow[i] += "<td width=\"10%\" align=\"left\">" + markers[i].getAttribute("ai") + "</td>";
      			prow[i] += "<td width=\"15%\" align=\"left\">";
				prow[i] += "<table border='0' width='98%' height='15px' cellspacing='0' cellpadding='0' bgcolor='" + bgclr + "'>";
				prow[i] += "<tr><td align='left'><table border='0' align='left' width='" + pflwn + "%' height='15px' cellspacing='0' cellpadding='0' bgcolor='" + barclr + "'>";
				prow[i] += "<tr><td align='center'><font face='Tahoma' size='1' color='#FFFFFF'>" + pdis + "</font></td></tr></table></td>";
				prow[i] += "</tr></table></td>";
      			/*prow[i] += "<td width=\"10%\" align=\"center\">" + markers[i].getAttribute("hd") + "</td>";*/
      			prow[i] += "<td width=\"5%\" align=\"center\">" + markers[i].getAttribute("gs") + "</td>";
      			prow[i] += "<td width=\"5%\" align=\"center\">" + markers[i].getAttribute("al") + "</td>";
      			prow[i] += "<td width=\"10%\" align=\"right\"><div align=\"center\">" + fltstat + "</div></td></tr>";
				
				allrows += prow[i];
			}
			
			var ptable = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">";
    			ptable += "<tr class=\"hdr\">";
      			/*ptable += "<td width=\"8%\" align=\"left\">EQUIPAMENTO</td>";*/
      			ptable += "<td width=\"20%\" align=\"left\">TRIPULANTE</td>";
      			ptable += "<td width=\"10%\" align=\"left\">ORIGEM</td>";
      			ptable += "<td width=\"10%\" align=\"left\">DESTINO</td>";
      			ptable += "<td width=\"15%\" align=\"left\">PROGRESSO</td>";
      			/*ptable += "<td width=\"10%\" align=\"center\">HEADING</td>";*/
      			ptable += "<td width=\"5%\" align=\"center\">GS</td>";
      			ptable += "<td width=\"5%\" align=\"center\">ALTITUDE</td>";
      			ptable += "<td width=\"10%\" align=\"right\"><div align=\"center\">SITUAÇÃO</div></td></tr>";
			
			window.document.getElementById("ptab").innerHTML = ptable + allrows + "</table>";

			ptable = "";
			allrows = "";
			
		});
	}