var map;
var vertexMarkers=new Array();
var popupMenu;
//Create google map v3
 var ib;
var geocoder;
var centermarker;
function init() {
	geocoder = new google.maps.Geocoder();

       map = new google.maps.Map(document.getElementById('map'), {
          'zoom': 13,
          'center': new google.maps.LatLng(21.001189,105.822372),
          'mapTypeId': google.maps.MapTypeId.TERRAIN,
		  'navigationControlOptions':{position:google.maps.ControlPosition.TOP_RIGHT}
        });
		//var poly=new PolylineEditor(map,vertexMarkers);
		// Create new control to display latlng and coordinates under mouse.
		//map.getPanes.overlayShadow.style.display = "none"; 
		var icon = new google.maps.MarkerImage("imagedir/draw.png",
     	 // This marker is 20 pixels wide by 32 pixels tall.
      	new google.maps.Size(10, 10),
      // The origin for this image is 0,0.
      	new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      	new google.maps.Point(5, 5));
	 
		centermarker= new google.maps.Marker({
              icon:icon
             
          });
		
		////////////////////////////////////////////////////
		
	var allBoundM=[new google.maps.LatLng(18.477213,105.0909),new google.maps.LatLng(18.570316,105.461002),new google.maps.LatLng(18.408167,105.519367),new google.maps.LatLng(18.30259,105.141712)];
	
	var allBound = new google.maps.Polygon({
		  strokeColor: "#FF0000",
    	  strokeOpacity: 0.8,
   		  strokeWeight: 0,
          fillColor: "FFFFFF",
          fillOpacity: 0.01,
          map: map
        });
	  allBound.setPaths(allBoundM);
	   google.maps.event.addListener(allBound, 'mousemove', function(mEvent) {
			//alert("bound");															
         // overalPolyZone.setOptions({ fillOpacity:0.01});
		  popupMenu.setText("?");
		 for(var i=0;i<allPolygons.length;i++){
			allPolygons[i].setOptions({ fillOpacity:0.01});
		 }
        });
	   
	   
	   
	   
	   /////////////////////////////////////////////////////////////
	
      var overalZoneM=[new google.maps.LatLng(20.958234,105.759888),new google.maps.LatLng(20.914945,105.857735),new google.maps.LatLng(20.96176,105.892067),new google.maps.LatLng(21.071045,105.909233),new google.maps.LatLng(21.14247,105.842285),new google.maps.LatLng(21.157839,105.781517),new google.maps.LatLng(21.065919,105.700493),new google.maps.LatLng(21.005997,105.711823)];
	  
	  
	  
//  ,new google.maps.LatLng(21.02138,105.867348)	 
	  
	  var contentString = "";
        
/********************************* Mennonite Central Committee - MCC (USA) *******/ 
var poly1=AddPolygon(map,overalZoneM,"#00BBFF",6,"BBBB00",contentString,12,"Mennonite Central Committee - MCC (USA)");
var cautreoM= [new google.maps.LatLng(20.987727,105.783577),new google.maps.LatLng(20.985803,105.771217),new google.maps.LatLng(20.976187,105.769501),new google.maps.LatLng(20.965287,105.769501),new google.maps.LatLng(20.959837,105.780144),new google.maps.LatLng(20.963043,105.793533),new google.maps.LatLng(20.972981,105.800743)];
var cautreoC="<iframe src ='web/cautreo.html' width='99%' height='99%'></iframe>";
var cautreo=AddPolygon(map,cautreoM,"#FF2200",2,"#FFAA00",cautreoC,14,"Research on Resettlement [Mennonite Central Committee - MCC (USA)]");
	
/*************************** Danish Embassy *********************************/
var sonkimM=[new google.maps.LatLng(21.01433,105.842972),new google.maps.LatLng(21.013048,105.840225),new google.maps.LatLng(21.005356,105.839195),new google.maps.LatLng(21.001189,105.841942),new google.maps.LatLng(20.998625,105.846405),new google.maps.LatLng(21.004074,105.850868),new google.maps.LatLng(21.008881,105.852585),new google.maps.LatLng(21.014009,105.850525)];
var sonkimC="Danish Embassy - Research on Privatization of Public Space";
	var sonkim=AddPolygon(map,sonkimM,"#FF0000",2,"#FFAA00",sonkimC,14,"Research on Privatization of Public Space [Danish Embassy]");
	
/**********************Caritas (1) & CRS (2) ****************/
var ubndM= [new google.maps.LatLng(21.025226,105.813446),new google.maps.LatLng(21.025226,105.80864),new google.maps.LatLng(21.022342,105.804863),new google.maps.LatLng(21.017534,105.803833),new google.maps.LatLng(21.012407,105.805893),new google.maps.LatLng(21.009202,105.808983),new google.maps.LatLng(21.011125,105.813789),new google.maps.LatLng(21.016573,105.818596),new google.maps.LatLng(21.020419,105.815506)];
var ubndC="<iframe src ='web/bql.html' width='99%' height='99%'></iframe>";
var ubnd=AddPolygon(map,ubndM,"#FF0000",1,"#FF0000",ubndC,17,"Employee Engagement for Sustainable Workplaces [Caritas (1) & CRS (2)]");

/**********************McKnight Foundation (USA) ****************/
var ttdhM=[new google.maps.LatLng(21.003112,105.811043),new google.maps.LatLng(21.001189,105.807266),new google.maps.LatLng(20.994779,105.804863),new google.maps.LatLng(20.988688,105.808983),new google.maps.LatLng(20.986765,105.816193),new google.maps.LatLng(20.994137,105.822716)];
var ttdhC="<img src='imagedir/TTDH.jpg'/>";
var ttdh=AddPolygon(map,ttdhM,"#FF0000",1,"#FF0000",ttdhC,17,"Reclaming Public Space [McKnight Foundation (USA)]");

/********************** CARE ****************/
var ttgtM=[new google.maps.LatLng(21.024905,105.860481),new google.maps.LatLng(21.024905,105.856361),new google.maps.LatLng(21.022021,105.854988),new google.maps.LatLng(21.019778,105.858421),new google.maps.LatLng(21.017534,105.860825),new google.maps.LatLng(21.022342,105.862541),new google.maps.LatLng(21.024905,105.862885)];
var ttgtC="";
var ttdh=AddPolygon(map,ttgtM,"#FF0000",1,"#FF0000",ttgtC,17,"Designing Effective Sustainable Behavior Change Programs [CARE]");

/******************* Irish Aid ************/
var kdlM=[new google.maps.LatLng(21.039966,105.813103),new google.maps.LatLng(21.037403,105.808296),new google.maps.LatLng(21.031635,105.809326),new google.maps.LatLng(21.030674,105.816536),new google.maps.LatLng(21.034519,105.821342),new google.maps.LatLng(21.040607,105.818253)];
var kdlC="<img src='imagedir/NGAN PHO 01 .jpg' style='width:662px;height:446px'/><br/><img src='imagedir/NGAN PHO 02 .jpg' style='width:662px;height:446px' />";
var kdl=AddPolygon(map,kdlM,"#FF0000",2,"#FFAA00",kdlC,15," Green Living for Kids [Irish Aid]");

/********************** HO HOAN KIEM ************************/
var hkM=[new google.maps.LatLng(21.032917,105.851555),new google.maps.LatLng(21.031315,105.848808),new google.maps.LatLng(21.02811,105.849152),new google.maps.LatLng(21.026508,105.851212),new google.maps.LatLng(21.025546,105.854645),new google.maps.LatLng(21.029392,105.855675)];
var hkC="<img src='imagedir/NGAN PHO 01 .jpg' style='width:662px;height:446px'/><br/><img src='imagedir/NGAN PHO 02 .jpg' style='width:662px;height:446px' />";
var hkl=AddPolygon(map,hkM,"#FF0000",2,"#FFAA00",hkC,15,"5'24H of Hanoi");

/**************** Fundeso (Spain) *************/
var mkM=[new google.maps.LatLng(20.999586,105.859108),new google.maps.LatLng(21.00183,105.864601),new google.maps.LatLng(20.998625,105.869408),new google.maps.LatLng(20.991573,105.867691),new google.maps.LatLng(20.990291,105.860138),new google.maps.LatLng(20.98965,105.854301),new google.maps.LatLng(20.99574,105.850525)];
var mkC="<img src='imagedir/NGAN PHO 01 .jpg' style='width:662px;height:446px'/><br/><img src='imagedir/NGAN PHO 02 .jpg' style='width:662px;height:446px' />";
var mkl=AddPolygon(map,mkM,"#FF0000",2,"#FFAA00",mkC,15,"Fighting against Urban Poverty [Fundeso (Spain)]");

/**************** Fundeso (Spain) *************/
var mk1M=[new google.maps.LatLng(20.947332,105.844345),new google.maps.LatLng(20.937072,105.837822),new google.maps.LatLng(20.932903,105.845718),new google.maps.LatLng(20.933865,105.857048),new google.maps.LatLng(20.940278,105.863228),new google.maps.LatLng(20.949897,105.858765),new google.maps.LatLng(20.951501,105.851898)];
var mk1C="<img src='imagedir/NGAN PHO 01 .jpg' style='width:662px;height:446px'/><br/><img src='imagedir/NGAN PHO 02 .jpg' style='width:662px;height:446px' />";
var mk1l=AddPolygon(map,mk1M,"#FF0000",2,"#FFAA00",mk1C,15,"Fighting against Urban Poverty [Fundeso (Spain)]");

/**************** Fundeso (Spain) *************/
var mk2M=[new google.maps.LatLng(21.035801,105.856361),new google.maps.LatLng(21.033558,105.854645),new google.maps.LatLng(21.032276,105.856361),new google.maps.LatLng(21.031635,105.858765),new google.maps.LatLng(21.032917,105.860138),new google.maps.LatLng(21.03516,105.859108),new google.maps.LatLng(21.036442,105.857048)];
var mk2C="<img src='imagedir/NGAN PHO 01 .jpg' style='width:662px;height:446px'/><br/><img src='imagedir/NGAN PHO 02 .jpg' style='width:662px;height:446px' />";
var mk2l=AddPolygon(map,mk2M,"#FF0000",2,"#FFAA00",mk2C,15,"Fighting against Urban Poverty [Fundeso (Spain)]");

/**************** Sida Environmental Fund (SEF - Sweden) *************/
var tqM=[new google.maps.LatLng(21.013048,105.818596),new google.maps.LatLng(21.014009,105.815506),new google.maps.LatLng(21.008881,105.812416),new google.maps.LatLng(21.006958,105.815849),new google.maps.LatLng(21.003433,105.819969),new google.maps.LatLng(21.009202,105.822716),new google.maps.LatLng(21.01465,105.820313)];
var tqC="<img src='imagedir/NGAN PHO 01 .jpg' style='width:662px;height:446px'/><br/><img src='imagedir/NGAN PHO 02 .jpg' style='width:662px;height:446px' />";
var tql=AddPolygon(map,tqM,"#FF0000",2,"#FFAA00",tqC,15,"GUONG – If a River Could Speak [Sida Environmental Fund (SEF - Sweden)]");

/**************** Thanh Xuan Organic Vegetables *************/
var nm1M=[new google.maps.LatLng(20.975545,105.77774),new google.maps.LatLng(20.973622,105.774307),new google.maps.LatLng(20.975545,105.771904),new google.maps.LatLng(20.976828,105.774307),new google.maps.LatLng(20.976828,105.77774)];
var nm1C="";
var nm1l=AddPolygon(map,nm1M,"#FF0000",2,"#FFAA00",nm1C,15,"Thanh Xuan Organic Vegetables [AusAid]");

/**************** Thanh Xuan Organic Vegetables *************/
var nm2M=[new google.maps.LatLng(21.149835,105.795937),new google.maps.LatLng(21.139588,105.792847),new google.maps.LatLng(21.129661,105.805206),new google.maps.LatLng(21.133824,105.818253),new google.maps.LatLng(21.141829,105.817223)];
var nm2C="";
var nm2l=AddPolygon(map,nm2M,"#FF0000",2,"#FFAA00",nm2C,15,"Thanh Xuan Organic Vegetables [AusAid]");

/**************** Neighbourhood Empowerment in Hanoi *************/
var nm3M=[new google.maps.LatLng(21.030033,105.858078),new google.maps.LatLng(21.026187,105.858421),new google.maps.LatLng(21.022662,105.860825),new google.maps.LatLng(21.024264,105.863228),new google.maps.LatLng(21.029392,105.862885)];
var nm3C="";
var nm3l=AddPolygon(map,nm3M,"#FF0000",2,"#FFAA00",nm3C,15," Neighbourhood Empowerment in Hanoi [IrishAid]");

/**************** Neighbourhood Empowerment in Hanoi *************/
var nm4M=[new google.maps.LatLng(20.996381,105.812416),new google.maps.LatLng(20.991573,105.814476),new google.maps.LatLng(20.991894,105.819969),new google.maps.LatLng(20.99542,105.820999),new google.maps.LatLng(20.998304,105.816879)];
var nm4C="";
var nm4l=AddPolygon(map,nm4M,"#FF0000",2,"#FFAA00",nm4C,15," Neighbourhood Empowerment in Hanoi [IrishAid]");

/**************** Neighbourhood Empowerment in Hanoi *************/
var nm5M=[new google.maps.LatLng(20.985483,105.843658),new google.maps.LatLng(20.98388,105.840912),new google.maps.LatLng(20.981316,105.840569),new google.maps.LatLng(20.981957,105.844345),new google.maps.LatLng(20.986124,105.845718)];
var nm5C="";
var nm5l=AddPolygon(map,nm5M,"#FF0000",2,"#FFAA00",nm5C,15," Neighbourhood Empowerment in Hanoi [IrishAid]");

/**************** No Motorbike day *************/
var nmM=[new google.maps.LatLng(20.988047,105.787697),new google.maps.LatLng(20.98356,105.785294),new google.maps.LatLng(20.977789,105.785294),new google.maps.LatLng(20.976187,105.791817),new google.maps.LatLng(20.980354,105.79937)];
var nmC="";
var nml=AddPolygon(map,nmM,"#FF0000",2,"#FFAA00",nmC,15,"No Motorbike day [World Bank]");

/**************** No Motorbike day *************/
var nm1M=[new google.maps.LatLng(21.020098,105.819626),new google.maps.LatLng(21.019778,105.822029),new google.maps.LatLng(21.016893,105.824089),new google.maps.LatLng(21.014971,105.822029),new google.maps.LatLng(21.016573,105.818596)];
var nm1C="";
var nm1l=AddPolygon(map,nm1M,"#FF0000",2,"#FFAA00",nm1C,15,"No Motorbike day [World Bank]");

/**************** Students Vote to Select Good Food Outlets *************/
var svM=[new google.maps.LatLng(21.000548,105.806236),new google.maps.LatLng(20.997022,105.803833),new google.maps.LatLng(20.988688,105.80658),new google.maps.LatLng(20.988688,105.814476),new google.maps.LatLng(20.99542,105.814819)];
var svC="";
var svl=AddPolygon(map,svM,"#FF0000",2,"#FFAA00",svC,15,"Students Vote to Select Good Food Outlets [World Bank]");
/************************/
var new1M=[new google.maps.LatLng(21.033097,105.788169),new google.maps.LatLng(21.032937,105.788147),new google.maps.LatLng(21.032616,105.788126),new google.maps.LatLng(21.032576,105.788362),new google.maps.LatLng(21.033077,105.788448)];
var new1C="";
var new1l=AddPolygon(map,new1M,"#FF0000",2,"#FFAA00",new1C,15,"Partnership for Community action on Climate change [Sida]");

var new2M=[new google.maps.LatLng(21.013588,105.799949),new google.maps.LatLng(21.013558,105.800185),new google.maps.LatLng(21.013809,105.800174),new google.maps.LatLng(21.013919,105.799906),new google.maps.LatLng(21.013769,105.799810)];
var new2C="";
var new2l=AddPolygon(map,mk12M,"#FF0000",2,"#FFAA00",new2C,15,"Partnership for Community action on Climate change [Sida]");
//////////////////////////////////////////////////////////////////////
	 
	
	
	google.maps.event.addListener(centermarker, 'click', function(event) {
		  //alert("xxx");
		  if(g_isDrawing==1){
			//  alert(event.latLng);
			  createClickedLines(centermarker.getPosition());
		  }
		
        });
	
	google.maps.event.addListener(centermarker, 'rightclick', function(event) {
		  //alert("xxx");
		  if(g_isDrawing==1) clearAllMarkersAndLine();
		
        });
	
	
	// Create new control to display latlng and coordinates under mouse.
      
        popupMenu = new PopupMenuClass(map);
        // Register event listeners
        google.maps.event.addListener(map, 'mouseover', function(mEvent) {
		  if(g_isDrawing!=1) popupMenu.set('visible', true);
		
        });
        google.maps.event.addListener(map, 'mouseout', function(mEvent) {
			if(g_isDrawing!=1) popupMenu.set('visible', false);
        });
        google.maps.event.addListener(map, 'mousemove', function(mEvent) {
          if(g_isDrawing==1){ 
		  	centermarker.setPosition(mEvent.latLng);
			
		  }else
		  	popupMenu.updatePosition(mEvent.latLng);
		 // alert(mEvent.latLng);
        });
		
		


		/*google.maps.event.addListener(map, 'center_changed', function(mEvent) {
          	centermarker.setPosition(map.getCenter());
        });
	    
	  /* var Poly = new ControlLabelClass(map,dosomething);
    
        // Register event listeners
        google.maps.event.addListener(map, 'rightclick', function(mEvent) {
          Poly.updatePosition(mEvent.latLng);
		  Poly.set('visible', true);
        });
       /* google.maps.event.addListener(map, 'mouseout', function(mEvent) {
          Poly.set('visible', false);
        });*/
       /* google.maps.event.addListener(map, 'mousemove', function(mEvent) {
          Poly.updatePosition(mEvent.latLng);
        });*/
	   
                
       
	//	alert("vvzz");
        
		createLabel(map,new google.maps.LatLng(18.4193027143544, 105.27171661376951),"KHU KINH TẾ CẦU TREO");
	 	createLabel(map,new google.maps.LatLng(18.403932,105.159178),"KCN Cầu Treo");
		createLabel(map,new google.maps.LatLng(18.440877,105.248485),"TT Hành Chính");
		createLabel(map,new google.maps.LatLng(18.503725,105.315836),"Khu Du lịch<br/> sinh thái");
   
 }
