var map;
var directionsPanel;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var briggsIcon;

function initialize() {
	directionsDisplay = new google.maps.DirectionsRenderer();
	var mapDiv = getObj('map');
	
	mapDiv.style.overflow = "hidden";
	//==========create the map==========
		
	var myLatlng = new google.maps.LatLng(lat,lon);
	var myOptions = {
		zoom: 15,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
		
	map = new google.maps.Map(mapDiv, myOptions);
	
	directionsPanel = getObj("route");
	
	directionsDisplay.setMap(map);
	directionsDisplay.setPanel(directionsPanel);

	//==========settings==========
	//map.enableDoubleClickZoom();
	//map.enableContinuousZoom();
	//map.enableScrollWheelZoom();

	
	addRBriggs();
}

function addRBriggs() {
	var location = new google.maps.LatLng(lat,lon);
	
	var briggsIcon = new google.maps.Marker({
		position: location,
		map: map,
		title: iconTitle
		//icon: image
	});
    var infowindow = new google.maps.InfoWindow({
        content: windowCon
    });
    google.maps.event.addListener(briggsIcon, 'click', function() {
		infowindow.open(map,briggsIcon);
    });
}
function getDirections(location) {
	directionsPanel.innerHTML = "";
	
	var request = {
		origin:location, 
		destination:lat+","+lon,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};

	directionsService.route(request, function(result, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(result);
			directionsDisplay.setMap(map);
		}
	});

	setTimeout("roadfix()",500);
}
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() {
	directionsDisplay.setMap(null);
	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();
}
