env_sys = "geoimgr.com";env_loggedin = false;

var map;
var marker;

function clearMessage() {
	if (document.getElementById("message") != null) { 
		document.getElementById("message").innerHTML = ""; 
		document.getElementById("message").style.borderColor = "#e8e8e8";
	}
}

// Create a marker at the given point with the given label
function GMcreateMarker(point) {
    marker = new GMarker(point, {draggable: true});

    GEvent.addListener(marker, "dragend", function() {
        point = marker.getPoint();
		marker.setPoint(point);
        document.getElementById("lat").value = point.lat();
        document.getElementById("lon").value = point.lng();
        document.getElementById("mapzoom").value = map.getZoom();
		document.getElementById("lat").style.color = "#FF0000";
		document.getElementById("lon").style.color = "#FF0000";
		clearMessage();
    });

    return marker;
}

// Initialize a map and return it
function GMinit(mapid) {
    
    var div = document.getElementById(mapid);
    
    if (!div) {
        alert("Map region not found");
        return null;
    }

    if (!GBrowserIsCompatible()) {
        div.innerHTML = "<p>Sorry, your browser is not compatible with Google Maps.</p>";
        return null;
    }

    map = new GMap2(div);
    marker;
    
    if (parseInt(div.style.height) >= 350) {
        map.addControl(new GOverviewMapControl());
        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());
    } else {
        map.addControl(new GSmallMapControl());
    }
    
    map.addControl(new GMapTypeControl());
    
    lat = document.getElementById("lat").value;
    lon = document.getElementById("lon").value;
    mapzoom = document.getElementById("mapzoom").value;
    maptype = document.getElementById("maptype").value;
    
    if (maptype == "G_NORMAL_MAP") maptype = G_NORMAL_MAP;
    if (maptype == "G_SATELLITE_MAP") maptype = G_SATELLITE_MAP;
    if (maptype == "G_HYBRID_MAP") maptype = G_HYBRID_MAP;
	
    if (mapzoom == 0) mapzoom = 0;
    if (mapzoom == 1) mapzoom = 1;
    if (mapzoom == 2) mapzoom = 2;
    if (mapzoom == 3) mapzoom = 3;
    if (mapzoom == 4) mapzoom = 4;
    if (mapzoom == 5) mapzoom = 5;
    if (mapzoom == 6) mapzoom = 6;
    if (mapzoom == 7) mapzoom = 7;
    if (mapzoom == 8) mapzoom = 8;
    if (mapzoom == 9) mapzoom = 9;
    if (mapzoom == 10) mapzoom = 10;
    if (mapzoom == 11) mapzoom = 11;
    if (mapzoom == 12) mapzoom = 12;
    if (mapzoom == 13) mapzoom = 13;
    if (mapzoom == 14) mapzoom = 14;
    if (mapzoom == 15) mapzoom = 15;
    if (mapzoom == 16) mapzoom = 16;
    if (mapzoom == 17) mapzoom = 17;
    if (mapzoom == 18) mapzoom = 18;
    if (mapzoom == 19) mapzoom = 19;
    if (mapzoom == 20) mapzoom = 20;
    
	map.enableScrollWheelZoom();
	
    if (lat == 33.72434 && lon == -39.023437) {
      map.setCenter(new GLatLng(33.72434, -39.023437), mapzoom, maptype);    
    } else {
      map.setCenter(new GLatLng(lat, lon), mapzoom, maptype);    
    }
    
    // Add a new member function that creates labeled markers
    map.addMarkerOverlay = function(lat, lng) {
        var point = new GLatLng(lat, lng);
        marker = GMcreateMarker(point, "");
        this.addOverlay(marker);
        return point;
    }

    // Allow to click anywhere and display the clicked points coordinates
    GEvent.addListener(map, "click", function(obj, point) {
        marker.setPoint(point);
        document.getElementById("lat").value = point.lat();
        document.getElementById("lon").value = point.lng();
        document.getElementById("mapzoom").value = map.getZoom();
		document.getElementById("lat").style.color = "#FF0000";
		document.getElementById("lon").style.color = "#FF0000";
		clearMessage();
    });

    GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
        document.getElementById("mapzoom").value = map.getZoom();
		document.getElementById("message").value = "";
		clearMessage();
    });

    GEvent.addListener(map, "maptypechanged", function() {
      if (map.getCurrentMapType() == G_NORMAL_MAP) {
			  document.getElementById("maptype").value = "G_NORMAL_MAP";
	    } else if (map.getCurrentMapType() == G_SATELLITE_MAP) {
			  document.getElementById("maptype").value = "G_SATELLITE_MAP";
		  } else if (map.getCurrentMapType() == G_HYBRID_MAP) {
			  document.getElementById("maptype").value = "G_HYBRID_MAP";
		  }
    });
	
    geocoder = new GClientGeocoder();

    map.addMarkerOverlay(lat, lon);
    
    window.onunload = GUnload;

    return map;
}

window.onload = function() {
  gmap = GMinit("map_canvas");
}