var allPolygons = new Array();
//var currentPolygon=-1;
var infowindow = new google.maps.InfoWindow({});
function createLabel(map,pos,text)
{
	var labelText = "<b style='font: bold 120% Tahoma;color:black;position:absolute;left:20px;top:19px;width:100px'>"+text+"</b><b 	style='font: bold 120% Tahoma;color:black;position:absolute;left:20px;top:21px;width:100px'>"+text+"</b><b style='font: bold 120% Tahoma;color:black;position:absolute;left:19px;top:20px;width:100px'>"+text+"</b><b style='font: bold 120% Tahoma;color:black;position:absolute;left:21px;top:20px;width:100px'>"+text+"</b><b style='font: bold 120% Tahoma;color:#EEEEEE;position:absolute;left:20px;top:20px;width:100px'>"+text+"</b>";

        var myOptions = {
                 content: labelText
                ,boxStyle: {
                   border: "0px solid black"
                  ,textAlign: "center"
                  ,fontSize: "8pt"
                  ,width: "800px"
                 }
                ,disableAutoPan: true
                ,pixelOffset: new google.maps.Size(-25, 0)                      
                ,position:pos
                ,closeBoxURL: ""
                ,isHidden: false
                ,pane: "floatPane"
        };

        var ibLabel = new InfoBox(myOptions);           
        ibLabel.open(map);
}

