// Copyright (C) 2009-2010 SomeonesGottaDoIt

// JavaScript Document
var map;
var geocoder;
var marker;
var mapdiv;
var mapInit = false;
var address;
var lat;
var lng;
var zoom;
var msg;
var container;

function initialise() {
	if (!GBrowserIsCompatible) return;
	// get the map display elements
	mapdiv = document.getElementById('MapDiv');
	var mc = document.getElementById('map_canvas');
	if (!mapdiv || !mc || arguments.length == 0) return;
	set_map_pos(arguments[0]);
	// create the map
	map = new GMap2(mc, {size:new GSize(425, 350)});
	// save the positional data (if supplied)
	if (arguments.length == 6) {
		lat = arguments[1];
		lng = arguments[2];
		zoom = arguments[3];
		msg = arguments[4];
		mapInit = arguments[5];
	}
}

function finalise() {
	if (GBrowserIsCompatible) GUnload();
}

function setMap(lat, long, zoom, msg) {
	var latlng = new GLatLng(parseFloat(lat), parseFloat(long));
	map.setCenter(latlng, parseInt(zoom, 10));
	map.addControl(new GSmallMapControl());
	if (!marker) {
		marker = new GMarker(latlng);
		map.addOverlay(marker);
	}
	else {
		marker.setLatLng(latlng);
	}
	if (msg != '') {
		marker.bindInfoWindowHtml('<p>' + msg + '</p>');
		marker.openInfoWindowHtml('<p>' + msg + '</p>');
	}
	mapInit = true;
}

function initMap(mlink) {
	if (!GBrowserIsCompatible) return;
	if (document.getElementById('latitude')) {
		// doing a database update
		if (document.getElementById('latitude').value == '' || document.getElementById('longitude').value == '') {
			address = document.getElementById('address1').value + "," + document.getElementById('address2').value + "," + document.getElementById('city').value + "," + document.getElementById('state').value + "," + document.getElementById('postcode').value + "," + document.getElementById('country').value;
			geocoder = new GClientGeocoder;
			geocoder.getLatLng(address, showAddress);
		}
		else {
			setMap(document.getElementById('latitude').value, document.getElementById('longitude').value, document.getElementById('zoom').value, document.getElementById('location').value);
			toggleMap(null);
		}
	}
	else {
		// on a location page, we should have already saved the required data
		setMap(lat, lng, zoom, msg);
		toggleMap(mlink);
	}
}

function showAddress(point) {
	if (!point) {
		alert("unable to find address: " + address);
		return;
	}
	setMap(point.lat(), point.lng(), parseInt(document.getElementById('zoom').value, 10), document.getElementById('location').value);
	document.getElementById('latitude').value = point.lat();
	document.getElementById('longitude').value = point.lng();
	toggleMap(null);
}

function resetMap() {
	mapInit = false;
	document.getElementById('latitude').value = '';
	document.getElementById('longitude').value = '';
}

function set_map_pos(container) {
	// figure out left position of map so that it is centered over container
	var c = document.getElementById(container);
	// 445 = width of map
	co = c.offsetLeft + (c.clientWidth - 445) / 2;
	mapdiv.style.left = co + "px";
}

function toggleMap(mlink) {
	if (!GBrowserIsCompatible) {
		alert("Sorry, your browser is not capable of displaying maps");
		return;
	}
	// is there a map?
	if (mapdiv) {
		// has it been initialised?
		if (!mapInit) {
			initMap(mlink);
			return;	// initMap will call us back when it's done
		}
		// toggle its visibility
		var s = mapdiv.style;
		if (s.display == "block") {
			s.display = "none";
			if (mlink) mlink.title = "Show Map";
		}
		else {
			s.display = "block";
			if (mlink) mlink.title = "Hide Map";
		}
	}
}

