function GetGMap(lat, long, zoomIns) {
	/*
	if (!GBrowserIsCompatible())
		return null;
	if (window.GoogleMapObject)
		return window.GoogleMapObject;
	window.GoogleMapObject = new GMap2(document.getElementById("GoogleMap"));
	return window.GoogleMapObject;
	*/

	//var place = new google.maps.LatLng(43.44164, -79.688473);
	var place = new google.maps.LatLng(lat, long);
	var myOptions = {
		zoom:zoomIns,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		center: place,
		scrollwheel: false
	}

	map = new google.maps.Map(document.getElementById("GoogleMap"), myOptions);
	//directionsDisplay.setMap(map);
	//geocoder = new google.maps.Geocoder();

	return map;
}


function GetGDirections() {
	if (window.GoogleMapDirectionObject)
		return window.GoogleMapDirectionObject;

	var map = GetGMap();
	if (!map)
		return null;
	window.GoogleMapDirectionObject = new GDirections(map, document.getElementById("GoogleMapDirection_textual"));
	GEvent.addListener(window.GoogleMapDirectionObject, "error", handleGoogleMapDirectionErrors);
	
	return window.GoogleMapDirectionObject;
}

function handleGoogleMapDirectionErrors() {
	var  code = GetGDirections().getStatus().code;
	if (code == G_GEO_UNKNOWN_ADDRESS)
		alert("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: " + code);
	else if (code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + code);
	else if (code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + code);
	//   else if (code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + code);
	else if (code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + code);
	else if (code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + code);
	else
		alert("Sorry, one of your inputs was not valid.  Please try again.");
}

/*******************************************************************************/
function initialize() {
	
	var zoomIn = 7;
	var Lat = 43.44164;
	var Long = -79.688473;
	if(SearchResults.length == 1){
		zoomIn = 12;
		Lat = SearchResults[0].Latitude;
		Long = SearchResults[0].Longitude;
	}
	
	var map = GetGMap(Lat, Long, zoomIn);
	if (!map)
		return;

	var rendererOptions = {
		map: map
	}
	directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
	directionsService = new google.maps.DirectionsService();


	//map.setMapType(G_NORMAL_MAP); // G_NORMAL_MAP   G_SATELLITE_MAP   G_HYBRID_MAP   G_DEFAULT_MAP_TYPES
  
	//map.disableDragging()
	
	//map.addControl(new GSmallZoomControl());
	//map.addControl(new GScaleControl());
	//map.addControl(new GMapTypeControl());
	//map.addControl(new GHierarchicalMapTypeControl());
	
	
	/*
	window.setTimeout(function() {
							map.panTo(new GLatLng(37.4569, -122.1569));
							}, 1000);

	map.setCenter(new GLatLng(37.4419, -122.1419), 13);
	map.openInfoWindow(map.getCenter(), document.createTextNode("Hello, world"));
	*/
	
	/*
	GEvent.addListener(map, "click", function(overlay, latlng) {
		document.getElementById("message").innerHTML = "You clicked the map.";
		if (latlng) {
			var myHtml = latlng.toString() + "<br/>The GPoint value is: " + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom();
			map.openInfoWindow(latlng, myHtml);
		}
	});
	GEvent.addListener(map, "dblclick", function() {
		alert('dblclick');
	});
	GEvent.addListener(map, "moveend", function() {
		var center = map.getCenter();
		document.getElementById("message").innerHTML = center.toString();
	});
	*/

	/*
	google.maps.event.addListener(map, 'click', function(event) {
		placeMarker(event.latLng);
	});


	google.maps.event.addListener(map, 'dblclick', function(event) {
		StopGallery();
	});
	*/

	/*
	GEvent.addListener(map, "moveend", function() {
		//var center = map.getCenter();
		document.getElementById("GoogleMapMessage").innerHTML = '';
		StopGallery();
	});
	*/

	//map.setCenter(new GLatLng(43.44164, -79.688473), 8);

	if (!(SearchResults == "undefined")) {
		for (var i = 0; i < SearchResults.length; i++) {
			//map.addOverlay(createMarker(map, SearchResults[i], i + 1));
			createMarker(map, SearchResults[i], i + 1);
			
			if (i==0) {
				  map.setCenter(new google.maps.LatLng(SearchResults[i].Latitude, SearchResults[i].Longitude));
			}

		}
		//window.setTimeout(Gallery, 1);
	}
	//else
		//map.setCenter(new GLatLng(43.44164, -79.688473), 8);

}



