/* ------------------------------------------------------------------

File:		airspace.js
Abstract:	handling and display of airspace
Version:	0.12
Author:		Pascal Dreer

------------------------------------------------------------------ */

var gAirspaceList = [];


function createPolygon(polygon,ptsName,ptsType,ptsAL,ptsAH,showAirspace)
{
	gMap.addOverlay(polygon);
	var obj = new Object();
	obj.poly = polygon;
	obj.ac = ptsType;
	obj.al = ptsAL;
	obj.ah = ptsAH;
	var frPos = ptsName.indexOf(":");
	if (frPos > 0) {
		obj.name = ptsName.substring(0,frPos);
		obj.freq = ", " + ptsName.substring(frPos+1);
	}
	else {
		obj.name = ptsName;
		obj.freq = "";
	}
	gAirspaceList.push(obj);
				
	if (!showAirspace)
		polygon.hide();
							
	GEvent.addListener(obj.poly,"click",function(point) {
		var windowHtml = "<span class='infoHead'>" + obj.name + "</span><br><span class='infoText'>" + obj.ac + obj.freq + "<br>" + obj.ah + " - " + obj.al + "</span>"
		windowHtml = "<div id='infoDiv" + "' class='infoBox'>" + windowHtml + "</div>";
		gMap.openInfoWindowHtml(point,windowHtml) 
	});
}



