/**
 * Googlemaps wrapper class
 */
var GoogleMap = Class.create(
{
	/**
	 * Map object constructor
	 */
	initialize: function(sID, aOptions) 
	{
		this.sID 				= sID;
		this.aDefaultOptions 	= 
		{
			height				: 350,
			width				: 290,
			zoom				: 10,
			setCenterLatitude	: 51.695,
			setCenterLongitude	: 4.195,
			controls			: [],
			mapTypes			: [],
			controlPadding		: 50
		};
		this.aOptions 			= $H(this.aDefaultOptions).merge(aOptions);

		//Setup the map
		this.setup();
	},
	
	/**
	 * Setup the initial map parameters
	 */
	setup: function()
	{
		//Root element
		this.oRoot 				= $(this.sID);
		if (!this.oRoot) 
		{
			return;
		}
		
		//Set map layout
		this.oRoot.setStyle
		({
		      height			: this.aOptions.get('height') + 'px',
		      width				: this.aOptions.get('width') + 'px'
	    });
		
		this.addMap();
		this.addControls();
		
		//Create a GLatLngBounds object
		this.oBounds			= new GLatLngBounds();
		
		//Create geocode object
		this.oGeocoder 			= new GClientGeocoder();
		this.oGeocoder.setBaseCountryCode('.nl');
		
		//Marker array
		this.aMarker			= new Array();

		Event.observe(window, 'unload', GUnload);
	},
	
	/**
	 * Create the map
	 */
	addMap: function()
	{
		if (!GBrowserIsCompatible()) 
		{
			return;
		}

		window.onunload=function(){window.onunload;GUnload()};
		this.oMap = new GMap2(this.oRoot);
		this.oMap.setCenter(new GLatLng(this.aOptions.get('setCenterLatitude'), this.aOptions.get('setCenterLongitude')), this.aOptions.get('zoom'));
		this.oMap.enableScrollWheelZoom();
	},
	
	/**
	 * Add the controls to the map
	 */
	addControls: function()
	{
		this.aOptions.get('controls').each(function(oControl, iIndex)
		{
			this.oMap.addControl(oControl);
		}.bind(this));
		this.oMap.addControl(new RewisTerrainControl());
	},
	
	/**
	 * Add the controls to the map
	 */
	addMapTypes: function()
	{
		this.aOptions.get('mapTypes').each(function(oMapType, iIndex)
		{
			this.oMap.addMapType(oMapType);
		}.bind(this));
	},

	/**
	 * Geocode an address
	 */
	geocode: function(sAddress, oCallback)
	{
		this.oGeocoder.getLatLng(sAddress, oCallback);
	},
	
	/**
	 * Show a location on the map
	 */
	showLocation: function(fLatitude, fLongitude)
	{
		this.aOptions.set('setCenterLatitude', fLatitude);
		this.aOptions.set('setCenterLongitude', fLongitude);
		this.oMap.setCenter(new GLatLng(this.aOptions.get('setCenterLatitude'), this.aOptions.get('setCenterLongitude')), this.aOptions.get('zoom'));
	},
	
	/**
	 * Set the zoom leven of the map
	 */
	setZoomLevel: function(iZoomLevel)
	{
		this.aOptions.set('zoom', iZoomLevel);
		this.oMap.setZoom(iZoomLevel);
	},
	
	/**
	 * Add multiple markers to the map
	 * @param aMarker Array with GMarker objects
	 */
	addMarkers: function(aMarker)
	{
		aMarker.each
		(
			function(oMarker)
			{
				this.addMarker(oMarker);
			}, this
		)
	},
	
	/**
	 * Add a marker to the map
	 * @param oMarker GMarker object
	 */
	addMarker: function(oMarker)
	{
		this.aMarker.push(oMarker);
		this.oBounds.extend(oMarker.getLatLng());
		this.oMap.addOverlay(oMarker);
	},
	
	/**
	 * Get a marker from a GPoint
	 */
	getMarker: function(oPoint, oOptions)
	{
		var oMarker = new GMarker(oPoint, oOptions);
		return oMarker;		
	},
	
	/**
	 * Delete a marker object from the map
	 */
	deleteMarker: function(oMarker)
	{
		this.oMap.removeOverlay(oMarker);
	},
	
	/**
	 * Center the map view on all the markers
	 */
	centerOnMarkers: function()
	{
		this.oMap.setCenter(this.oBounds.getCenter());
	},
	
	/**
	 * Zoom the map view on all the markers
	 */	
	zoomOnMarkers: function(iZoomOutExtra)
	{
		var iZoomLevel	= this.oMap.getBoundsZoomLevel( this.oBounds );
		if(typeof iZoomOutExtra != 'undefined')
		{
			iZoomLevel = iZoomLevel - iZoomOutExtra;
		}
		this.setZoomLevel(iZoomLevel);
	},
	
	/**
	 * Get custom made icon
	 */
	getGoogleIcon: function(sPalSet, sIcon)
	{
	     var oBaseIcon 				= new GIcon();
	     oBaseIcon.iconSize			= new GSize(22,22);
	     oBaseIcon.iconAnchor		= new GPoint(11,22);
	     oBaseIcon.infoWindowAnchor	= new GPoint(11,0);
	     return new GIcon(oBaseIcon, '/images/gmaps_house.png', null, null);
	},
	
	/**
	 * This function will refresh the map state
	 * Usefull when showing a map in a div that was hidden and then shown 
	 */
	refresh: function()
	{
		this.oMap.checkResize();
	}
});

// A RewisTerrainControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).

// We define the function first
function RewisTerrainControl() {
}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object
RewisTerrainControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
RewisTerrainControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var satDiv = document.createElement("div");
  this.setButtonStyle_(satDiv);
  container.appendChild(satDiv);
  satDiv.appendChild(document.createTextNode("Satelliet"));
  GEvent.addDomListener(satDiv, "click", function() {
    map.setMapType(G_SATELLITE_MAP);
  });

  var mapDiv = document.createElement("div");
  this.setButtonStyle_(mapDiv);
  container.appendChild(mapDiv);
  mapDiv.appendChild(document.createTextNode("Kaart"));
  GEvent.addDomListener(mapDiv, "click", function() {
    map.setMapType(G_NORMAL_MAP);
  });

  var bothDiv = document.createElement("div");
  this.setButtonStyle_(bothDiv);
  container.appendChild(bothDiv);
  bothDiv.appendChild(document.createTextNode("Beide"));
  GEvent.addDomListener(bothDiv, "click", function() {
    map.setMapType(G_HYBRID_MAP);
  });

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
RewisTerrainControl.prototype.getDefaultPosition = function() {
  var canvasSize = $('map_canvas').getDimensions().width;
  if(canvasSize > 1)
  {
	  canvasSize = canvasSize / 2 - 125;
  }
  else
  {
	  canvasSize = 50;
  }
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(canvasSize, 15));
}

// Sets the proper CSS for the given button element.
RewisTerrainControl.prototype.setButtonStyle_ = function(button) {
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.fontWeight = "bold";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginLeft = "12px";
  button.style.textAlign = "center";
  button.style.width = "70px";
  button.style.cursor = "pointer";
  button.style.cssFloat = "left";
  button.style.styleFloat = "left";
}

