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

File:		airspace.js
Abstract:	handling and display of airspace
Version:	1.0
Author:		Pascal Dreer / Jože Senegačnik

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

var gAirspaceList = [];

var radar_map_overlay = false;
var radar_map;

//strokeType A string that specifies the stroke style for the current selection. Acceptable values are "hairline", "solid", "dashed", "dotted", "ragged", "stipple", and "hatched".                                                                                                                                                                                                                                                                                                                                                                                                                                                                    									break;
var lineStrike = "";


function createPolygon(polygon,ptsName,ptsType,ptsAL,ptsAH,showAirspace)
{
	gMap.addOverlay(polygon);
        //polygon.setStrokeStyle(lineStrike);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	
	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 "P":	var polygon = new GPolygon(polyArray,"#cf0019",2,0.6,"#cf0019",0.1);
									ptsType = "Prohibited Area";
									lineStrike = "dotted";
									break;
						case "MT":	var polygon = new GPolygon(polyArray,"#cf0019",2,0.6,"#cf0019",0.1);
									ptsType = "Military Training Area";
									lineStrike = "dotted";
									break;
						case "R":	var polygon = new GPolygon(polyArray,"#cf0019",2,0.6,"#cf0019",0.1);
									ptsType = "Restricted Area";
									lineStrike = "dotted";
									break;
						case "Q":	var polygon = new GPolygon(polyArray,"#cf0019",2,0.6,"#cf0019",0.1);
									ptsType = "Danger Area";
									lineStrike = "dotted";
									break;
						
						case "C":	var polygon = new GPolygon(polyArray,"#0000dd",2,0.5,"#3366ff",0.05);
									ptsType = "CTR";
									lineStrike = "dashed";
									break;
						case "TC":	var polygon = new GPolygon(polyArray,"#860e31",4,0.5,"#860e31",0.05);
									ptsType = "TMA Class C";
									lineStrike = "solid";
									break;
									
						case "TD":	var polygon = new GPolygon(polyArray,"#0000dd",4,0.5,"#3366ff",0.05);
									ptsType = "TMA Class D";
									lineStrike = "solid";
									break;
									
						case "TE":	var polygon = new GPolygon(polyArray,"#135428",4,0.5,"#135428",0.05);
									ptsType = "TMA Class E";
									lineStrike = "solid";
									break;
						case "TM":	var polygon = new GPolygon(polyArray,"#135428",4,0.5,"#135428",0.05);
									ptsType = "TMA";
									lineStrike = "solid";
									break;

						case "A":	var polygon = new GPolygon(polyArray,"#135428",4,0.5,"#135428",0.05);
									ptsType = "CTA Class E";
									lineStrike = "solid";
									break;
	
						case "TT":	var polygon = new GPolygon(polyArray,"#0000dd",1,0.5,"#3366ff",0.05);
									ptsType = "MIL TMA Tempo"
									lineStrike = "solid";
									break;
									
						default:	;
										
					}
					if (polygon) {
						createPolygon(polygon,ptsName,ptsType,ptsAL,ptsAH,showAirspace);
						pts = false;
						ptsType = "";
						ptsName = "";	
						ptsAL = "";
						ptsAH = "";
					}
	
				}
				continue;
			}
			
			
			if (pts) {
                                // multiple airspaces for the same polygon
				if (lines[i].indexOf("AM") >= 0) {
					ptsAH = lines[i].substr(3);
                                        ptsAL = "";
					continue;
				}

				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);
					lat = parseFloat(lines[i].substr(3,2))  + (parseFloat(lines[i].substr(6,2)) / 60) + (parseFloat(lines[i].substr(9,5)) / 3600);
					lng = parseFloat(lines[i].substr(17,3))  + (parseFloat(lines[i].substr(21,2)) / 60) + (parseFloat(lines[i].substr(24,5)) / 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,5)) / 3600);
					lng = parseFloat(lines[i].substr(18,3))  + (parseFloat(lines[i].substr(22,2)) / 60) + (parseFloat(lines[i].substr(25,5)) / 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 < 65; j++) { 
							var theta = Math.PI * (j/32); 
    						        var CPlng = lng + (Clng * Math.cos(theta)); 
    						        var CPlat = lat + (Clat * Math.sin(theta)); 
    						        var P = new GLatLng(CPlat,CPlng);
    						        polyArray.push(P); 
						}
                                                /*
                                                // new algorithm
                                                var R = 6371; // earth's mean radius in km
                                                var lat = (lat * Math.PI) / 180; //rad
                                                var lon = (lng * Math.PI) / 180; //rad
                                                var d = rad/R;  // d = angular distance covered on earth's surface
                                                //var locs = new Array();
                                                for (var x = 0; x <= 360; x+=10)
                                                {
                                                    //var p2 = new GLatLng(CPlat,CPlng);           
                                                    brng = x * Math.PI / 180; //rad
                                                    var CPlat = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
                                                    var CPlng = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(CPlat))) * 180) / Math.PI;
                                                    CPlat = (CPlat * 180) / Math.PI;
                                                    var P = new GLatLng(CPlat,CPlng); 
                                                    polyArray.push(P);
                                                }
                                                */
                                                //alert(x);

					}
                          
					
					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 MIL") >= 0) {
				i++;
				pts = true;
				ptsType = "MT";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC P") >= 0) {
				i++;
				pts = true;
				ptsType = "P";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC CTR") >= 0) {
				i++;
				pts = true;
				ptsType = "C";
                                lineStrike = "dashed";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC CTA") >= 0) {
				i++;
				pts = true;
				ptsType = "A";
                                lineStrike = "dashed";
				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 D") >= 0) {
				i++;
				pts = true;
				ptsType = "TD";
				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 E") >= 0) {
				i++;
				pts = true;
				ptsType = "TE";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC TMA M") >= 0) {
				i++;
				pts = true;
				ptsType = "TM";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
			if (lines[i].indexOf("AC TMA R") >= 0) {
				i++;
				pts = true;
				ptsType = "R";
				ptsName = lines[i].substr(3);
				var polyArray = Array();
				continue;
			}
		}
	}
