var map;
var directions;
var bounds;
var KMLfile;
function showDirMapBlowup(point){
	var pnts=point.split(',');
	var lat = parseFloat(pnts[1]);
	var lng = parseFloat(pnts[0]);
	var point = new GLatLng(lat, lng);
	map.showMapBlowup(point);
	GEvent.addListener(map, 'infowindowclose', function() {
		map.panTo(bounds.getCenter());
	});
}
function googlemap(KMLfile) {
	if (GBrowserIsCompatible()) {
		if(!KMLfile) KMLfile = $('#srcDomain').val()+'/u/psd/kml/secondnarrowsbridge.kml';
		GDownloadUrl(KMLfile, function(data) {
			map = new GMap2(document.getElementById('contactUsMapDiv'));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());    
			map.setCenter(new GLatLng(0,0),13);
			bounds = new GLatLngBounds();
			var xml = GXml.parse(data);
			var placemarks = xml.documentElement.getElementsByTagName('Placemark');
			//create the polyline
			var polycoords = placemarks[(placemarks.length-1)].getElementsByTagName('coordinates')[0].childNodes[0].nodeValue.split(' ');
			var points = [];
			for (var i = 0; i < (polycoords.length-1); i++) {
				var pnts=polycoords[i].split(',');
				var lat = parseFloat(pnts[1]);
				var lng = parseFloat(pnts[0]);
				var point = new GLatLng(lat, lng);
				points.push(point);
				bounds.extend(point);
				if(i==0) startpoint=point;
				if(i==(polycoords.length-2)) endpoint=point;
			}
			map.addOverlay(new GPolyline(points));
          	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
			//create the markers
			var baseIcon = new GIcon();
			baseIcon.shadow = 'http://www.google.com/mapfiles/shadow50.png';
			baseIcon.iconSize = new GSize(20, 34);
			baseIcon.shadowSize = new GSize(37, 34);
			baseIcon.iconAnchor = new GPoint(9, 34);
			baseIcon.infoWindowAnchor = new GPoint(9, 2);
			baseIcon.infoShadowAnchor = new GPoint(18, 25);
			function createMarker(point, type) {
				var icon = new GIcon(baseIcon);
				if(type=='start'){
					icon.image = 'http://www.google.com/intl/en_ALL/mapfiles/dd-start.png';
				}else if(type=='end'){
					icon.image = 'http://www.google.com/intl/en_ALL/mapfiles/dd-end.png';
				}
				var marker = new GMarker(point, icon);
				GEvent.addListener(marker, 'click', function() {
					marker.showMapBlowup();
				});
				GEvent.addListener(marker, 'infowindowclose', function() {
					map.panTo(bounds.getCenter());
				});
				return marker;
			}
			map.addOverlay(createMarker(startpoint, 'start'));
			map.addOverlay(createMarker(endpoint, 'end'));
			//add the directions
			var directionsStr;
			for (var i = 0; i < (placemarks.length-1); i++) {
				if(i==0){
					directionsStr = '<div jstcache="0"><div class="googledir" jstcache="1"><div jstcache="2" jsinstance="0"><table style="border: 1px solid silver; margin: 10px 0px; background-color: rgb(238, 238, 238); border-collapse: collapse; color: rgb(0, 0, 0);" jstcache="0"><tbody jstcache="0"><tr onclick="showDirMapBlowup(\''+placemarks[i].getElementsByTagName('coordinates')[0].childNodes[0].nodeValue+'\')" style="cursor: pointer;" jstcache="7"><td style="padding: 4px 15px 0px 5px; vertical-align: middle; width: 20px;" jstcache="0"><img src="http://www.google.com/intl/en_ALL/mapfiles/icon-dd-play-trans.png" jstcache="10"/></td><td jscontent="address" style="vertical-align: middle; width: 100%;" jstcache="8">'+placemarks[i].getElementsByTagName('address')[0].childNodes[0].nodeValue+'</td></tr></tbody></table><div jstcache="4"><div style="text-align: right; padding-bottom: 0.3em;" jstcache="5">'+placemarks[(placemarks.length-1)].getElementsByTagName('description')[0].childNodes[0].nodeValue.substr(0,parseFloat(placemarks[(placemarks.length-1)].getElementsByTagName('description')[0].childNodes[0].nodeValue.indexOf(')'))+1)+'</div><table style="margin: 0px; padding: 0px; border-collapse: collapse;" jstcache="6"><tbody jstcache="0">';
				}else if(i==(placemarks.length-2)){
					directionsStr += '</tbody></table></div></div><div jstcache="2" jsinstance="*'+i+'"><table style="border: 1px solid silver; margin: 10px 0px; background-color: rgb(238, 238, 238); border-collapse: collapse; color: rgb(0, 0, 0);" jstcache="0"><tbody jstcache="0"><tr onclick="showDirMapBlowup(\''+placemarks[i].getElementsByTagName('coordinates')[0].childNodes[0].nodeValue+'\')" style="cursor: pointer;" jstcache="7"><td style="padding: 4px 15px 0px 5px; vertical-align: middle; width: 20px;" jstcache="0"><img src="http://www.google.com/intl/en_ALL/mapfiles/icon-dd-stop-trans.png" jstcache="10"/></td><td jscontent="address" style="vertical-align: middle; width: 100%;" jstcache="8">'+placemarks[i].getElementsByTagName('name')[0].childNodes[0].nodeValue+'</td></tr></tbody></table><div jstcache="4" style="display: none;"><div style="text-align: right; padding-bottom: 0.3em;" jstcache="5"/><table style="margin: 0px; padding: 0px; border-collapse: collapse;" jstcache="6"><tbody jstcache="0"><tr onclick="showDirMapBlowup(\''+placemarks[i].getElementsByTagName('coordinates')[0].childNodes[0].nodeValue+'\')" style="cursor: pointer;" jstcache="9"><td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px; vertical-align: top; text-align: right;" jstcache="0"><a jscontent="'+(parseFloat(i)+1)+'" href="javascript:void(0)" jstcache="13"/>.</td><td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px; vertical-align: top; width: 100%;" jstcache="11"/><td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px 0.3em 0.5em; vertical-align: top; text-align: right;" jstcache="12"/></tr></tbody></table></div></div></div></div>';
					break;
				}
				directionsStr += '<tr onclick="showDirMapBlowup(\''+placemarks[i].getElementsByTagName('coordinates')[0].childNodes[0].nodeValue+'\')" style="cursor: pointer;" jstcache="9" jsinstance="'+i+'"><td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px; vertical-align: top; text-align: right;" jstcache="0"><a jscontent="'+(parseFloat(i)+1)+'" href="javascript:void(0)" jstcache="13">'+(parseFloat(i)+1)+'</a>.</td><td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px; vertical-align: top; width: 100%;" jstcache="11">'+placemarks[i].getElementsByTagName('name')[0].childNodes[0].nodeValue+'</td><td style="border-top: 1px solid rgb(205, 205, 205); margin: 0px; padding: 0.3em 3px 0.3em 0.5em; vertical-align: top; text-align: right;" jstcache="12">'+placemarks[i].getElementsByTagName('description')[0].childNodes[0].nodeValue.substr(2)+'</td></tr>';
			}
			document.getElementById('contactUsMapDirDiv').innerHTML=directionsStr;
		});
	}
}
