var map;
var geocoder;
function initialize(address, subdomain, id) {
  map = new google.maps.Map2(document.getElementById("map"));
  var gto = new Object();
  gto.minResolution = 12;
  var mapControl = new GMapTypeControl();
  geocoder = new GClientGeocoder();  
  showAddressStartup(address, subdomain, id);
  map.addControl(mapControl);
  map.addControl(new GLargeMapControl3D());
  map.addControl(new GScaleControl());
  //map.enableScrollWheelZoom();    
  GEvent.addListener(map, "moveend", function() {
    load_data();
  });
  GEvent.addListener(map, "zoomend", function() {
    load_data();
  });
  $(document).ready(function() {
    if(id == "false") $.get('/home/categories', {}, '', 'script');
  });
  var minMapScale = 7; 
  var maxMapScale = 21; 
  // get array of map types 
  var mapTypes = map.getMapTypes(); 
  // overwrite the getMinimumResolution() and getMaximumResolution() methods for each map type 
  for (var i=0; i<mapTypes.length; i++) { 
    mapTypes[i].getMinimumResolution = function() {return minMapScale;} 
    mapTypes[i].getMaximumResolution = function() {return maxMapScale;} 
  }
  //load_data();
}


function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 13);
        }
      }
    );
  }
}
function showAddressStartup(address, subdomain, id) {
  if (id == "false") {
    var zoom = 5;
    if (address == "us") zoom = 4;
    if (subdomain) zoom = 13 
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (point) {
            map.setCenter(point, zoom);
          } else {
            map.setCenter(new google.maps.LatLng(44.316704068857426, 1.9335937500000013), zoom);
          }        
        }
      );
    }
  } else {
    map.setCenter(new google.maps.LatLng(44.316704068857426, 1.9335937500000013), 3);
  }
}
function load_coord() {
  $('#coord').html("Lat : " + map.getBounds().getCenter().lat() + "<br />Lng : " + map.getBounds().getCenter().lng() + "<br /> Zoom Level : " + map.getZoom());
}

function load_data() {
  $.get('/items/load', { zoom: map.getZoom(), lat_sw: map.getBounds().getSouthWest().lat(), 
                              lng_sw: map.getBounds().getSouthWest().lng(), lat_ne: map.getBounds().getNorthEast().lat(), 
                              lng_ne: map.getBounds().getNorthEast().lng() }, '', 'script');
}
function load_description(id) {
  //alert(id);
  $.get('/items/show/', { id: id }, '', 'script');
}

function add_item(value, category) {
  $.get('/items/add_item', {  id: value, category: category, lat: map.getBounds().getCenter().lat(), lng: map.getBounds().getCenter().lng() }, '', 'script');  
}
function select_type() {
  $.get('/items/select_item_type', {}, '', 'script');  
}
function change_type(value, category) {
  $.get('/items/change_value', { id: value, category: category, zoom: map.getZoom(), lat_sw: map.getBounds().getSouthWest().lat(), 
                              lng_sw: map.getBounds().getSouthWest().lng(), lat_ne: map.getBounds().getNorthEast().lat(), 
                              lng_ne: map.getBounds().getNorthEast().lng() }, '', 'script');  
}