/* Used to control google map functions */

var map;
var msgBox;
var marker;
var gc;
var Route;
var RouteInfo;
var ShowRoute = true;
var markerLabel = '<strong>Singletary Gallery</strong>';

var atzMap = function () {
	
	return {
		
		init: function () {
			gc = new GClientGeocoder();
			gc.getLatLng(defaultAddress,this.LoadMainMap);
		},
		
		LoadMainMap: function(point)
		{
			//Load Map
			map = new GMap2(document.getElementById("gmap"));
			map.setCenter(point, 13);
			map.setMapType(G_NORMAL_MAP);
			
			atzMap.MapControls();			
			atzMap.addMarker(point);
			
		},
		
		addMarker: function(point)
		{			
			if (point)
			{
				//var strWinInfo = markerLabel + ":<br />"+defaultAddress;
				marker = new GMarker(point);
				map.addOverlay(marker);
				//marker.openInfoWindowHtml(strWinInfo);
				map.setCenter(point,15);
				
			}else{
				gui.dialog('Error','That address could not be found.','error');
			}
		},
		
		MapControls: function()
		{
			map.enableScrollWheelZoom();
			//map.addControl(new GSmallMapControl());
			map.enableDoubleClickZoom();
		},
		
		GetDirections: function()
		{			
			var To = defaultAddress;
			var From = $('#from-address').val();
			var dirHtml = $('#hidden-directions').get(0);
			
			var strQ = 'from: '+From.toString()+' to: '+To.toString();
			
			if (!Route)
			{
				Route = (ShowRoute)? new GDirections(map, dirHtml) : new GDirections();
			}else{
				Route.clear();
			}
			
			Route.load(strQ,{ "locale": "en_US" });
			GEvent.addListener(Route, "load", atzMap.PrintDirections);
			GEvent.addListener(Route, "error", handleErrors);
			
			return true;
			
		},
		
		PrintDirections: function () {			
			setTimeout(function () {
				if ($('#hidden-directions').html() != "") {
					$('#directions-html').val($('#hidden-directions').html());
					$('#directions').trigger('submit');
				}
			}, 1000);
		},
		
		ClearRoute : function () {
			if (Route) {
				Route.clear();
				hideSaveLink();
			}
		}
	}
}();

function handleErrors()
{
	var message = "";
	var statusCode = Route.getStatus().code;
	
	switch (statusCode)
	{
		case G_GEO_UNKNOWN_ADDRESS:
			message = "No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + statusCode;
		break;
		
		case G_GEO_SERVER_ERROR:
			message = "A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + statusCode;
		break;
		
		case G_GEO_BAD_REQUEST:
			message = "A directions request could not be successfully parsed.\n Error code: " + statusCode;
		break;
		
		default:
			message = "An unknown error occurred.";
		break;
	}
	
	gui.dialog('Error',message,'error');
}

function showPrintLink () {
	$('.PrintDirectionsButton').show();
}

function hidePrintLink () {
	$('.PrintDirectionsButton').hide();
}