function AddPolygon(map,overalZoneM,LineC,lineW,fillC,contentString,maxZoom,label){
	
	 var overalPolyZone = new google.maps.Polygon({
		  strokeColor: LineC,
    	  strokeOpacity: 0.6,
   		  strokeWeight: lineW,
          fillColor: fillC,
          fillOpacity: 0.01,
          map: map
        });
	  overalPolyZone.setPaths(overalZoneM);
	  
	  google.maps.event.addListener(overalPolyZone, 'click', function(event) {
		 if(g_isDrawing==1) return;
         if(map.getZoom()<=maxZoom){ 
		 // infowindow.setPosition(event.latLng);
		//  infowindow.setContent(contentString);
		 //setTextAndOpenInfoW(map,contentString,event.latLng);
		 document.getElementById("divInfoContent").innerHTML=contentString;
		 document.getElementById("divInfoPanel").style.display="block";	
		 
		//  infowindow.open(map);
		 }

        });
	  google.maps.event.addListener(overalPolyZone, 'mousemove', function(mEvent) {
		 // alert("zone");	
		 if(g_isDrawing!=1){
		 for(var i=0;i<allPolygons.length;i++){
			allPolygons[i].setOptions({ fillOpacity:0.01});
		 }
		 popupMenu.setText("..");
         if(map.getZoom()<=maxZoom){
			 overalPolyZone.setOptions({ fillOpacity:0.35});
			// popupMenu.updatePosition(mEvent.latLng,label);
			 popupMenu.setText(label);
		 }
		 }
        });
	 // currentPolygon=allPolygon.length;
	  allPolygons.push(overalPolyZone);
	  return overalPolyZone;
	
}



