googlemaps_geocoder=new google.maps.Geocoder();

function googlemaps_init_map(div_id, zoom, centerLat, centerLng, mapTypeId, maxMarkers, lat_value_object, lng_value_object, autozoom)
{

 if((centerLat!=null)&&(centerLng!=null)){latlng = new google.maps.LatLng(centerLat, centerLng);}else{ latlng=null; }
 var mapOptions = {
      zoom: zoom,
      mapTypeId: mapTypeId 
    };
 map=new google.maps.Map(document.getElementById(div_id), mapOptions);
 if(centerLat!=null){googlemaps_set_center(map, centerLat, centerLng);}
 map.googlemaps_markersArray=[];
 map.maxMarkers=maxMarkers;
 map.googlemaps_infoWindowsArray=[];
 map.lat_value_object=lat_value_object;
 map.lng_value_object=lng_value_object;
 map.lat_min = null;
 map.lat_max = null;
 map.lng_min = null;
 map.lng_max = null;
 map.autozoom=autozoom;
 return map;    
}   

function googlemaps_add_marker(map, lat, lng, title, info, draggable, lat_value_object, lng_value_object)
  {                                         
      if(map.maxMarkers>map.googlemaps_markersArray.length)
      { 
        if((map.lat_min == null)||(map.lat_min>lat)){map.lat_min=lat;}
        if((map.lat_max == null)||(map.lat_max<lat)){map.lat_max=lat;}
        if((map.lng_min == null)||(map.lng_min>lng)){map.lng_min=lng;}
        if((map.lng_max == null)||(map.lng_max<lng)){map.lng_max=lng;}

       var latlng = new google.maps.LatLng(lat, lng);
       var marker = new google.maps.Marker({
       map:map,
       position: latlng,
       title:title,
       draggable:draggable
       });
       map.googlemaps_markersArray.push(marker);  
       if(lat_value_object!=null){lat_value_object.value=lat;}
       if(lng_value_object!=null){lng_value_object.value=lng;}
       if(map.lat_value_object!=null){map.lat_value_object.value=lat;}
       if(map.lng_value_object!=null){map.lng_value_object.value=lng;}
       
        if(draggable){
        if(lat_value_object!=null){marker.lat_value_object=lat_value_object;}
        if(lng_value_object!=null){marker.lng_value_object=lng_value_object;}
        google.maps.event.addListener(marker, 'mouseup', function(event) {
        var ll = event.latLng;
        if(lat_value_object!=null){lat_value_object.value=ll.lat();}
        if(lng_value_object!=null){lng_value_object.value=ll.lng();}
        if(map.lat_value_object!=null){map.lat_value_object.value=ll.lat();}
        if(map.lng_value_object!=null){map.lng_value_object.value=ll.lng();}
        });
        }
       
       if(info!=""){googlemaps_attachInfo(map, marker, info);}
      
       if(map.autozoom){googlemaps_autoZoom(map);}
      }
  }
 
 function googlemaps_setZoom(map, zoom){ map.setZoom(zoom); }

 function googlemaps_autoZoom(map)
 {
  if(map.lat_min!=null)
  {
   map.setCenter(new google.maps.LatLng(
    ((map.lat_max + map.lat_min) / 2.0),
    ((map.lng_max + map.lng_min) / 2.0)
    ));

   if( map.googlemaps_markersArray.length > 1)
   {
   map.fitBounds(new google.maps.LatLngBounds(
   //bottom left
   new google.maps.LatLng(map.lat_min, map.lng_min),
  //top right
   new google.maps.LatLng(map.lat_max, map.lng_max)
   ));
   }
   if(map.zoom>10){ googlemaps_setZoom(map, 10); }      
  }
 }

 function googlemaps_add_marker_to_map_center(map, title, info, draggable, lat_value_object, lng_value_object)
 {      
  googlemaps_add_marker(map, map.center.lat(), map.center.lng(), title, info, draggable, lat_value_object, lng_value_object);
 }
  
 function googlemaps_add_marker_by_address(map, address, title, info, draggable, lat_value_object, lng_value_object)
  {
      if(map.maxMarkers>map.googlemaps_markersArray.length)
      {
       googlemaps_geocoder.geocode( { 'address': address}, function(results, status) {
       if (status == google.maps.GeocoderStatus.OK) {
       googlemaps_add_marker(map, results[0].geometry.location.lat(), results[0].geometry.location.lng(), title, info, draggable, lat_value_object, lng_value_object);
      } else {
        // NO RESULT
      }
      });
     }       
  }
 
 function googlemaps_get_address_latlng(map, address, callback_function)
  {

       googlemaps_geocoder.geocode( { 'address': address}, function(results, status) {
       if (status == google.maps.GeocoderStatus.OK) {
       callback_function.call(this, results[0].geometry.location.lat(), results[0].geometry.location.lng());
      } else {
        // NO RESULT
      }
      });
  }


 function googlemaps_attachInfo(map, marker, info)
  {
     var infowindow = new google.maps.InfoWindow({ content: info});
     google.maps.event.addListener(marker, 'click', function() {
     googlemaps_close_info_windows(map);
     infowindow.open(map, marker);
     map.googlemaps_infoWindowsArray.push(infowindow);          
  });
  }
  
    
  function googlemaps_set_center_by_address(map, address)
  {
    googlemaps_geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
      } else {
        // NO RESULT
      }
      });      
  }
    
  function googlemaps_set_center(map, lat, lng)
  {
        var latlng = new google.maps.LatLng(lat, lng);
        map.setCenter(latlng);
  }
  

  function googlemaps_delete_markers(map)
  {
  if (map.googlemaps_markersArray) {
    for (i in map.googlemaps_markersArray) {
      map.googlemaps_markersArray[i].setMap(null);
      if(map.googlemaps_markersArray[i].lat_value_object!=null){map.googlemaps_markersArray[i].lat_value_object.value='';}
      if(map.googlemaps_markersArray[i].lng_value_object!=null){map.googlemaps_markersArray[i].lng_value_object.value='';}
    }
    map.googlemaps_markersArray.length = 0;
  }
  if (map.googlemaps_infoWindowsArray) {
    for (i in map.googlemaps_infoWindowsArray) {
      map.googlemaps_infoWindowsArray[i]=null;
    }
    map.googlemaps_infoWindowsArray.length = 0;
  }
  
  if(map.lat_value_object!=null){map.lat_value_object.value='';}
  if(map.lng_value_object!=null){map.lng_value_object.value='';}
  
  map.lat_min = null;
  map.lat_max = null;
  map.lng_min = null;
  map.lng_max = null;
   
  }

  function googlemaps_close_info_windows(map)
  {
  if (map.googlemaps_infoWindowsArray) {
    for (i in map.googlemaps_infoWindowsArray) {
      map.googlemaps_infoWindowsArray[i].close();
    }
  }
  }
  
  function googlemaps_draw_circle(map, center, radius, strokeColor, strokeOpacity, strokeWeight, fillColor, fillOpacity)
  {
  var options = {
      strokeColor: strokeColor,
      strokeOpacity: strokeOpacity,
      strokeWeight: strokeWeight,
      fillColor: fillColor,
      fillOpacity: fillOpacity,
      map: map,
      center: center,
      radius: radius
    };
    var circle = new google.maps.Circle(options);
  }
  
  function googlemaps_draw_rectangle(map, bounds, strokeColor, strokeOpacity, strokeWeight, fillColor, fillOpacity)
  {
    var options = {
      strokeColor: strokeColor,
      strokeOpacity: strokeOpacity,
      strokeWeight: strokeWeight,
      fillColor: fillColor,
      fillOpacity: fillOpacity,
      map: map,
      bounds: bounds
    };
    var rectangle = new google.maps.Rectangle(options);
    rectangle.setOptions(options);
      rectangle.setBounds(bounds);


  }
  
  function googlemaps_get_map_center(map)
  {
   return map.getCenter();
  }