function createMarker(map, location, number) {
	//document.getElementById("GoogleMapMessage").innerHTML = ' ';
	var myLatlng = new google.maps.LatLng(location.Latitude, location.Longitude);
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		title: location.Name
	});
	marker.value = number;



	var infowindow = new google.maps.InfoWindow(
		{
			content: "<b>" + location.Name + "</b><br/>" + location.Address + ", " + location.PostalCode + "<br/>" + location.City + ", " + location.Province + "<br/><b>P</b> " + location.Phone + " <b>F</b> " + location.Fax,
			size: new google.maps.Size(50,50)
		});


	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map, marker);
	});

	/*
	GEvent.addListener(marker, "click", function() {
		
		map.openInfoWindowHtml(point, myHtml);
	});
	*/
	return marker;
}

function mapIcon() {
	var iconObj = new GIcon();
	iconObj.image = "/imgs/mapicons/horse.png";
	iconObj.shadow = "/imgs/mapicons/shadow.png";
	iconObj.iconSize = new GSize(34,55);
	iconObj.shadowSize = new GSize(36,33);
	iconObj.iconAnchor = new GPoint(27,27);
	iconObj.infoWindowAnchor = new GPoint(25,31);
	return iconObj;
}


function Gallery() {
	if (SearchResults == "undefined")
		return

	if (!SearchResults.CurrentIndex)
		SearchResults.CurrentIndex = 0;
	if (SearchResults.CurrentIndex < 0 || SearchResults.CurrentIndex >= SearchResults.length)
		SearchResults.CurrentIndex = 0;

	var location = SearchResults[SearchResults.CurrentIndex];

	try {
		GetGMap().panTo(new GLatLng(location.Latitude, location.Longitude));
	}
	catch (ex) {}
	
	if (SearchResults.length > 1) {
		SearchResults.CurrentIndex++;
		if (SearchResults.CurrentIndex < 0 || SearchResults.CurrentIndex >= SearchResults.length)
			SearchResults.CurrentIndex = 0;
		window.GNapTimerID = window.setTimeout(Gallery, 3000);
	}
	
	
}

function StopGallery() {
	if(window.GNapTimerID)
		window.clearTimeout(window.GNapTimerID);
}


function MoveTo(Latitude, Longitude) {
	//StopGallery();
	map.setCenter(new google.maps.LatLng(Latitude, Longitude));
	map.setZoom(13);
	//GetGMap().panTo(new GLatLng(Latitude, Longitude));
}

/*******************************************************************************/

// Google request
function GetGMapKey() {
	alert(2);
	/*
	var GMapKey = "";
	switch (window.location.hostname) {
		case "lonestartexasgrill.com":
			GMapKey = "ABQIAAAA_ZTTFcZVT_cqwGfo3skzMxTpwb872rYsIfMab8dq1rHoryq-3xSl317BvQ7wz7ZPHl1bIfgJhz0P7A";
			break
		case "www.lonestartexasgrill.com":
			GMapKey = "ABQIAAAA_ZTTFcZVT_cqwGfo3skzMxRSh_mW_cOb0rS8hTKs7JORXGNycBTlsMTNGuTaSb673Ws6CVDBNqJiCw";
			break
		case "lonestartexasgrill-com.sitepreview.ca":
			GMapKey = "ABQIAAAA_ZTTFcZVT_cqwGfo3skzMxSTb3CS08JsgAxnzKpdhXoxFM6pSBRqRO4stHYUQcPo1n1FNve6ssfe-A";
			break
		case "lonestar":
			GMapKey = "ABQIAAAA_ZTTFcZVT_cqwGfo3skzMxSx2keEZZAiizCr4wsQUQMT5RTbkxTx8wNnDYRKE3RTaXn7P0HV2JFXGg";
			break
		case "dev.lonestartexasgrill.com":
			GMapKey = "ABQIAAAA_ZTTFcZVT_cqwGfo3skzMxS46Bo3sUHS8JKULdRxkekYCWi5ARQBBWcH_A-egKYVHyEqjppoUiP81A";
			break
		default:
			GMapKey = ""
	}
	return GMapKey;
	/*
}

function GetDirections() {
	//alert('why');
	//StopGallery();
	var LocationFrom = $('LocationFrom');
	var LocationTo = $('LocationTo');
	/*
	if (!(LocationFrom.Latitude && LocationFrom.Longitude)) {
		F_getLatLongFromPostal(LocationFrom);
		//setTimeout(GetDirections, 5000)
		return;
	}
	*/

	if (LocationFrom.value == LocationFrom.defaultValue || LocationFrom.value == '') {
		alert("Please Enter Your Location");
		return;
	}

	if (LocationTo.selectedIndex == 0) {
		alert("Please Select Store");
		return;
	}

	var rez = function() {
		var LID = LocationTo[LocationTo.selectedIndex].value;
		for (var i = 0; i < LatLonLocations.length; i++)
			if (LatLonLocations[i][0] == LID) {
				LocationTo.Latitude = LatLonLocations[i][1];
				LocationTo.Longitude = LatLonLocations[i][2];
				break;
			}

		var start = new google.maps.LatLng(LocationFrom.Latitude, LocationFrom.Longitude);
		var end = new google.maps.LatLng(LocationTo.Latitude, LocationTo.Longitude);


		var request = {
			origin:start, 
			destination:end,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};

		
		document.getElementById("GoogleMapDirection_textual").innerHTML = '';
		directionsDisplay.setPanel(document.getElementById("GoogleMapDirection_textual"));
		
		directionsService.route(request, function(result, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(result);
			}
		});


		/*
		var LID = LocationTo[LocationTo.selectedIndex].value;
		for (var i = 0; i < LatLonLocations.length; i++)
			if (LatLonLocations[i][0] == LID) {
				LocationTo.Latitude = LatLonLocations[i][1];
				LocationTo.Longitude = LatLonLocations[i][2];
				break;
			}
		
		var gdir = GetGDirections();
		if (!gdir)
			return;
	
		gdir.load("from: " + LocationFrom.Latitude + ", " + LocationFrom.Longitude + " to: " + LocationTo.Latitude + ", " + LocationTo.Longitude);
		*/
	};

	if (!(LocationFrom.Latitude && LocationFrom.Longitude))
		F_getLatLongFromPostal(LocationFrom, rez);
	else
		rez();
}

