﻿    // Long live Google
    // Long live Open Source
    
    function init( code , index ) {
    
      //prepare values
      lon = lonArray[index];
      lat = latArray[index];
      desc = descs[index];
      city = cities[index];
      country = countries[index];
      
      // if browser is compatible
      if ( GBrowserIsCompatible() ) {
      
        // get container
        var map = new GMap2( document.getElementById("vmaps") );
        
        // set map type
        var mapControl = new GMapTypeControl();
        map.addControl( mapControl );
        // small zoom control
        map.addControl( new GSmallZoomControl() );
        
        // lat , lon
		var point = new GLatLng( lat , lon );
		
        // draw map
        map.setCenter( point , 6 );
        
        // Create marker icon
        var blueIcon = new GIcon( G_DEFAULT_ICON );
        blueIcon.image = "./html_forms/images/plane.png";
        
        // icon size
        blueIcon.iconSize = new GSize( 30 , 29 );
        
        // icon anchor
        //blueIcon.iconAnchor = new GPoint(6, 20);
        
        // Set up our GMarkerOptions object
        markerOptions = { icon:blueIcon };
        
        //add marker + markerOptions to the selected lon , lat
        marker = new GMarker( point , markerOptions );
		map.addOverlay( marker );
		
		// info Windows
        GEvent.addListener( marker , "click" , function() {
            marker.openInfoWindowHtml( city + ", " + country + "<br />" + desc );
        });
      }
    }
//***********************************************************************************************
    function init_map_with_lines() {
    
      // if browser is compatible
      if ( GBrowserIsCompatible() ) {
        
			// === The basis of the arrow icon information ===
			var arrowIcon = new GIcon();
			arrowIcon.iconSize = new GSize(24,24);
			arrowIcon.shadowSize = new GSize(1,1);
			arrowIcon.iconAnchor = new GPoint(12,12);
			arrowIcon.infoWindowAnchor = new GPoint(0,0);
			  
			// === Returns the bearing in degrees between two points. ===
			// North = 0, East = 90, South = 180, West = 270.
			var degreesPerRadian = 180.0 / Math.PI;
			
			function bearing( from , to ) {
				  // See T. Vincenty, Survey Review, 23, No 176, p 88-93,1975.
				  // Convert to radians.
				  var lat1 = from.latRadians();
				  var lon1 = from.lngRadians();
				  var lat2 = to.latRadians();
				  var lon2 = to.lngRadians();
		
				  // Compute the angle.
				  var angle = - Math.atan2( Math.sin( lon1 - lon2 ) * Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) - Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
				  if ( angle < 0.0 ) angle  += Math.PI * 2.0;
		
				  // And convert result to degrees.
				  angle = angle * degreesPerRadian;
				  angle = angle.toFixed(1);
		
				  return angle;
			}
			   
			// === A function to create the arrow head at the end of the polyline ===
			function arrowHead( points ) {
				  // == obtain the bearing between the last two points
				  var p1=points[points.length-1];
				  var p2=points[points.length-2];
				  var dir = bearing(p2,p1);
				  // == round it to a multiple of 3 and cast out 120s
				  var dir = Math.round(dir/3) * 3;
				  while (dir >= 120) {dir -= 120;}
				  // == use the corresponding triangle marker 
				  arrowIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/dir_" + dir + ".png";
				  map.addOverlay(new GMarker(p1, arrowIcon));
			}
			 
			// === A function to put arrow heads at intermediate points
			function midArrows( points ) {
				  for (var i=1; i < points.length-1; i++) {  
					var p1=points[i-1];
					var p2=points[i+1];
					var dir = bearing(p1,p2);
					// == round it to a multiple of 3 and cast out 120s
					var dir = Math.round(dir/3) * 3;
					while (dir >= 120) {dir -= 120;}
					// == use the corresponding triangle marker 
					arrowIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/dir_" + dir + ".png";
					map.addOverlay( new GMarker( points[i] , arrowIcon ) );
				  }
			}
			
			// create a marker			
			function createMarker( point , html ) {
              var marker = new GMarker(point);
              GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
              });
              return marker;
            }
            
			// get container
			var map = new GMap2( document.getElementById("entire_map") );
			
			map.setCenter( new GLatLng( 0 , 0 ) , 0 );
			//map.setCenter( new GLatLng( latArray[0] , lonArray[0] ) , 4 );
			
			// ===== Start with an empty GLatLngBounds object =====
			var bounds = new GLatLngBounds();
			
			// set map type
			var mapControl = new GMapTypeControl();
			map.addControl( mapControl );
			map.addControl( new GLargeMapControl() );
				
			// === The array of points for the polyline ===
		    var points = [];

           /*
           var icon = new GIcon();
           icon.image = 'http://gmaps-samples.googlecode.com/svn/trunk/markers/circular/greencirclemarker.png';
           icon.iconSize = new GSize(32, 32);
           icon.iconAnchor = new GPoint(16, 16);
           icon.infoWindowAnchor = new GPoint(25, 7);
           */
                //alert(lonArray[0]);
			for( i=0 ; i<lonArray.length; i++ ){
			    
			    d = descs[i];
			    c = cities[i];
			    cn = countries[i];
			    
			    //alert( latArray[i] +  " / " + lonArray[i] );
			    
				point = new GLatLng( latArray[i] , lonArray[i] );
				//prepare the points array
			    points[i] = point;
			    
			    // add marker to the selected lon , lat
			    // info Windows
                //marker = new GMarker( point , 'A' );
                xx = createMarker( point , "" + c + ", " + cn + "<br />" + d  );
		        map.addOverlay( xx );
		
		        // ==== Each time a point is found, extent the bounds to include it =====
                bounds.extend(point);
			}
			//points = points.substring(0, points.length-1);
			//var points2 = [ new GLatLng(37.4419, -122.1419), new GLatLng(37.4519, -122.1519),new GLatLng(37.4619, -122.1619),new GLatLng(37.4719, -122.1719)];
			
			// === Create the polyline
			map.addOverlay(new GPolyline(points));//,color,line_width
			
			// === add the arrow head
			arrowHead(points);
			midArrows(points);
			
			// ===== determine the zoom level from the bounds =====
            map.setZoom(map.getBoundsZoomLevel(bounds));

            // ===== determine the centre from the bounds ======
            map.setCenter(bounds.getCenter());

    	}
	}
