var map;

var dirPanel = null;
var directions = null;

var geoCode = null;

function viewLoc(map, point, zLevel) {
	var cpoint = map.getCenter();

	if (Math.round(point.y) != Math.round(cpoint.y) || Math.round(point.x) != Math.round(cpoint.x)) {
		map.panTo(new GLatLng(point.y, point.x));
		window.setTimeout(function() {
			map.setCenter(new GLatLng(point.y, point.x));
			map.setZoom(zLevel);
		}, 1000);
	}
	else {
			map.setCenter(new GLatLng(point.y, point.x));
			map.setZoom(zLevel);
	}
}

function loadMap(sMapX, sMapY, sMapName, sZoom) {
	if (GBrowserIsCompatible()) {
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		var baseIcon = new GIcon();
		baseIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		baseIcon.iconSize = new GSize(12, 20);
		baseIcon.shadowSize = new GSize(22, 20);
		baseIcon.iconAnchor = new GPoint(6, 20);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);

		var icon = new GIcon(baseIcon);
		icon.image = "/images/_gpointer.png";

		var point = new GPoint(sMapX, sMapY);
		
		if (sZoom == null) {
			sZoom = 14;
		}
		
		// start the map
		var map = new GMap2(document.getElementById(sMapName));
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(point.y, point.x));
		map.setZoom(sZoom);

		function createMarker(point) {
			var zTo = 16;
			var marker = new GMarker(point, icon);

			return marker;
		}

		var marker = createMarker(point);
		map.addOverlay(marker);
	}
}

var geoCode = null;

function hideMapDir(sMapX, sMapY) {
	document.getElementById('btnHide').style.display = 'none';
	document.getElementById('btnPrint').style.display = 'none';
	
	document.getElementById('dirDetails').style.display = 'none';
	document.getElementById('mapContainer').style.width = '100%';
	
	if (sMapX != null && sMapY != null) {
		loadMap(sMapX, sMapY, 'mapContainer');
	}
}

function getMapDir(address, pointY, pointX, isPrint) {
	if (GBrowserIsCompatible()) {
		geoCode = new GClientGeocoder();

		geoCode.getLatLng(address,
			function(point) {
				if (!point) {
					alert("Address: \'"+ address + "\' not found in Google Database");
				}
				else {
					if (document.getElementById("dirDetails").style.display == 'none') {
						document.getElementById('mapContainer').style.width = (parseInt(document.getElementById('mapContainer').offsetWidth) - 300) +'px';
					}

					var dirMap = new GMap2(document.getElementById('mapContainer'));
					dirMap.addControl(new GLargeMapControl());

					document.getElementById("dirContainer").innerHTML = '';

					var directions = new GDirections(dirMap, document.getElementById("dirContainer"));
					directions.load(point.y +','+ point.x +" to "+ pointY +","+ pointX);

					document.getElementById("dirDetails").style.display = '';
					if (isPrint == null) {
						document.getElementById("btnHide").style.display = '';
						document.getElementById("btnPrint").style.display = '';
					}
				}
			}
		);
	}
}
