// Usage:
// new LocationMap(containerId)
//
// new LocationMap(containerId, params)
// Valid params:
//   centerLat, centerLng
//   zoom
//   pinLat, pinLng
//   editable
//
function LocationMap(containerId, param) {

	// default to view of Australia
	var defaultLat = -28.00;
	var defaultLng = 140.00;
	var defaultZoom = 3;

	//** Private members **
	var self = this;
	var centerLat, centerLng;
	var container
	var editable = false;
	var map;
	var pin;
	var pinLat, pinLng;
	var zoom;
	var contentID;

	//** Public properties **

	//** Private Methods **
	function init() {
		if(param) {
			if(param.centerLat) centerLat = param.centerLat;
			if(param.centerLng) centerLng = param.centerLng;
			if(param.zoom) zoom = param.zoom;
			if(param.pinLat) pinLat = param.pinLat;
			if(param.pinLng) pinLng = param.pinLng;
			if(param.editable) editable = true;
			if(param.contentID) contentID = param.contentID;
		}
		google.load('maps', 2);
		google.setOnLoadCallback(handleApiLoadCallback);
	}

	function handleApiLoadCallback() {
		if(typeof window.onunload == 'function') {
			var oldunload = window.onunload;
			window.onunload = function() {
				oldunload();
				google.maps.Unload();
			}
		}
		else {
			window.onunload = google.maps.Unload;
		}

		loadMap(containerId);
	}

	function handleGeocodeCallback(geoData) {

		if(!geoData) {
			alert('Geocoding failed.\n\nPlease try an alternative or less specific address');
			return false;
		}

		pinLat = geoData.lat();
		pinLng = geoData.lng();
		map.setZoom(15);
		updatePinPosition(pinLat, pinLng);
		updateMapPosition(pinLat, pinLng)
	}

	function handleMapMove() {
		centerLat = map.getCenter().lat(); 
		centerLng = map.getCenter().lng(); 
	}

	function handlePinDrag() {
		var point = pin.getLatLng();
		map.panTo(point);
		pinLat = point.lat();
		pinLng = point.lng();
	}

	function handleZoom(oldzoom, newzoom) {
		zoom = newzoom;
	}

	function loadMap(id) {

		
	   	map = new GMap2(document.getElementById(id));
		map.enableContinuousZoom();
		map.addControl((map.getSize().height > 280) ? new GLargeMapControl() : new GSmallMapControl());

		if((typeof centerLat == 'undefined') || (typeof centerLng == 'undefined')) {
			centerLat = defaultLat;
			centerLng = defaultLng;
		}
		if(typeof zoom == 'undefined') {
			zoom = defaultZoom;
		}

		map.setCenter(new GLatLng(centerLat, centerLng));
		map.setZoom(zoom);

		if(typeof pinLat != 'undefined' && typeof pinLng != 'undefined') {
			updatePinPosition(pinLat, pinLng);
		} else {
			updatePinPosition(centerLat, centerLng);
		}

		GEvent.addListener(map, "moveend", handleMapMove);
		GEvent.addListener(map, "zoomend", handleZoom);

		map.savePosition();

	}

	function updatePinPosition(lat, lng) {
		var point = new GLatLng(lat, lng);
		if(typeof pin == 'undefined') {
			if (editable) {
				pin = new GMarker(point, {draggable: true});
				GEvent.addListener(pin, "dragend", handlePinDrag);
			}
			else {
				pin = new GMarker(point, {draggable: false});
			}
			map.addOverlay(pin);
		}
		else {
			pin.setLatLng(point);
		}
		return true;
	}

	function updateMapPosition(lat, lng) {
		map.panTo(new GLatLng(lat, lng));
		return true;
	}

	//** Public methods **
	this.save = function() {
		if(!editable) return;
		// fade out the map
		kQuery('div#locationMapContainer_'+contentID).fadeTo('slow','0.0');
		// perform an ajax save
		ajaxURL='http://'+location.host+'/generateAjaxResponse.php?kLibrary=editItem&action=saveLocationMap&contentItemID='+contentID+'&pinLat='+pinLat+'&pinLng='+pinLng+'&centerLat='+centerLat+'&centerLng='+centerLng+'&zoom='+zoom;
		ajaxRequest=ajaxHandler.addRequest(ajaxURL, 'GET', null, 'saveMapResponseHandler')
		// fade it in
		kQuery('div#locationMapContainer_'+contentID).fadeTo('slow','1.0');
		// replace the icon with a successful one
		returnDOMObject('locationMapSaveImg_'+contentID).src = '/images/komodo/saveLocation2.png';
	}

	this.search = function(address) {

		if(!editable) return;
		if(!address) return;

		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address, handleGeocodeCallback);
	}

	init();
	return this;

}

function saveMapResponseHandler(ajaxRequest) {
	eval(ajaxRequest.responseText);
}
