function createMarker(map, lid, point, type, level, text, infoWindow)
{
	var icon = getIcon(type);
	var marker = new GMarker(point, icon);
	if (level < 1)
	{
        if (infoWindow)
        {
            GEvent.addListener(marker, "click", function() {
                sm = marker;
                x_getDetails(lid, showDetails);
            });
        }
	}
	else
	{
		GEvent.addListener(marker, "click", function() {
			map.setCenter(marker.getPoint());
			map.setZoom(6);
		});
	}
	return marker;
}

function showDetails(text){
	var events = text[2].length > 0;
	var infoTabs = new Array(events ? 3 : 2);
	infoTabs[0] = new GInfoWindowTab("Info", text[0]);
	infoTabs[1] = new GInfoWindowTab("Details", text[1]);
	if (events)
	{
		infoTabs[2] = new GInfoWindowTab("Events", text[2]);
	}
	sm.openInfoWindowTabsHtml(infoTabs);
}

function getDirections(addr)
{
	f = document.getElementById("directionsForm");
	f.innerHTML = 'Enter your location: <form onsubmit="return false"><input type=text name=a1 size=20><input type=button value="Go" onclick="showDirections(this.form.a1.value, \'' + addr + '\')"></form>';
}

function showDirections(a1, addr)
{
	map.closeInfoWindow();
	var top = dijit.byId('topContainer');
	var dirDivMain = document.createElement('div');
	dirDivMain.style.height = '40%';
	var bc = new dijit.layout.ContentPane({id: "directionsMain", region: "bottom", 
		splitter: "true", href: "google_directions.php"}, dirDivMain);
  	dojo.connect(bc, 'onLoad', function(){
  		createMap();
  		map.checkResize();
	  	var dirDiv = dojo.byId('directions');
  		directions = new GDirections(map, dirDiv);
  		GEvent.addListener(directions, "error", handleErrors);
  		directionsText = a1 + " to " + addr;
  		directions.load(directionsText);
  	});
	top.domNode.appendChild(dirDivMain);
  	top.addChild(bc);
}

function closeDirections()
{
	var bc = dijit.byId('directionsMain');
	var top = dijit.byId('topContainer');
	top.removeChild(bc);
	bc.destroy();
	load();
}

function printDirections()
{
	var url = window.location + '&pw=1&directions=' + escape(directionsText);
	window.open(url);
}

function showAndPrint(directionsText)
{
	createMap();
	var dirDiv = document.getElementById('directions');
	directions = new GDirections(map, dirDiv);
	GEvent.addListener(directions, "error", handleErrors);
	GEvent.addListener(directions, "load", function(e){
		// Wait 2s to fully initialize everything.
		setTimeout(function() { window.print() }, 200);
	});
	directions.load(directionsText);
}

function handleErrors()
{
   if (directions.getStatus().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: " + directions.getStatus().code);
   else if (directions.getStatus().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: " + directions.getStatus().code);

   else if (directions.getStatus().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: " + directions.getStatus().code);

//   else if (directions.getStatus().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: " + directions.getStatus().code);

   else if (directions.getStatus().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: " + directions.getStatus().code);

   else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);

   else alert("An unknown error occurred." + directions.getStatus().code);
   closeDirections();
}

function getIcon(type)
{
	return type == 0 ? baseIcon : visitorIcon;
}