// useful load event function written by Simon Willison (http://simon.incutio.com/archive/2004/05/26/addLoadEvent)
function addLoadEvent(func){
	var oldonload=window.onload;
	if (typeof window.onload != 'function') window.onload=func;
	else{
		window.onload=function (){
			oldonload();
			func();
		}
	}
}

// create elements function to circumvent XHTML/DOM compliance
function createElement(element){
	if (typeof document.createElementNS != 'undefined')
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	if (typeof document.createElement != 'undefined')
		return document.createElement(element);
	return false;
}

function insertAfter(newElement,targetElement){
	var parent=targetElement.parentNode;
	if (parent.lastChild == targetElement) parent.appendChild(newElement);
	else parent.insertBefore(newElement,targetElement.nextSibling);
}

function stopDefaultEvent(e){
	if (window.event){
		window.event.cancelBubble=true;
		window.event.returnValue=false;
	}
	if (e && e.preventDefault && e.stopPropagation){
		e.preventDefault();
		e.stopPropagation();
	}
}



/* --- Google Maps functions ---
*/

var baseIcon, map, map_overlay=null, map_point, map_html;

function initMap(){
	setTimeout("if (typeof GMap2 != 'undefined') loadMap();", 10);
}

function loadMap(){
	if (!GBrowserIsCompatible() || !(document.createElementNS || document.createElement) || !document.createTextNode) return false;
	if (!(typeof map_location == 'object')) return false;

	// try to get map element
	var map_element=document.getElementById("map");
	if (!map_element) return false;
	while(map_element.hasChildNodes()) map_element.removeChild(map_element.firstChild);

	// create map
	map=new GMap2(map_element);
	if (!map) return false;
	map_point=new GLatLng(map_location["latitude"], map_location["longitude"]);

	// add controls
	map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT));
	//map.addControl(new GLargeMapControl());
	//map.addControl(new GMapTypeControl());

	map.setCenter(map_point, 14);

	return initOverlay();
}

function createIcon(icon_file){
	var icon=new GIcon();

	icon.image="http://labs.google.com/ridefinder/images/mm_20_red.png";
	icon.shadow="http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize=new GSize(12, 20);
	icon.shadowSize=new GSize(22, 20);
	icon.iconAnchor=new GPoint(6, 20);
	icon.infoWindowAnchor=new GPoint(5, 1);

	return icon;
}

function initOverlay(){
	var map_marker=new GMarker(new GPoint(parseFloat(map_location["longitude"]),parseFloat(map_location["latitude"])),createIcon())
	map.addOverlay(map_marker);

	map_html="<div id=\"map-bubble\">";

	map_html += "<div id=\"bubble-top\">";
	map_html += "<img src=\"/img/content/ico16-close.gif\" alt=\"Close\" onclick=\"closeOverlay()\"\/>";
	map_html += "<\/div>";

	map_html += "<div id=\"bubble-content\">";
	map_html += '<form method="get" action="http://maps.google.com/maps">';
	map_html += '<p><label for="saddr">Where are you travelling from? <input class="text" type="text" id="saddr" name="saddr" value="" \/></label></p>';
	map_html += '<p><input type="hidden" name="daddr" value="' + map_location["postcode"] + '" \/>';
	map_html += '<input type="hidden" name="hl" value="en" \/>';
	map_html += '<input class="submit" type="submit" value="Get directions" \/></p>';
	map_html += '<\/form>';
	map_html += "<\/div>";

	map_html += "<\/div>";

	openOverlay();

	GEvent.addListener(map_marker, 'click', openOverlay);

	return true;
}

function openOverlay(){
	if (map_overlay == null){
		map.savePosition();
		map.panTo(map_point);
		map_overlay=new Growverlay(map,map_point,map_html);
		map_overlay.initialize();
		map_overlay.redraw(true);
	}
}

function closeOverlay(){
	map_overlay.remove();
	map_overlay=null;
	map.returnToSavedPosition();
}

function focusDirections(e){
	document.getElementById('saddr').focus();
}

// add load event functions
addLoadEvent(initMap);

// add unload event functions
window.onunload=function(){
	if (typeof GUnload != 'undefined') GUnload();
}



function Growverlay(omap,opoint,ohtml,xo,yo){
	this.map=omap;
	this.point=opoint;
	this.html=ohtml;
	this.xo=xo || 0;
	this.yo=yo || 0;
}

Growverlay.prototype=new GOverlay();

Growverlay.prototype.initialize=function(){
	var pane=this.map.getPane(G_MAP_FLOAT_PANE);
	pane.innerHTML=this.html;
	var ediv=pane.getElementsByTagName("div");
	if (!ediv) return;
	if (ediv.length) ediv=ediv[0];
	ediv.style.position="absolute";

	this.ediv=ediv;

	GEvent.addDomListener(document.getElementById('saddr'), "click", focusDirections);
}

Growverlay.prototype.redraw=function(force){
	if (!force){
		return;
	}
    var p=this.map.fromLatLngToDivPixel(this.point);
	var xo,yo;
	xo=this.xo;
	yo=this.yo;

    this.ediv.style.left=p.x + xo + "px";
    this.ediv.style.bottom=- p.y + yo + "px";
}

Growverlay.prototype.remove=function(){
	this.ediv.parentNode.removeChild(this.ediv);
}