function loadAirspace(file)
{
	
	var showAirspace = gConfigShowAirspace;
	
	var lines = file.split("\n");
	
	var str = "";
	var pts = false;
	var ptsType = "";
	var ptsName = "";
	var ptsAH = "";
	var ptsAL = "";
	var lat = 0;
	var lng = 0;
	
	for (var i=0;i<lines.length;i++) {
     	if (lines[i].length > 0) {
			if (lines[i].indexOf("*") >= 0) {
				if (pts) {
					
					switch (ptsType) {
						case "Q":	var polygon = new GPolygon(polyArray,"#cf0019",2,0.6,"#cf0019",0.1);
									ptsType = "Danger Area";
									break;
						
						case "C":	var polygon = new GPolygon(polyArray,"#0000dd",4,0.5,"#3366ff",0.05);
									ptsType = "CTR";
									break;
									
						case "TC":	var polygon = new GPolygon(polyArray,"#860e31",4,0.5,"#860e31",0.05);
									ptsType = "TMA Class C";
									break;
									
						case "TD":	var polygon = new GPolygon(polyArray,"#0000dd",4,0.5,"#3366ff",0.05);
									ptsType = "TMA Class D";
									break;
									
						case "TE":	var polygon = new GPolygon(polyArray,"#135428",4,0.5,"#135428",0.05);
									ptsType = "TMA Class E";
									break;
	
						case "TT":	var polygon = new GPolygon(polyArray,"#0000dd",1,0.5,"#3366ff",0.05);
									ptsType = "MIL TMA Tempo"
									break;
									
						default:	;
										
					}
					if (polygon) {
						createPolygon(polygon,ptsName,ptsType,ptsAL,ptsAH,showAirspace);
						pts = false;
						ptsType = "";
						ptsName = "";	
						ptsAL = "";
						ptsAH = "";
					}
	
				}
				continue;
			}
			
			
			if (pts) {
				if (lines[i].indexOf("AH") >= 0) {
					ptsAH = lines[i].substr(3);
					continue;
				}
				if (lines[i].indexOf("AL") >= 0) {
					ptsAL = lines[i].substr(3);
					if (ptsAL.indexOf("SFC") >= 0)
						ptsAL = "GND";
					continue;
				}
	
				
				
				if (lines[i].indexOf("DP") >= 0) {
					lat = parseFloat(lines[i].substr(3,2))  + (parseFloat(lines[i].substr(6,2)) / 60) + (parseFloat(lines[i].substr(9,2)) / 3600);
					lng = parseFloat(lines[i].substr(14,3))  + (parseFloat(lines[i].substr(18,2)) / 60) + (parseFloat(lines[i].substr(21,2)) / 3600);
					polyArray.push(new GLatLng(lat,lng));
					continue;
				}
				
				
				if (lines[i].indexOf("V") >= 0) {
					lat = parseFloat(lines[i].substr(4,2))  + (parseFloat(lines[i].substr(7,2)) / 60) + (parseFloat(lines[i].substr(10,2)) / 3600);
					lng = parseFloat(lines[i].substr(15,3))  + (parseFloat(lines[i].substr(19,2)) / 60) + (parseFloat(lines[i].substr(22,2)) / 3600);
					i++;
					if (lines[i].indexOf("DC") >= 0) {
						var rad = parseFloat(lines[i].substr(3,6)) * 1.852;	// NM to km
						
  						var d2r = Math.PI/180; 
  						var r2d = 180/Math.PI; 
  						var Clat = (rad/6367)*r2d; 
  						var Clng = Clat/Math.cos(lat*d2r); 
  						for (var j=0; j < 33; j++) { 
							var theta = Math.PI * (j/16); 
    						var CPlng = lng + (Clng * Math.cos(theta)); 
    						var CPlat = lat + (Clat * Math.sin(theta)); 
    						var P = new GLatLng(CPlat,CPlng);
    						polyArray.push(P); 
						}
					}
					
					continue;
				}
				
			}
			
			
			if (lines[i].indexOf("AC Q") >= 0) {
				i++;
				pts = true;
				ptsType = "Q";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC CTR") >= 0) {
				i++;
				pts = true;
				ptsType = "C";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC TMA TEMPO") >= 0) {
				i++;
				pts = true;
				ptsType = "TT";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}	
			if (lines[i].indexOf("AC TMA C") >= 0) {
				i++;
				str = str + lines[i] + ",";
				pts = true;
				ptsType = "TC";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC TMA E") >= 0) {
				i++;
				pts = true;
				ptsType = "TE";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC TMA") >= 0) {
				i++;
				pts = true;
				ptsType = "TD";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
		}
	}
}



function initAirspace()
{
	if (gConfigShowAirspace)
		document.getElementById("prefs_airspace").checked = true;
	
	GDownloadUrl("LR_OpenAir_Swiss_All.txt",loadAirspace);
	
}




function hideAirspace()
{
for (var i = 0; i < gAirspaceList.length; i++) {
		gAirspaceList[i].poly.hide();
	}	
	
	
}



function displayAirspace()
{
	for (var i = 0; i < gAirspaceList.length; i++) {
		gAirspaceList[i].poly.show();
	}
	
	
	
	
	
	
	return;
  	var polygon = new GPolygon([
    	new GLatLng(47.014167,6.647778),
    	new GLatLng(47.056389,6.708056),
    	new GLatLng(47.063056,6.711944),
    	new GLatLng(47.125000,6.827778),
    	new GLatLng(47.178611,6.933889),
		new GLatLng(47.135556,6.973889),
		new GLatLng(47.100000,6.870833),
		new GLatLng(47.029444,6.791389),
		new GLatLng(46.980556,6.719722),
		new GLatLng(47.014167,6.647778)], "#0000dd", 5, 0.5, "#3366ff", 0.05);
	gMap.addOverlay(polygon);
	
	
	
	
	
  	var polygon = new GPolygon([
    	new GLatLng(46.915833,6.958056),
    	new GLatLng(46.832222,6.819722),
    	new GLatLng(46.857778,6.586111),
    	new GLatLng(47.021389,6.846667),
		new GLatLng(46.915833,6.958056)], "#830f33", 2, 0.5, "#830f33", 0.05);
	gMap.addOverlay(polygon);
	


	GEvent.addListener(polygon, "click", function(point) {
		
		var windowHtml = "<div>LS-D4</div>";
		
		point.openInfoWindowHtml(windowHtml);
	});
	

}