/*
 * jsutherland
 * 
 * Handles google map implementation
 * See http://code.google.com/apis/maps/documentation/reference.html for complete documentation
 * 
 * @param lat: long for geoCode latitude of property
 * @param lng: long for geoCode longitude of property
 * @param zoom: defaults to 13 unless otherwise site by owner
 * @param showMarker: defaulted to false unless otherwise set to true by owner
 * @param mapControlLevel: if creates various types of controls. see function
 * @param isSetByOwner: whether this geocode was actually chosen by the owner using the mapping widget, if false, then it was created some other way not known to the owner of the listing
 * @param markerTitle: the title for the property "pin" marker
 */

$j(window).unload(function(){
	GUnload();
});

ha.map = {
		
	init: function(mapData){
      	if (GBrowserIsCompatible()) {
      		// wire up some global map variables
      		// this implementation is for one map this will need to change if there are multi maps
      		// initializing as the vars are global and will get overwritten
      		
      		// get the map
      		ha.map.getMap(mapData);
      	}
	},
    
	getMap: function(mapData) {
		var map = new GMap2(document.getElementById("property-map"));
		var latLng = new GLatLng(mapData.latitude, mapData.longitude);
		
		var mapTypes = map.getMapTypes();
		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMaximumResolution = function() {return mapData.maxZoomLevel;}
		}
		
		if (mapData.showExactLocation) ha.map.setMarker(map, mapData);
		
  		ha.map.setControlLevel(map, mapData.controlLevel);
		
		map.setCenter(latLng, 13);
	},
	
	setControlLevel: function(map, controlLevel){
		// level 4: creates map type buttons (hybrid, sattelite), creates a control with buttons to pan in four directions, zoom in and zoom out, and a zoom slider
		// level 3: creates map type buttons (hybrid, sattelite), creates a control with buttons to pan in four directions, and zoom in and zoom out
		// level 2: creates a control with buttons to pan in four directions, and zoom in and zoom out
		// level 1: creates a control with buttons to zoom in and zoom out.
		
		if (controlLevel >= 3){
			var mapTypeControl = new GMapTypeControl();
			var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
			var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
			map.addControl(mapTypeControl, topRight);
			
			if (controlLevel == 4) map.addControl(new GLargeMapControl());
			else map.addControl(new GSmallMapControl());
			
			GEvent.addListener(map, "dblclick", function() {
				map.removeControl(mapTypeControl);
				map.addControl(new GMapTypeControl(), bottomRight);
			});
		} else if (controlLevel == 2){
			map.addControl(new GSmallMapControl());
		} else if (controlLevel == 1){
			map.addControl(new GSmallZoomControl());
		}
		
	},
	
	setMarker: function(map, mapData) {
		var marker = new GMarker(new GLatLng(mapData.latitude, mapData.longitude), { title: mapData.markerTitle, draggable:false, clickable:false });
        GEvent.addListener(marker, "dragend", function() {
            geoLatitude = marker.getPoint().lat();
            geoLongitude = marker.getPoint().lng();
            geoMap.setCenter(new GLatLng(geoLatitude, geoLongitude));
            geoPrecision = mapData.precision;
        });
        
        map.addOverlay(marker);
	}
};



