var map;
var thelat = 1.299773;
var thelng = 103.860476;

function init(){
    
        var docHeight = 100;
        var bpos = document.getElementById("id_footer1").offsetTop+20;

        document.getElementById("map_canvas").style.height = bpos+"px"; //20px added because of the footer height
        document.getElementById("map_canvas").style.backgroundAttachment = "fixed"
        document.getElementById("map_canvas").style.backgroundPosition = "center"
        
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(geoip_latitude(), geoip_longitude()), 15); //8
        //map.setCenter(new GLatLng(thelat, thelng), 15); //8
        
        map.setMapType(G_SATELLITE_MAP);
        map.addControl(new GLargeMapControl3D());
        map.disableDoubleClickZoom();
        //map.disableScrollWheelZoom();
        
        geocoder = new GClientGeocoder();
        
        var blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.iconSize = new GSize(16, 16);
        blueIcon.shadowSize = new GSize(0, 0);
        blueIcon.image = "/images/mobilephone1.png";
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
		
        markerOptions = {
            icon: blueIcon
        };
        
        for (var i = 0; i < 100; i++) {
            var point = new GLatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
            getAddress(map, point);
        }
    
}

window.onload = init;

function getAddress(overlay, latlng){
    if (latlng != null) {
        address = latlng;
        geocoder.getLocations(latlng, showAddress);
    }
}

function showAddress(response){
	var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.iconSize = new GSize(16, 16);
		blueIcon.shadowSize = new GSize(0, 0);
		blueIcon.image = "/images/mobilephone1.png";
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();
		
    if (!response || response.Status.code != 200) {
		//alert("Status Code:" + response.Status.code);
		
		//var point = new GLatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
		//getAddress(map, point)
	}
	else {
		place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		markerOptions = {
			icon: blueIcon
		};
		
		map.addOverlay(new GMarker(point, markerOptions));
	}
}

