/*
	MyKidsTime Google Maps integration
	Author: John Rix
	Copyright 2009
*/

var PROBLEM_TEXT = "Unfortunately, there is a problem accessing the Google Maps service and we can't display a map at this time.";
var ADDRESS_ZOOM = 15;
var DEFAULT_ZOOM = 6;

var MAP_WIDTH;
var MAP_HEIGHT;
var COUNTRY_CODE;
var DEFAULT_LOCATION;
var MAP_UPDATE;

var geocoder;
var zoomLevel;
var marker;
var map;

Window.onDomReady(function(){

	MAP_WIDTH = window.MKT_MAP_WIDTH ? window.MKT_MAP_WIDTH : 375;
	MAP_HEIGHT = window.MKT_MAP_HEIGHT ? window.MKT_MAP_HEIGHT : 250;
	COUNTRY_CODE = window.MKT_COUNTRY_CODE ? window.MKT_COUNTRY_CODE : 'ie';
	DEFAULT_LOCATION = (window.MKT_DEFAULT_LOCATION != null) ? window.MKT_DEFAULT_LOCATION : 'Ireland';
	MAP_UPDATE = window.MKT_MAP_UPDATE ? window.MKT_MAP_UPDATE : false;

//	showAddress(window.MKT_MAP_LOCATION, ADDRESS_ZOOM);
});

function showAddress(mapLocation, zoom)
{
	canvas = $('map_canvas');
	zoomLevel = zoom ? zoom : ADDRESS_ZOOM;

	try {
		if (!mapLocation.length) return noMapInfo(canvas);

		if (!GBrowserIsCompatible()) {
			canvas.innerHTML = PROBLEM_TEXT;
			return;
		}

		if (mapLocation.indexOf('<iframe') > -1 ) {
			canvas.innerHTML = mapLocation;
			canvas.firstChild.width = MAP_WIDTH;
			canvas.firstChild.height = MAP_HEIGHT;
		}
		else {
			if (!geocoder) {
				geocoder = new GClientGeocoder();
				geocoder.setBaseCountryCode(COUNTRY_CODE);
			}

			if (mapLocation.match(/^[\-0-9\.]+, [\-0-9\.]+/) ) {
				x = Number(mapLocation.substr(0, mapLocation.indexOf(',')));
				y = Number(mapLocation.substr(mapLocation.indexOf(',') + 2));

				point = new GLatLng(x, y, true);
				SetLocation(point);
			}
			else {
				geocoder.getLatLng(mapLocation, SetLocation);
			}
		}
	}
	catch (e) {
		canvas.innerHTML = PROBLEM_TEXT;
		//alert(e);
	}
}

function SetLocation(point) {
	//canvas.style.width = MAP_WIDTH + 'px';
	canvas.style.height = MAP_HEIGHT + 'px';

	if (!point) return noMapInfo(canvas);

	canvas.style.bgcolor = "black";

	map = new GMap2(canvas);
	map.setCenter(point, zoomLevel);

	marker = new GMarker(point);
	map.addOverlay(marker);
	var control = new GSmallZoomControl();
    var zoomPos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(3,38));
	map.addControl(control, zoomPos);
	control = new GMapTypeControl()
    var mapTypePos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5,15));
	map.addControl(control, mapTypePos);

	if (MAP_UPDATE) {
		GEvent.addListener(map, "click", OnMapClick);
	}
}

function noMapInfo(canvas)
{
	if (DEFAULT_LOCATION.length) {
		showAddress(DEFAULT_LOCATION, DEFAULT_ZOOM);
	}
	else {
		canvas.style.border = 'thin solid #AAAAAA';
		canvas.innerHTML = "<table width=\"100%\" height=\"100%\"><tr><td style=\"text-align:center;vertical-align:middle\">Map information is unavailable<br/> for this listing.</td></tr></table>";
	}
}

function OnMapClick(overlay, latlng) {

	if ($('map_location').readOnly || $('map_location').disabled)
		return;

	$('map_location').value = latlng.y + ', ' + latlng.x;

	marker.setLatLng(latlng);
}

//function isdefined( variable)
//{
//    return (typeof(window[variable]) == "undefined")?  false: true;
//}