/**
       * PopupMenuClass class displays the LatLng and pixel coordinates
       * underneath the mouse within a container anchored to it.
       * @param {google.maps.Map} map Map to add custom control to.
       */
      function PopupMenuClass(map) {
        /**
         * Offset the control container from the mouse by this amount.
         */
        this.ANCHOR_OFFSET_ = new google.maps.Point(8, 8);
        
        /**
         * Pointer to the HTML container.
         */
		
        this.node_ = this.createHtmlNode_();
		this.text_="?";
        
        // Add control to the map. Position is irrelevant.
        map.controls[google.maps.ControlPosition.TOP].push(this.node_);
        
        // Bind this OverlayView to the map so we can access MapCanvasProjection
        // to convert LatLng to Point coordinates.
        this.setMap(map);
        
        // Register an MVC property to indicate whether this custom control
        // is visible or hidden. Initially hide control until mouse is over map.
        this.set('visible', false);
      }
      
      // Extend OverlayView so we can access MapCanvasProjection.
      PopupMenuClass.prototype = new google.maps.OverlayView();
      PopupMenuClass.prototype.draw = function() {};
      
      /**
       * @private
       * Helper function creates the HTML node which is the control container.
       * @return {HTMLDivElement}
       */
      PopupMenuClass.prototype.createHtmlNode_ = function() {
        var divNode = document.createElement('div');
        divNode.id = 'latlng-control';
        divNode.index = 100;
        return divNode;
      };
      
      /**
       * MVC property's state change handler function to show/hide the
       * control container.
       */
      PopupMenuClass.prototype.visible_changed = function() {
        this.node_.style.display = this.get('visible') ? '' : 'none';
      };
      
      /**
       * Specified LatLng value is used to calculate pixel coordinates and
       * update the control display. Container is also repositioned.
       * @param {google.maps.LatLng} latLng Position to display
       */
      PopupMenuClass.prototype.updatePosition = function(latLng,text) {
        var projection = this.getProjection();
        var point = projection.fromLatLngToContainerPixel(latLng);
        
        // Update control position to be anchored next to mouse position.
        this.node_.style.left = point.x + this.ANCHOR_OFFSET_.x + 'px';
        this.node_.style.top = point.y + this.ANCHOR_OFFSET_.y + 'px';
        
        // Update control to display latlng and coordinates.
        /*this.node_.innerHTML = [
          latLng.toUrlValue(4),
          '<br/>',
          point.x,
          'px, ',
          point.y,
          'px'
        ].join('');*/
		this.node_.innerHTML=this.text_;//+latLng.toUrlValue(4);
      };
	PopupMenuClass.prototype.setText = function(text) {
		this.text_=text;
		
	}
	
	
	////////////////////UTILITY
	
	function DisplayInfo(){
		document.getElementById("divInfoPanel").style.display="none";	
		
	}
	
	function viewArea()
	{
		//alert("xx");
		//alert(document.getElementById("gotoPoint").selectedIndex);
		switch(document.getElementById("gotoPoint").selectedIndex){
			case 0: setViewMapOfArea(new google.maps.LatLng(18.4493027143544, 105.31371661376951),12);
			break;
			case 1: setViewMapOfArea(new google.maps.LatLng(18.4421, 105.2636),14);
			break;
			case 2: setViewMapOfArea(new google.maps.LatLng(18.4994, 105.3159),14);
			break;
			case 3: setViewMapOfArea(new google.maps.LatLng(18.4371, 105.2523),16);
			break;
			case 5: setViewMapOfArea(new google.maps.LatLng(18.4434, 105.2483),16);
			break;
			case 4: setViewMapOfArea(new google.maps.LatLng(18.4413, 105.2521),16);
			break;
			case 6: setViewMapOfArea(new google.maps.LatLng(18.4027, 105.1637),14);
			break;
		}
			
	}
	
	function setViewMapOfArea(latLng,zoom)
	{
		
		map.setCenter(latLng);
		map.setZoom(zoom);
	}
	
	function codeAddress() {
    var address = document.getElementById("searchBox").value;
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          /*var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });*/
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
  }