/*
        var VFRrecomendedRoute = new GPolyline([
           new GLatLng( 46.64685833, 15.209175 ),
           new GLatLng( 46.23302778, 15.29516667 ),
           new GLatLng( 46.06637500, 15.17852222 ),
           new GLatLng( 45.90527500, 15.0202166 ),
           new GLatLng( 45.83305278, 14.62860000 ),
           new GLatLng( 45.83905000, 14.26353889 ),
           new GLatLng( 45.75750278, 14.07165833 ),
           new GLatLng( 45.729026, 14.004242 ),
           new GLatLng( 45.550301, 13.878908 )
           ], 
        "#3366ff", 4, 0.4);
        gMap.addOverlay(VFRrecomendedRoute);
	GEvent.addListener(VFRrecomendedRoute,"click",function(point) {
		var windowHtml = "<span class='infoHead'>VFR Recommended route</span><br><span class='infoText'><br>7500 MSL - 1000 AGL</span>";
		windowHtml = "<div id='infoDiv" + "' class='infoBox'>" + windowHtml + "</div>";
		gMap.openInfoWindowHtml(point,windowHtml)
        });

        gAirspaceList.push(VFRrecomendedRoute);
*/
}



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




function hideAirspace()
{
for (var i = 0; i < gAirspaceList.length; i++) {
		gAirspaceList[i].poly.hide();
	}
//       if (radar_map_overlay )
//          radar_map.hide();
          	
/*	
	var boundaries = new GLatLngBounds(new GLatLng(45.22240833, 13.21842778), new GLatLng(47.01185, 16.71321944));
        var radar_map = new GGroundOverlay("http://meteo.arso.gov.si/uploads/probase/www/observ/radar/si1_zm_si_17.jpg", boundaries);
//        var radar_map = new GGroundOverlay("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/radar_animacija.html", boundaries);
        gMap.addOverlay(radar_map);

*/	
}



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

/*
        if ( !radar_map_overlay ) {
	alert('do sem 6a');

	  var boundaries = new GLatLngBounds(new GLatLng(45.22240833, 13.21842778), new GLatLng(47.01185, 16.71321944));
	alert('do sem 6b');

          //var radar_map = new GGroundOverlay("http://meteo.arso.gov.si/uploads/probase/www/observ/radar/si1_zm_si_17.jpg", boundaries);
	alert('do sem 7');
          radar_map = new GGroundOverlay("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/radar_anim.gif", boundaries);
          gMap.addOverlay(radar_map);
	alert('do sem 8');
          radar_map_overlay = true;
          alert("overlay");
        }
*/
//radar_map.show();
//	alert('do sem 5');
	
	return;

}