	var map = null;
	var center = null;
	var offcenter = null;
	var marker = null;
	var to_htmls = null;
	var from_htmls = null;
	var geocoder = null;
	var gdir;
	var addressMarker;

	var myHtml = "<table width='200' cellpadding='0' cellspacign='0' border='0'><tr><td valign='top'><img alt='' src='http://www.luostotunturi.com/pikkukuvat/luosto.jpg' width='80' height='70' vspace='0' hspace='5' border='1'></td><td valign='top'><b>Hotelli Luostotunturi</b><br>Luostontie 1<br>99555 Luosto<br><a href='http://www.luostotunturi.com/' target='_blank'>www.luostotunturi.com</a></td></tr></table>";

function load() {

	if (GBrowserIsCompatible()) {

	        map = new GMap2(document.getElementById("map"));
	        center = new GLatLng(67.156821,26.916258);
	        addressMarker = new GMarker(center, {draggable: false});
		geocoder = new GClientGeocoder();
		gdir = new GDirections(map, document.getElementById("directions"));

	        map.setCenter(center, 15);
	        map.openInfoWindowHtml(center, myHtml);
	        map.addOverlay(addressMarker);

		map.enableScrollWheelZoom()
	        map.setUIToDefault();
//		map.removeMapType(G_HYBRID_MAP);
//		map.removeMapType(G_SATELLITE_MAP);
//		map.removeMapType(G_PHYSICAL_MAP);

		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		GEvent.addListener(addressMarker,"click", function() {
			map.openInfoWindowHtml(center, myHtml);
		        map.setCenter(center);
		});
	}
}

function onGDirectionsLoad(){ 

}

function handleErrors(){

	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Lähtö- tai määränpääosoitetta ei löytynyt\nTarkista oikeinkirjoitus tai täydennä tietoja");
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("Virhe hakuehtojen käsittelyssä");
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("Virhe hakuehtojen käsittelyssä\nTarkista oikeinkirjoitus tai täydennä tietoja");
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("Määrittelemätön virhe");
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		return;
	else alert("Määrittelemätön virhe");
}

function tyhjenna() {  
  
	gdir.clear();
	map.removeOverlay(addressMarker);
	map.setCenter(center, 15);
	map.openInfoWindowHtml(center, myHtml);

        addressMarker = new GMarker(center, {draggable: false});
        map.addOverlay(addressMarker);
	GEvent.addListener(addressMarker,"click", function() {
		map.openInfoWindowHtml(center, myHtml);
	        map.setCenter(center);
	});

	hakulomake.mista.value = "";
	hakulomake.mihin.value = "Luostontie 1, 99555 Sodankylä, Finland";
}

function keskita() {   
 
        map.setCenter(center);
}

function showAddress(address) {

	if (geocoder) {
		geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert("Osoitetta ei löytynyt");
			} else {
				map.setCenter(point, 15);
				map.removeOverlay(addressMarker);
				addressMarker = new GMarker(point);
				map.addOverlay(addressMarker);

				var printaddress = address.replace(/,/g,",<br>");
				addressMarker.openInfoWindowHtml("<div class='txt' style='text-align:center'><b><br>"+printaddress+"</b></div>");

				GEvent.addListener(addressMarker,"click", function() {
					map.openInfoWindowHtml(point, "<div class='txt' style='text-align:center'><b><br>"+printaddress+"</b></div>");
				        map.setCenter(point);
				});
			}
		}
		);
	}
}

function centerAddress(address) {

	if (geocoder) {
		geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert("Osoitetta ei löytynyt");
			} else {
				map.setCenter(point, 15);
				map.removeOverlay(addressMarker);
				addressMarker = new GMarker(point);
				map.addOverlay(addressMarker);

				var printaddress = address.replace(/,/g,",<br>");
				addressMarker.openInfoWindowHtml("<div class='txt' style='text-align:center'><b><br>"+printaddress+"</b></div>");

				GEvent.addListener(addressMarker,"click", function() {
					map.openInfoWindowHtml(point, "<div class='txt' style='text-align:center'><b><br>"+printaddress+"</b></div>");
				        map.setCenter(point);
				});
			}
		}
		);
	}
}
function setDirections(fromAddress, toAddress) {

	gdir.load("from: " + fromAddress + " to: " + toAddress,{travelMode:G_TRAVEL_MODE_DRIVING});
}

function kasitteleOsoitehaku(mistaOsoite, mihinOsoite) {

	if(mistaOsoite && mihinOsoite) {
		setDirections(mistaOsoite, mihinOsoite);
		map.closeInfoWindow();
	} else if(mistaOsoite) {
		gdir.clear();
		showAddress(mistaOsoite);
	} else if(mihinOsoite) {
		gdir.clear();
		showAddress(mihinOsoite);
	} else
		alert("Molemmat osoitekentät olivat tyhjiä");
}

function kasitteleKohdista(osoite) {

	if(osoite)
		centerAddress(osoite);
	else
		alert("Osoitekenttä oli tyhjä");
}
