var map;
var directions;
var directionspanel;
var selectedlatlng;
var currentlyopen;
var icon;

function OfficeFinderLoad()
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("mapcanvas"));
		
		smallmapcontrol = new GSmallMapControl();
		map.addControl(smallmapcontrol);
		maptypecontrol = new GMapTypeControl();
		map.addControl(maptypecontrol);
		map.setCenter(new GLatLng(51.90162, 4.372779), 1);
		
		directionspanel = document.getElementById("directionspanel");
		
		icon = new GIcon();
		icon.image = "/Portals/1/Skins/mammoet_skin/img/mammoet_mapicon.gif";
		icon.iconSize = new GSize(25, 19);
		icon.iconAnchor = new GPoint(12, 18);
		icon.infoWindowAnchor = new GPoint(25, 19);
		
		if(map)
		{
			map.setMapType(G_HYBRID_MAP);
		}
	} 
}

function ShowHideOffice(id, name, address, postalcode, city, country, lat, lng)
{
	elm = document.getElementById(id);
	if(elm.className == "mm_office")
	{
		//show/hide office address in left bar
		elm.className = "mm_office mm_open";
		if(currentlyopen && currentlyopen.id != elm.id)
			currentlyopen.className = "mm_office";
		currentlyopen = elm;
		
		//lookup address with geocoding, focus map on Mammoet office
		fulladdress = address + ", " + postalcode + ", " + city + ", " + country;
		document.getElementById("txtDestination").value = fulladdress;
		
		markerhtml = "<strong>" + name + "</strong><br>"+address+"<br>"+postalcode+" "+city+"<br>"+country;
		
		if(lat != "" && lng != "")
		{
			SetMapMarker(new GLatLng(lat, lng), markerhtml);
		}
	}
	else
	{
		elm.className = "mm_office";
		document.getElementById("txtDestination").value = "";
	}		
}

function SetMapMarker(point, markerhtml)
{
	if(point)
	{
		if(map)
		{
			map.clearOverlays();
			map.setCenter(point, 13);
			var marker = new GMarker(point, icon);
			GEvent.addListener(marker, "click", 
				function(){
					marker.openInfoWindowHtml(markerhtml);
				}
			);
			
			map.addOverlay(marker);
			marker.openInfoWindowHtml(markerhtml);
			selectedlatlng = point;
		}
	}
	else
	{
		window.status = "Error fetching address coordinates. Result: " + point;
	}
}

function GetDirections()
{
	startlocation = document.getElementById("txtStartLocation").value;
	destination = document.getElementById("txtDestination").value;
	
	if(startlocation != "")
	{
		if(!directions)
		{
			directions = new GDirections(map, directionspanel);
			GEvent.addListener(directions, "error", DirectionsError);
		}
		if(directions)
		{
			directions.clear();
			directions.load(startlocation + " to " + destination);
		}
	}
}

function DirectionsError()
{
	geostatus = directions.getStatus();
	if(geostatus.code == G_GEO_UNKNOWN_ADDRESS)
	{
		directionspanel.appendChild(document.createTextNode("Directions error: one of the addresses is unknown to the Directions service."));
	}
	else
	{
		directionspanel.appendChild(document.createTextNode("Directions for the addresses you supplied could not be retrieved."));
	}
}

