
// the map in the search panel
var GoogleMapSearch = {

  map: null,

  load: function() 
  {
    //Prevent error message
    if (typeof(google.maps.Icon) == "undefined") return;
    
    if (!google.maps.BrowserIsCompatible()) {
      Spif.ClassNameAbstraction.add(document.body, "incompatibleBrowser");
      return;
    }
    
    var canvas = document.getElementById("googleMapSearchCanvas");
    if (!canvas)
    {
      return;
    }
    var map = GoogleMapSearch.map = new google.maps.Map2(canvas);
    
    // enable various features on the map
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
    
    // add controls
		var overviewMapControl = new google.maps.OverviewMapControl();		
		map.addControl(new google.maps.SmallMapControl());
		map.addControl(overviewMapControl);
		overviewMapControl.hide(true);
    
    var theSize = map.getSize();
		if (theSize.width != 0) 
		{
			// set default view
			var googleMapBounds = window.location.href.match('[&|\?]google-map-bounds=(.[^&]*)&');
			var bounds, sw, ne, rect;
			if (googleMapBounds && googleMapBounds[1] && googleMapBounds[1].indexOf(",") != -1)
			{
				bounds = unescape(googleMapBounds[1]).split(",");
				sw = new google.maps.LatLng(bounds[0], bounds[1]);
				ne = new google.maps.LatLng(bounds[2], bounds[3]);
				rect = new google.maps.LatLngBounds(sw, ne);
				map.setCenter(rect.getCenter(), 6);
			}
			else
			{
				map.setCenter(new google.maps.LatLng(45.95115, 10.371094), 3);
			}

			map.savePosition();
    }
    else
    {
			map.setCenter(new google.maps.LatLng(85, 85), 18);
    }
    
    GoogleMapUtils.setMinMaxZoom(3, 15);
    
		// listen to events
		var handleZoom = google.maps.Event.addListener(map, "zoomend", GoogleMapSearch.handleZoom);
		var handleMove = google.maps.Event.addListener(map, "moveend", GoogleMapSearch.handleMove);
		var handleDrag = google.maps.Event.addListener(map, "dragend", GoogleMapSearch.handleDrag);
    
    // fix glitch in map position by initially showing the map, then hiding it onload
    Spif.ClassNameAbstraction.replace(
      document.getElementById('search-full-1-mapContainer'), 'show-googlemap', 'show-flashmap');
          
  },
  
  reset: function(href)
  {
    var bounds = document.getElementById('googleMapSearchBounds');
    var zoom = document.getElementById('googleMapSearchZoom');
    bounds.value = zoom.value = '';
    GoogleMapSearch.map.returnToSavedPosition();
    ClickTracker.trackClick(new Click(resources.messages.clicktracker.ct_search_ca_resetmap));
  },
  
  resetForFlash: function()
  {
    var bounds = document.getElementById('googleMapSearchBounds');
    var zoom = document.getElementById('googleMapSearchZoom');
    bounds.value = zoom.value = '';
  },
  
  getCurrentPosition: function()
  {
    var bounds = GoogleMapSearch.map.getBounds();
    var SWLatLng = bounds.getSouthWest();
    var NELatLng = bounds.getNorthEast();
    
    var SW = { lat: SWLatLng.lat(), lng: SWLatLng.lng() };
    var NE = { lat: NELatLng.lat(), lng: NELatLng.lng() };
    
    return { SW: SW, NE: NE };
  },
  
  handleZoom: function(map) {
    GoogleMapSearch.handleViewChange();
    ClickTracker.trackClick(new Click(resources.messages.clicktracker.ct_search_ca_zoommap));
  },  

  handleMove: function(map) {
  },  

  handleDrag: function(map) {
    GoogleMapSearch.handleViewChange();
    ClickTracker.trackClick(new Click(resources.messages.clicktracker.ct_search_ca_dragmap));
  },  

  handleViewChange: function(map)
  {
    //Reset Flash country info
    if (countryAndArea) countryAndArea.doResetForMap();
    
    var boundsEl = document.getElementById('googleMapSearchBounds');
    var zoomEl = document.getElementById('googleMapSearchZoom');
    var bounds = GoogleMapSearch.getCurrentPosition();
    boundsEl.value = [bounds.SW.lat, bounds.SW.lng, bounds.NE.lat, bounds.NE.lng].join(",");
    zoomEl.value = GoogleMapSearch.map.getZoom();
    
    // fires search to get new totals
    dataCollector.getTotals();
  },
  
	checkResize: function()
	{
		var theSize = GoogleMapSearch.map.getSize();
		if (theSize.width == 0) 
		{
			GoogleMapSearch.map.checkResize();
			
			// set default view
			var googleMapBounds = window.location.href.match('[&|\?]google-map-bounds=(.[^&]*)&');
			var bounds, sw, ne, rect;
			if (googleMapBounds && googleMapBounds[1] && googleMapBounds[1].indexOf(",") != -1)
			{
				bounds = unescape(googleMapBounds[1]).split(",");
				sw = new google.maps.LatLng(bounds[0], bounds[1]);
				ne = new google.maps.LatLng(bounds[2], bounds[3]);
				rect = new google.maps.LatLngBounds(sw, ne);
				GoogleMapSearch.map.setCenter(rect.getCenter(), 6);
			}
			else
			{
				GoogleMapSearch.map.setCenter(new google.maps.LatLng(45.95115, 10.371094), 3);
			}
	    
			GoogleMapSearch.map.savePosition();
	  }
	}
};