﻿//var airportID;
var _lon;
var _lat;
//var oldPoly = [];
var map1;
var poly = []; 
var line; 
//******************************************************************************************************
function getLonLat(id){
        _lon = trim($(id).value.split('|')[1]);
        _lat = trim($(id).value.split('|')[2]);
        initMapCatalog( _lon , _lat ); 
}
//******************************************************************************************************
function initMapCatalog(lon__, lat__){
      if ( GBrowserIsCompatible() ) {
        map1 = new GMap2( document.getElementById("map") );
        var mapControl = new GMapTypeControl();
        map1.addControl( mapControl );
        map1.addControl( new GSmallZoomControl() );
		var point = new GLatLng( lat__ , lon__ );
        map1.setCenter( point , 3 ); //3 degrees down
        
        var blueIcon = new GIcon( G_DEFAULT_ICON );
        blueIcon.image = "./html_forms/images/plane.png";
        blueIcon.iconSize = new GSize( 30 , 29 );
        markerOptions = { icon:blueIcon };
        
        marker = new GMarker(point, markerOptions);
		map1.addOverlay(marker);

		//drawCircle_(lon__, lat__, miles, strokeColor_, 2, 0.75, fillColor_, 0.15);//},30000);
		startPoint = new GLatLng(lat__, lon__);
		drawCircle(startPoint, miles, 40);
      }
}
//******************************************************************************************************
// Draw a circle on map around center (radius in miles)
function drawCircle(center, radius, numPoints){
    poly = [] ; 
    var lat = center.lat() ;
    var lng = center.lng() ;
    var d2r = Math.PI/180 ;                // degrees to radians
    var r2d = 180/Math.PI ;                // radians to degrees
    var Clat = (radius/3963) * r2d ;      //  using 3963 as earth's radius
    var Clng = Clat/Math.cos(lat*d2r);
    
    //Add each point in the circle
    for (var i = 0 ; i < numPoints ; i++){
        var theta = Math.PI * (i / (numPoints / 2)) ;
        Cx = lng + (Clng * Math.cos(theta)) ;
        Cy = lat + (Clat * Math.sin(theta)) ;
        poly.push(new GLatLng(Cy,Cx)) ;
    }
    
    //Remove the old line if it exists
//            if(line)
//            {
//                //map.removeOverlay(line) ;
//            }
    
    //Add the first point to complete the circle
    poly.push(poly[0]) ;

    //Create a line with teh points from poly, red, 3 pixels wide, 80% opaque
    line = new GPolyline(poly,'#FF0000', 3, 0.8) ;
    map1.addOverlay(line) ;
}

//******************************************************************************************************
/*
 * Add a circle to the global variable "map". This function won't work for circles that encompass
 * the North or South Pole. Also, there is a slight distortion in the upper-left, upper-right,
 * lower-left, and lower-right sections of the circle that worsens as it gets larger and/or closer
 * to a pole.
 * @param lat Latitude in degrees
 * @param lng Longitude in degrees
 * @param radius Radius of the circle in statute miles
 * @param {String} strokeColor Color of the circle outline in HTML hex style, e.g. "#FF0000"
 * @param strokeWidth Width of the circle outline in pixels
 * @param strokeOpacity Opacity of the circle outline between 0.0 and 1.0
 * @param {String} fillColor Color of the inside of the circle in HTML hex style, e.g. "#FF0000"
 * @param fillOpacity Opacity of the inside of the circle between 0.0 and 1.0
 */
//function drawCircle_(lat_, lng_, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
//  //alert(lat_+' , '+ lng_+' , '+ radius+' , '+ strokeColor+' , '+ strokeWidth+' , '+ strokeOpacity+' , '+ fillColor+' , '+ fillOpacity);
//  var d2r_ = Math.PI/180;
//  var r2d_ = 180/Math.PI;
//  var Clat_ = (radius/4) * 0.014483;  // Convert statute miles into degrees latitude
//  var Clng_ = Clat_/Math.cos(lat_*d2r_); 
//  var Cpoints_ = []; 
//  for (var i_=0; i_ < 33; i_++) { 
//    var theta_ = Math.PI * (i_/16); 
//    Cy_ = lat_ + (Clat_ * Math.sin(theta_)); 
//    Cx_ = lng_ + (Clng_ * Math.cos(theta_)); 
//    var P_ = new GPoint(Cx_,Cy_); 
//    Cpoints_.push(P_); 
//  }

