
// show map (find lat & long from database first, if not found, get from address
function show_google_map(editable){
  map_editable = editable;  
  if (GBrowserIsCompatible()) {
    if (typeof(latitude) != "undefined" && typeof(longitude) != "undefined") {
      if (latitude <= 1 || longitude <= 1)
      {	  	
        show_map_address(map_number, map_road, map_district_ward_city);        
      }
      else
      {	
	//alert("point");
        show_map_point(new GLatLng (latitude, longitude));        
      }
    }
    else{      
      show_map_point(null);
    }
      
  }	  	   
}


//show map from a given address
function show_map_address(number_str, road_str, ward_district_city_str) {	
  var geocoder = new GClientGeocoder();

  if (typeof(number_str) != "undefined" && typeof(road_str) != "undefined" && typeof(ward_district_city_str) != "undefined"){
    // xy ly number dang 112-114 ...
    if (number_str.indexOf("-") > 0) {
      number_str = number_str.substr(0, number_str.indexOf("-"));
    }

    address_str = number_str + " " + road_str + ", " + ward_district_city_str;
    
    //get lat long from address and show map
    geocoder.getLatLng(address_str, function(new_point){show_map_point(new_point);});
  }
}


// input is a string only
function show_map_from_address(address){
  var geocoder = new GClientGeocoder();

  if (typeof(address) != "undefined"){
    //get lat long from address and show map
    geocoder.getLatLng(address, function(new_point){show_map_point(new_point);});
  }  
}


//show map at a given point
function show_map_point(point){
  var map = new GMap2(document.getElementById("map_canvas"));  
  map.setUIToDefault();	
  map.disableScrollWheelZoom();
  map.disableGoogleBar(); 
  
  //limit the zoom level
  if ((typeof(min_zoom_level) != "undefined") && (typeof(max_zoom_level) != "undefined")){
    // Get the list of map types      
    var mt = map.getMapTypes();
    // Overwrite the getMinimumResolution() and getMaximumResolution() methods
    for (var i=0; i<mt.length; i++) {
      mt[i].getMinimumResolution = function() {return min_zoom_level;}
      mt[i].getMaximumResolution = function() {return max_zoom_level;}
    }    
  }
  
  // default zoom level
  if (typeof(map_zoom_level) == "undefined") {
    map_zoom_level = 16;
  }
  
  if (!point) {
    //null point => display cannot find location
    point = new GLatLng (0, 0);
    map.setCenter(point, map_zoom_level);
    map.openInfoWindow(point, document.createTextNode("Không tìm thấy \"" + map_info + "\"  "));
  }
  else {
    //add original marker
    map.setCenter(point, map_zoom_level);
    marker = new GMarker(point);
    map.addOverlay(marker);	
    
    if (typeof(map_info) != 'undefined' && map_info != '' && map_info != null){
      map.openInfoWindow(point, document.createTextNode(map_info + "  "));
    }
    
    GEvent.addListener(marker, "click", function() {
      map.openInfoWindow(point, document.createTextNode(map_info + "  "));
    });	
		    
    if (map_editable == 1){
      setEditableMap(map, point);
    }
  
  }
  
  // drawing polyline
  if (typeof(polyline) != 'undefined') {
    map.addOverlay(polyline);
  }
  
}


function setEditableMap(map, point){
   //if editable add draggable marker
   // Create our drag marker icon	
   markerOptions = { draggable:true };		
   Dmarker = new GMarker(point, markerOptions);
   dIcon = Dmarker.getIcon();
  
   dIcon.infoWindowAnchor =  new GPoint(16, 32);
   newLatitude = Dmarker.getLatLng().lat()*1000000;
   newLongitude = Dmarker.getLatLng().lng()*1000000;
   set_input_value('google_edit_new_longitude', newLongitude); 
   set_input_value('google_edit_new_latitude', newLatitude );
   
   //event cho drag marker
   GEvent.addListener(Dmarker, "dragstart", function() {
     map.closeInfoWindow();
   })  

   //after drag => update new lat long
   GEvent.addListener(Dmarker, "dragend", function() {
     Dmarker.openInfoWindowHtml("Vị trí mới...");
	 newLatitude = Dmarker.getLatLng().lat()*1000000;
   	 newLongitude = Dmarker.getLatLng().lng()*1000000;
	 set_input_value('google_edit_new_longitude', newLongitude); 
	 set_input_value('google_edit_new_latitude', newLatitude );
   }); 

   //click on map also update lat long
   GEvent.addListener(map, "click", function(overlay, latlng) {
     if (latlng) { 
       Dmarker.setLatLng(latlng); 
 	   Dmarker.openInfoWindowHtml("Vị trí mới...");
	   newLatitude = Dmarker.getLatLng().lat()*1000000;
   	   newLongitude = Dmarker.getLatLng().lng()*1000000;
	   set_input_value('google_edit_new_longitude', newLongitude); 
	   set_input_value('google_edit_new_latitude', newLatitude );
     }		  
   });
   map.addOverlay(Dmarker);

}