//***********************************************************************************************
    function init_map_for_nearby_airports( array ) {
    
      // if browser is compatible
      if ( GBrowserIsCompatible() ) {
        
			// === The basis of the arrow icon information ===
			var arrowIcon = new GIcon();
			arrowIcon.iconSize = new GSize(24,24);
			arrowIcon.shadowSize = new GSize(1,1);
			arrowIcon.iconAnchor = new GPoint(12,12);
			arrowIcon.infoWindowAnchor = new GPoint(0,0);
			  
			// create a marker			
			function createMarker( point , html ) {
              var marker = new GMarker( point );
              GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
              });
              
              return marker;
            }
                
            // get container
			var map = new GMap2( document.getElementById("vmaps") );
			
			//set map zoom to 0 [the entire world]
			map.setCenter( new GLatLng( 0 , 0 ) , 0 );
			
			// ===== Start with an empty GLatLngBounds object =====
			var bounds = new GLatLngBounds();
			
			// set map type
			var mapControl = new GMapTypeControl();
			map.addControl( mapControl );
			map.addControl( new GSmallZoomControl() );
			
            //to get the first lon, lat to center the map
            var firstArrayLonLat = 0;
            
            for( k=1 ; k<array.length ; k++ ){
            
                //alert( array[k] );
                
                //split each entry
                current = array[k].split("|");
                //alert(current.length);
                
//                //prepare values
//                city = current[0];
//                desc = current[1];
//                lon = current[2];
//                lat = current[3];
//                country = current[4];
                city = current[3];
                desc = "*";
                lon = current[2];
                lat = current[1];
                country = current[4];
                code = current[0];
                
                
                //alert(city);alert(desc);alert(lon);alert(lat);alert(country);
               
                //point
                point = new GLatLng( lat , lon );
              
                //yellow icon
                //var myIcon = new GIcon(G_DEFAULT_ICON);
                //myIcon.image = "http://www.google.com/uds/samples/places/temp_marker.png";
                
                // add marker to the selected lon , lat
			    // info windows
			    if( firstArrayLonLat == 0 ){
			    
//			        map.setCenter( point , 6 );
                    marker = createMarker( point , code + "<br />" + city + "." );
		            map.addOverlay( marker );
		            marker.setImage( "http://esa.ilmari.googlepages.com/markeryellow.png" );
                    //just once
                    firstArrayLonLat = 1;
                    
                }else{
                
                    marker = createMarker( point , code + "<br />" + city + "." );
                    map.addOverlay( marker );
                    
                }
                		        
		        // ==== Each time a point is found, extent the bounds to include it =====
                bounds.extend(point);
		                        
            }
            
            // ===== determine the zoom level from the bounds =====
            map.setZoom(map.getBoundsZoomLevel(bounds));

            // ===== determine the centre from the bounds ======
            map.setCenter(bounds.getCenter());
    	}
	}
