	var k = 0;	
	var wrkar = [];
	var map;
	var setor = 'ZZ'; 

	var distancia = new Number(); distancia=0;
	var distotal = new Number(); distotal=0;
	var tempo = new Number(); tempo=0;
	var tempototal = new Number(); tempototal=0;
	var consumo = new Number(); consumo=0;
	var consumototal = new Number(); consumototal=0;
	    
	function load() 
	{
      if (GBrowserIsCompatible()) 
	  {
			map = new GMap2(document.getElementById("map"), {draggableCursor: 'crosshair'});
			map.setCenter(new GLatLng(-24.315277,-47.013888), 12);					 
			map.setMapType(G_MAP_TYPE);											 
			var marker1 = new GMarker(new GPoint(-47.013888, -24.315277));
			map.addOverlay(marker1);
						
			GEvent.addListener(marker1, 'click', function() {
				marker1.openInfoWindowHtml("<div  style=\"white-space: nowrap;\">Peruíbe<br/>São Paulo, SP</div>");
			});
			//marker1.openInfoWindowHtml("<div  style=\"white-space: nowrap;\">Peruíbe<br/>São Paulo, SP</div>");
			
			map.addControl(new GLargeMapControl());									
			map.addControl(new GMapTypeControl());									
			map.addControl(new GOverviewMapControl());
			map.addControl(new google.maps.LocalSearch());								

			map.enableContinuousZoom();												
			map.enableScrollWheelZoom();
			
     function buscasetor( point1, point2 ){
     
      var LATITUDE1  =  point1.y;
      var LATITUDE2  =  point2.y;
      var LONGITUDE1 =  point1.x;
      var LONGITUDE2 =  point2.x;
      var SETOR = 'ZZ';      
      var DISTANCIA  = Math.abs((point1).distanceFrom(point2));
      var ALTURA     = Math.abs((new GLatLng(LATITUDE1, LONGITUDE2)).distanceFrom(point2));
      var SENO       = 0;      

      // Validando ALTURA e DISTÂNCIA

      if ( ALTURA == 0){
         SENO = 0;

      }else if ( DISTANCIA == 0 ){
         SENO = 90;

      }else{
         SENO = Math.abs(ALTURA / DISTANCIA);

      }
      
      //DEFINIDO O SETOR DO DO PONTOB

      if (       (LATITUDE1 < LATITUDE2) && (LONGITUDE1 < LONGITUDE2) ){
         SETOR='NE';

      }else if ( (LATITUDE1 < LATITUDE2) && (LONGITUDE1 == LONGITUDE2) ){
         SETOR='N';

      }else if ( (LATITUDE1 < LATITUDE2) && (LONGITUDE1 > LONGITUDE2) ){

         SETOR='NW';

      }else if ( (LATITUDE1 == LATITUDE2) && (LONGITUDE1 > LONGITUDE2) ){
         SETOR='W';		 

      }else if ( (LATITUDE1 > LATITUDE2) && (LONGITUDE1 < LONGITUDE2) ){
         SETOR='SE';

      }else if ( (LATITUDE1 == LATITUDE2) && (LONGITUDE1 < LONGITUDE2) ){
         SETOR='E';		 

      }else if ( (LATITUDE1 > LATITUDE2) && (LONGITUDE1 > LONGITUDE2) ){
         SETOR='SW';

      }else if ( (LATITUDE1 > LATITUDE2) && (LONGITUDE1 == LONGITUDE2) ){
         SETOR='S';

      }

	  else{
       SETOR='ZZ';

      }      

      /*var ANGULO = anguloBySeno( SENO );
      var RUMO = 0;      

      if( SETOR == 'N'){
         RUMO = 0;       

      }else if( SETOR == 'E'){
         RUMO = 90;

      }else if( SETOR == 'S'){
         RUMO = 180;

      }else if( SETOR == 'W'){
         RUMO = 270;

      }else if( SETOR == 'NE'){         
         RUMO = Math.abs(ANGULO - 90);

         if ( RUMO == 90 ){
            SETOR='E';

         } else if ( RUMO == 0 ){
            SETOR='N';

         } else if ( RUMO == 360 ){
            RUMO = 0;
            SETOR='N';

         }

	  }else if( SETOR == 'SE'){
         RUMO = ANGULO + 90; 

         if ( RUMO == 90 ){
            SETOR='E';

         } else if ( RUMO == 180 ){
            SETOR='S';

         }       

      }else if( SETOR == 'SW'){
         RUMO = 180 + Math.abs(ANGULO - 90);

         if ( RUMO == 180 ){

            SETOR='S';

         } else if ( RUMO == 270 ){
            SETOR='W';
         }       

      }else if( SETOR == 'NW'){
         RUMO = ANGULO + 270;

         if ( RUMO == 270 ){
            SETOR='S';

         } else if ( RUMO == 360 ){
            RUMO = 0;
            SETOR='N';
         }         
      }*/

      return SETOR ;
     }
			
			function distance(point1,point2)
			{
				var ponto1 = [];
				var ponto2 = [];
				
				ponto1[0]= point1.y*(Math.PI/180);
				ponto1[1]= point1.x*(Math.PI/180);
				ponto2[0]= point2.y*(Math.PI/180);
				ponto2[1]= point2.x*(Math.PI/180);
				
				dist = 2*Math.asin(Math.sqrt(Math.pow(Math.sin((ponto2[0]-ponto1[0])/2),2)+
												(Math.cos(ponto1[0])*Math.cos(ponto2[0])*
														Math.pow(Math.sin((ponto2[1]-ponto1[1])/2), 2))));
		
				distreal = dist * 6372.795;
				//tempo = dist / velocidade;
				//totalcon = consumo * tempo;
				return distreal;
			}
			
			function datadraw(point) 
			{
				if(k==1)
				{
					wrkar[0] = point;	
				}
				wrkar[1] = point;
				for (var i = 0; i < wrkar.length; i++)
				{
					map.addOverlay( new GPolyline(wrkar,"#ff0000", 3, 1) ) ;
					distancia = distance(wrkar[0],point);
					setor = buscasetor(wrkar[0],point);
					tempo = distancia / document.getElementById("velo").value;
					consumo = tempo * document.getElementById("cons").value; 					 
				}
				distotal = distotal + distancia;
				tempototal += tempo;
				consumototal += consumo; 
				wrkar[0] = point;
			}    

			function createMarker(point, number)
			{
				var marker = new GMarker(point, {draggable:false});
				var distlocal = distancia.toFixed(2);
				var dtotal = distotal.toFixed(2);
				var cons = consumo.toFixed(2);
				var constotal = consumototal.toFixed(2);
				var time = tempo.toFixed(2);
				var timetotal = tempototal.toFixed(2);
				var hora = Math.round(time);
				var horatotal = Math.round(timetotal);
				var min =  (time - hora) * 60;
				var mintotal =  (timetotal - horatotal) * 60;
				var clock = new Date();
				clock.setHours(hora);
				clock.setMinutes(min);
				var clocktotal = new Date();
				clocktotal.setHours(horatotal);
				clocktotal.setMinutes(mintotal);
				var setorlocal = setor; 
				
				min = clock.getMinutes();
				if (min<10){
					min = "0" + min;
				}
				
				mintotal = clocktotal.getMinutes();
				if (mintotal<10){
					mintotal = "0" + mintotal;
				}
				
				GEvent.addListener(marker, "click", 
				function() 
				{
					var myHtml = "Coordenadas: <br/>" 
									   + "latitude : " + point.y
									   + "<br/>longitude : " + point.x
									   + "<hr></hr>" 
									   + "<br/>Distância do ultimo ponto: " + distlocal + " km" 
									   + "<br/>Total percorrido: " + dtotal + " km"
									   + "<br/>Tempo gasto: " + clock.getHours() + ":" + min + " min"
									   + "<br/>Tempo total: " + clocktotal.getHours() + ":" + mintotal + " min" 
									   + "<br/>Consumo: " + cons + " lts"
									   + "<br/>Total consumido: " + constotal + " lts"
									   + "<br/>Sentido do vôo: " + setorlocal;
					map.openInfoWindowHtml(point, myHtml);
				});

				GEvent.addListener(marker,"dragend", 
				function() 
				{
					map.removeOverlay(marker);
				});

				return marker;
			}
		
			GEvent.addListener(map, "click", function(overlay, point) { 
			if (point) {
								
				k++;
				datadraw(point);
				map.addOverlay(createMarker(point, k));
								
			}
			});
	  }
	}

	function mapclear()
	{
		map.clearOverlays();
		k=0;
		distotal=0;
		tempototal=0;
		consumototal = 0;
	}
    