function F_getLatLongFromPostal(field, _function) {
	//alert(field.id);
	var GeoValue = field.value.toLowerCase();
	try {
		$("GetDirectionGo").disabled = true;
	} catch (err) { }

	//if it doesnt look like a postalcode alert
	/*
	if (!GeoValue.match(/[a-z]\d[a-z]\s?\d[a-z]\d/)) {
		alert("Sorry this doesnt look like a postalcode [A1B2C3] or [A1B 2C3]");
		field.focus();
		return;
	}
	postalcode = postalcode.replace(/\s/g, '');
	*/

	geocoder = new google.maps.Geocoder();
    if (geocoder) {
		geocoder.geocode( { 'address': GeoValue, 'country' : 'Canada'}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				if (results[0]) {
					field.Latitude = results[0].geometry.location.lat();
					field.Longitude = results[0].geometry.location.lng();
					if (typeof _function == "function") {
						_function();
					}
					/*
					map.setCenter(results[0].geometry.location);
					var marker = new google.maps.Marker({
							map: map, position: results[0].geometry.location
					});
					*/
				}
				/*
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
						map: map, position: results[0].geometry.location
				});
				*/
			}
			else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
    }
	
	return;


	var functionName = "F_georesults" + field.id;
	
	window[functionName] = function(obj) {
			if (obj.Status.code == 200) {
				if (obj.Placemark.length > 1) {
					alert("Please specify location");
				}
				else {
					field.Latitude = obj.Placemark[0].Point.coordinates[1];
					field.Longitude = obj.Placemark[0].Point.coordinates[0];
					if (typeof(_function) == 'function')
						_function();
				}
					
			}
			else {
				alert('Error Code '+(obj.Status.code)+' \nLocation not found for ' + obj.name);
			}
			try {
				$("GetDirectionGo").disabled = false;
			} catch (err) { }
		
			if ($("loadScript")) {
				try {
					var head = document.getElementsByTagName('head').item(0);
					head.removeChild($("loadScript"));
					delete $("loadScript");
				} catch (err) { }
			}
			

		}
	
	//var url = "http://maps.google.com/maps/geo?q=" + GeoValue + "&callback=" + functionName + "&output=json&key=" + GetGMapKey();
	//var url = "http://maps.google.com/maps/geo?q=" + GeoValue + "&callback=" + functionName + "&output=json&oe=utf8&sensor=false&key=" + GetGMapKey();
	var url = "http://maps.google.com/maps/geo?q=" + GeoValue + "&callback=" + functionName + "&output=json&oe=utf8&sensor=false";
	F_loadContent(url);
}

function F_loadContent(file) {
	var head = document.getElementsByTagName('head').item(0);
	var scriptTag = document.getElementById('loadScript');
	if (scriptTag) {
		head.removeChild(scriptTag);
	}
	script = document.createElement('script');
	script.src = file;
	script.type = 'text/javascript';
	script.id = 'loadScript';
	head.appendChild(script);
}



/******************************************************************/
//Event.observe(window, 'load', initialize);
//Event.observe(window, 'unload', GUnload);