var g_isDrawing=0;
var g_lineVertexs = new Array();
function clickOnCalculateDistance()
{
	if(g_isDrawing==0){
		document.getElementById("divCalDistance").style.border="2px solid #FF0000";	
		document.getElementById("divKmResult").style.display="block";
		g_isDrawing=1;
		centermarker.setMap(map);
		g_lineVertexs=[];
	}else {
		document.getElementById("divCalDistance").style.border="2px solid #000000";	
		document.getElementById("divKmResult").style.display="none";	
		
		g_isDrawing=0;
		//g_lineVertexs=[];
		clearAllMarkersAndLine();
		centermarker.setMap(null);
		
		//g_polyLine=null;
		

	}
	
}
var g_polyLine;
var g_polyLinePath=[];
function createClickedLines(pos)
{
	var icon = new google.maps.MarkerImage("imagedir/vertex.png",
     	 // This marker is 20 pixels wide by 32 pixels tall.
    new google.maps.Size(10, 10),
      // The origin for this image is 0,0.
     new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
     new google.maps.Point(5, 5));
	 
	 var vertexmarker= new google.maps.Marker({
              icon:icon,
			  map:map,
			  position:pos
             
          });
	g_polyLinePath.push(pos);
	 
	g_lineVertexs.push(vertexmarker);	
	if(g_polyLinePath.length>1){
		if(g_polyLinePath.length>2) g_polyLine.setMap(null);
		 
	    g_polyLine=new google.maps.Polyline({
			path:g_polyLinePath,
     		strokeColor: "#FF9900",
      		strokeOpacity: 0.8,
      		strokeWeight: 1,
			map:map
			});
		   
		document.getElementById("disContent").innerHTML=calPolyLineDistance();
	}
	//update info
	
}
function clearAllMarkersAndLine(){
	for(var i=0;i<g_lineVertexs.length;i++)
		g_lineVertexs[i].setMap(null);
	g_lineVertexs=[];
	g_polyLinePath=[];
	g_polyLine.setMap(null);
	document.getElementById("disContent").innerHTML="0";
}

function calPolyLineDistance()
{
	var distance=0;
	for(var i=0;i<g_lineVertexs.length-1;i++){
		lat1=g_lineVertexs[i].getPosition().lat();
		lng1=g_lineVertexs[i].getPosition().lng();
		lng2=g_lineVertexs[i+1].getPosition().lng();
		lat2=g_lineVertexs[i+1].getPosition().lat();
		distance=distance+calc_distance(lat1,lng1,lat2,lng2);
	}
	return Math.round(distance*1000);
}


function calc_distance(lat1, lng1, lat2, lng2){
		//alert("no erro");
		var R = 6371; // km
		var dLat = (lat2-lat1)*3.14159/180;
		var dLng = (lng2-lng1)*3.14159/180; 
		var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1*3.14159/180) * Math.cos(lat2*3.14159/180) * 
        Math.sin(dLng/2) * Math.sin(dLng/2); 
		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		var d = R * c;	
		return d;		
		
}	