//  var polygon_ = new GPolygon(Cpoints_, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity);
//  map1.addOverlay(new GPolygon(Cpoints_, '#000000', 2, 1, '#000000', 0.25));
//}
//*******************************************************************************************************
//function eraseCircles() {
//	if(oldPoly) {
//		for (var i = 0; i < oldPoly.length; i++) {
//			map1.removeOverlay(oldPoly[i]);
//		}
//		oldPoly = [];
//	}
//}
//*******************************************************************************************************
//function drawCircle(center, radius, color, thickness, opacity) {
//    //alert(radius);
//    r = radius / 100;
//    alert(r);
//	//Function created by Chris Haas
//	var circleQuality = 1;			//1 is best but more points, 5 looks pretty good, too
//	var M = Math.PI / 180;			//Create Radian conversion constant
//	var L = map1.getBounds();		//Holds copy of map bounds for use below
//	var sw = L.getSouthWest();
//	var ne = L.getNorthEast();

//	//The map is not completely square so this calculates the lat/lon ratio
//	// this works because we create a square map
//	var circleSquish = (ne.lng() - sw.lng()) / (ne.lat() - sw.lat());

//	var points = [];							//Init Point Array
//	//Loop through all degrees from 0 to 360
//	for(var i=0; i<360; i+=circleQuality){
//		var P = new GLatLng(
//			center.lat() + (r * Math.sin(i * M)),
//			center.lng() + (r * Math.cos(i * M)) * circleSquish
//			);
//		points.push(P);
//	}
//	points.push(points[0]);	// close the circle
//	var p = new GPolyline(points, color, thickness, opacity)
//	map1.addOverlay(p);
//	oldPoly.push(p);
//}
//var map2_;
//******************************************************************************************************
//function load_(lat_o,lon_o){
//      // if browser is compatible
//      if ( GBrowserIsCompatible() ) {
//     
//        map2_ = new GMap2( document.getElementById("map") );
//        var mapControl = new GMapTypeControl();
//        map2_.addControl( mapControl );
//        map2_.addControl( new GSmallZoomControl() );
//		var point = new GLatLng( lat_o , lon_o );
//        map2_.setCenter( point , 6 );       
//        
//        var blueIcon = new GIcon( G_DEFAULT_ICON );
//        blueIcon.image = "./html_forms/images/plane.png";
//        blueIcon.iconSize = new GSize( 30 , 29 );
//        markerOptions = { icon:blueIcon };
//        
//        marker = new GMarker( point , markerOptions );
//		map2_.addOverlay( marker );
//		drawCircle_(lat_o, lon_o, 100.0, "#FF0000", 2, 0.75, "#0000FF", 0.15);
//    }
//    
//}
//******************************************************************************************************
//is airports valid?
function validateAirport( airportCode ){
	//var exist = false;
	
	//if( edit ) airport = airportName;
	//else 
//	airport = airportCode.toUpperCase();
	
	//connect to xml file
	//load all airport nodes from the xml
	//var allItemsFromXML = xmlDoc.getElementsByTagName("airport");
	//alert(allItemsFromXML.length);
	//search it
	//for( i=0 ; i < allItemsFromXML.length ; i++ ){
		//get code attribute for each airport in the xml
		//var attr = trim(allItemsFromXML[i].getAttribute('code').toUpperCase());

		//compare the entered value against the XML
		//if( attr == airport ){
			exist = true;
	//		airportID = i;
		//	break;
		//}
	//}
	
	return exist;
}
//******************************************************************************************************
