var map;
var overlayControl;
var directionsPanel;
var directions;
var briggsIcon;

function initialize() {
	var mapDiv = getObj('map');
	if (GBrowserIsCompatible()) {
		mapDiv.style.overflow = "hidden";
		//==========create the map==========
		map = new GMap2(mapDiv);
		map.addMapType(G_PHYSICAL_MAP);
		directionsPanel = getObj("route");
		directions = new GDirections(map, directionsPanel);
		//==========create controls==========
		overlayControl = new GOverviewMapControl();
		zoomControl = new GLargeMapControl();
		var typeControl = new GHierarchicalMapTypeControl();
		//==========add controls==========
		map.addControl(zoomControl);
		//map.addControl(new GMenuMapTypeControl());
		map.addControl(typeControl);
		map.addControl(overlayControl);
		//==========set controls==========
		overlayControl.hide(true);
		//==========settings==========
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		map.enableGoogleBar();
		map.setCenter(new GLatLng(lat,lon),14);
		map.setMapType(G_NORMAL_MAP);
		//==========disable scrolling on the page if over map==========
		GEvent.addDomListener(mapDiv, "DOMMouseScroll", wheelevent);
		mapDiv.onmousewheel = wheelevent;
		addRBriggs();
	} else {
		mapDiv.innerHTML = "<div style='font-weight:bold;'>Sorry, your browser does not support the advanced Javascript needed to display this map.<br\/>If your browser has javascript disabled, please turn it back on and refresh the page to continue, else consider downloading an standards complient browser, like the popular <a href='http://www.mozilla.com/en-US/firefox'>firefox<\/a><\/div>";
	}
}
function wheelevent(e) {
	if (!e)
		e = window.event;
	if (e.preventDefault)
		e.preventDefault();
	e.returnValue = false;
}
function addRBriggs() {
	var location = new GLatLng(lat,lon);
	var briggsIcon = new GMarker(location);
	briggsIcon.title = iconTitle;
	var tabs = new Array();
	tabs[0] = new GInfoWindowTab("Address", windowCon);
	tabs[1] = new GInfoWindowTab("Picture",  maxContent);
	GEvent.addListener(briggsIcon, 'click', function() {
		briggsIcon.openInfoWindowTabsHtml(tabs);
		//briggsIcon.openInfoWindowHtml(windowCon,{maxContent: windowCon+maxContent,maxTitle: iconTitle});
	});
	map.addOverlay(briggsIcon);
}
function getDirections(location) {
	var locale = getObj('locale').options[getObj('locale').selectedIndex].value;
	if(!locale)
		locale = "en";
	try {
		directionsPanel.innerHTML = "";
		directions.load("from:"+location+" to: "+lat+","+lon,{ "locale": locale });
		setTimeout("roadfix()",500);
	} catch(e) { }
	return false;
}
function roadfix() {
	var html = directionsPanel.innerHTML;
	html = html.replace("Unknown road","Pull into parking lot");
	html += "<div id='print-icon'><span id='clr' onclick='clearDirections();'>Clear<\/span> <span id='prnt' onclick='printDirections();'>Print<\/span><\/div>";
	directionsPanel.innerHTML = html;
}
function clearDirections() {
	directions.clear();
	directionsPanel.innerHTML = "Enter your Location Below";	
}
function printDirections() {
	var header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+"\n";
		header += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">'+"\n";
		header += '<head><title>'+iconTitle+' - [Directions]<\/title>'+"\n";
		header += '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" \/>'+"\n";
		header += '<meta http-equiv="content-language" content="en" \/>'+"\n";
		header += '<link rel="shortcut icon" href="favicon.ico" type="image/vnd.microsoft.icon" \/>'+"\n";
		header += '<\/head><body>'+"\n";
	var footer = '<\/body></html>';
	var html = directionsPanel.innerHTML;
	html = html.replace(new RegExp("<div id=['\"]?print-icon[\"']?>(.+?)</div>","i"),"");
	html = html.replace(new RegExp("<a(.+?)>(.+?)</a>","ig"),"$2");
	html = html.replace(/([0-9\.]+&nbsp;mi \(about [0-9]+ mins\))/,"<div style='text-align:right'>$1</div>");
	var newWindow = window.open('','_blank');
	newWindow.document.write(header+html+footer);
	for(var x = 0;x < newWindow.document.images.length;x++) {
		newWindow.document.images[x].src = "images/blank.gif";
	}
	newWindow.document.close();
	newWindow.print();
}