//***********************************************************************************************
//init map for one leg and round trip
    function init_map_with_lines2( where ) {//alert(f1);alert(t1);
    
        /*
        alert(oneLeg_roundTrip.length);
        alert(oneLeg_roundTrip[0]);
        alert(oneLeg_roundTrip[1]);
        alert(oneLeg_roundTrip[2]);
        alert(oneLeg_roundTrip[3]);
        */
    
      // if browser is compatible
      if ( GBrowserIsCompatible() ) {
        
			// === The basis of the arrow icon information ===
			var arrowIcon = new GIcon();
			arrowIcon.iconSize = new GSize(24,24);
			arrowIcon.shadowSize = new GSize(1,1);
			arrowIcon.iconAnchor = new GPoint(12,12);
			arrowIcon.infoWindowAnchor = new GPoint(0,0);
			  
			// === Returns the bearing in degrees between two points. ===
			// North = 0, East = 90, South = 180, West = 270.
			var degreesPerRadian = 180.0 / Math.PI;
			
			function bearing( from , to ) {
				  // See T. Vincenty, Survey Review, 23, No 176, p 88-93,1975.
				  // Convert to radians.
				  var lat1 = from.latRadians();
				  var lon1 = from.lngRadians();
				  var lat2 = to.latRadians();
				  var lon2 = to.lngRadians();
		
				  // Compute the angle.
				  var angle = - Math.atan2( Math.sin( lon1 - lon2 ) * Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) - Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
				  if ( angle < 0.0 ) angle  += Math.PI * 2.0;
		
				  // And convert result to degrees.
				  angle = angle * degreesPerRadian;
				  angle = angle.toFixed(1);
		
				  return angle;
			}
			   
			// === A function to create the arrow head at the end of the polyline ===
			function arrowHead( points ) {
				  // == obtain the bearing between the last two points
				  var p1=points[points.length-1];
				  var p2=points[points.length-2];
				  var dir = bearing(p2,p1);
				  // == round it to a multiple of 3 and cast out 120s
				  var dir = Math.round(dir/3) * 3;
				  while (dir >= 120) {dir -= 120;}
				  // == use the corresponding triangle marker 
				  arrowIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/dir_" + dir + ".png";
				  mapOtherLegs.addOverlay(new GMarker(p1, arrowIcon));
			}
			 
			// === A function to put arrow heads at intermediate points
			function midArrows( points ) {
				  for (var i=1; i < points.length-1; i++) {  
					var p1=points[i-1];
					var p2=points[i+1];
					var dir = bearing(p1,p2);
					// == round it to a multiple of 3 and cast out 120s
					var dir = Math.round(dir/3) * 3;
					while (dir >= 120) {dir -= 120;}
					// == use the corresponding triangle marker 
					arrowIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/dir_" + dir + ".png";
					mapOtherLegs.addOverlay( new GMarker( points[i] , arrowIcon ) );
				  }
			}
			
			// create a marker			
			function createMarker( point , html ) {
              var marker = new GMarker(point);
              GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
              });
              return marker;
            }
            
			// get container			
			var mapOtherLegs = new GMap2( document.getElementById("entire_map") );
			mapOtherLegs.setCenter( new GLatLng( 0 , 0 ) , 0 );
						
			// ===== Start with an empty GLatLngBounds object =====
			var bounds_ = new GLatLngBounds();
			
			// set map type
			var mapControl_ = new GMapTypeControl();
			mapOtherLegs.addControl( mapControl_ );
			mapOtherLegs.addControl( new GLargeMapControl() );
				
			// === The array of points for the polyline ===
            var points_ = [];
            
            if( where == 'one_leg' ){
                    //alert($('ctl00_content_book1_hdn_From').value);
                    var pFromOneLeg = $('ctl00_content_book1_hdn_From').value.split('|');
                    var pToOneLeg = $('ctl00_content_book1_hdn_To').value.split('|');
                    
				    point1_ = new GLatLng( trim(pFromOneLeg[2]) , trim(pFromOneLeg[1]) );
			        html1 = trim(pFromOneLeg[3]) +', '+ trim(pFromOneLeg[4]) +'<br /> '+ trim(pFromOneLeg[5]);
				    point2_ = new GLatLng( trim(pToOneLeg[2]) , trim(pToOneLeg[1]) );
			        html2 = trim(pToOneLeg[3]) +', '+ trim(pToOneLeg[4]) +'<br /> '+ trim(pToOneLeg[5]);
			        
				    //prepare the points array
			        points_.push( point1_ , point2_ );
    			    
			        // add marker to the selected lon , lat
			        // info Windows
                    xxOtherLegs1 = createMarker( point1_ , html1 );
		            mapOtherLegs.addOverlay( xxOtherLegs1 );
		            xxOtherLegs2 = createMarker( point2_ , html2 );
		            mapOtherLegs.addOverlay( xxOtherLegs2 );   		        
    		
		            // ==== Each time a point is found, extent the bounds to include it =====
                    bounds_.extend(point1_);
                    bounds_.extend(point2_);
                    
			}else if( where == 'round_trip' ){
                    //alert($('ctl00_content_book1_hdn_From_round_trip').value);
                    var pFromRoundtrip = $('ctl00_content_book1_hdn_From_round_trip').value.split('|');
                    var pToRoundtrip = $('ctl00_content_book1_hdn_To_round_trip').value.split('|');
			        
			        //alert(f3+' '+t3+' '+f4+' '+t4);
				    point1_ = new GLatLng( trim(pFromRoundtrip[2]) , trim(pFromRoundtrip[1]) );
			        html1 = trim(pFromRoundtrip[3]) +', '+ trim(pFromRoundtrip[4]) +'<br /> '+ trim(pFromRoundtrip[5]);
				    point2_ = new GLatLng( trim(pToRoundtrip[2]) , trim(pToRoundtrip[1]) );
			        html2 = trim(pToRoundtrip[3]) +', '+ trim(pToRoundtrip[4]) +'<br /> '+ trim(pToRoundtrip[5]);
				    point3_ = new GLatLng( trim(pFromRoundtrip[2]) , trim(pFromRoundtrip[1]) );
			        html3 = trim(pFromRoundtrip[3]) +', '+ trim(pFromRoundtrip[4]) +'<br /> '+ trim(pFromRoundtrip[5]);
			        
				    //prepare the points array
			        points_.push( point1_ , point2_ , point3_ );
    			    
			        // add marker to the selected lon , lat
			        // info Windows
                    xxOtherLegs1 = createMarker( point1_ , html1 );
		            mapOtherLegs.addOverlay( xxOtherLegs1 );
		            
		            xxOtherLegs2 = createMarker( point2_ , html2 );
		            mapOtherLegs.addOverlay( xxOtherLegs2 );
		            
		            xxOtherLegs3 = createMarker( point3_ , html3 );
		            mapOtherLegs.addOverlay( xxOtherLegs3 );
    		
		            // ==== Each time a point is found, extent the bounds to include it =====
                    bounds_.extend(point1_);
                    bounds_.extend(point2_);
                    bounds_.extend(point3_);
			}
					
			// === Create the polyline
			mapOtherLegs.addOverlay(new GPolyline(points_));//,color,line_width
			
			// === add the arrow head
			arrowHead(points_);
			midArrows(points_);
			
			// ===== determine the zoom level from the bounds =====
            mapOtherLegs.setZoom(mapOtherLegs.getBoundsZoomLevel(bounds_));

            // ===== determine the centre from the bounds ======
            mapOtherLegs.setCenter(bounds_.getCenter());
            
            // empty the lon/lat array
            //oneLeg_roundTrip.length = 0;

    	}
	}
//***********************************************************************************************
