// LOAD THE MAP
$(document).ready(function() {
    loadMap();
});
// UNLOAD THE MAP
window.onunload = function(){ GUnload(); apts = null; };

// GLOBAL VARIABLES
var map = null; // the map
var basePin = null; // the marker
var apts = null;

function loadMap() {
	if (GBrowserIsCompatible()) {
		
		// CREATE THE MAP OBJECT
		map = new GMap2(document.getElementById("map"));
		map.addControl(new CLAControls());		
		
		// SET THE CENTRE AND ZOOM TO THE DEFAULT POSITION
		map.setCenter(new GLatLng(start_lat, start_lng), start_zoom);
		
		// SET UP THE PIN PLACEHOLDER
		basePin = new GIcon();
		basePin.shadow = "/gfx/map/pin-shadow.png";
		basePin.iconSize = new GSize(18, 28);
		basePin.shadowSize = new GSize(30, 28);
		basePin.iconAnchor = new GPoint(9, 28);
		basePin.infoWindowAnchor = new GPoint(9, 2);
		basePin.infoShadowAnchor = new GPoint(18, 25);
		basePin.imageMap = [2,5,16,5,16,16,9,22,2,16];
		basePin.transparent = "/gfx/map/pin-transparent.png";		
		
        // SET UP MARKER MOUSEOVER TOOLTIP DIV
        tooltip = document.createElement("div");
        tooltip.style.height = "15px";
        map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
		
		// SAVE THE MAP POSITION
		map.savePosition();
		// SAVE THE INITAL ZOOM LEVEL
		current_zoom = map.getZoom();
		
		// ADD THE APARTMENT MARKERS
		getApartments(location_id);
		
	}
	else {
	    alert("Sorry but your browser is not compatible with our maps");
	}
		
}

function getApartments(location) {
    apts = [];
    // get apartment markers
    $.get("/js/map_data.php", { type: "sub", id: location }, function(data) {
        var json = eval("(" + data + ")");
        var apartments = json.data;
        for (var i=0; i<apartments.length; i++) {
            setApartmentDetails(apartments[i], i);
        }
    });
}

function setApartmentDetails(apt, i) {    
    apts[i] = new Array();
    apts[i]['name'] = apt.name;
    apts[i]['class'] = apt.classname;
    apts[i]['link'] = "/"+apt.link+".php";
    apts[i]['folder'] = "/apts/"+apt.folder+"/";
    apts[i]['minstay'] = apt.minstay;
    apts[i]['lowrate'] = apt.lowrate;
    
    var point = new GLatLng(parseFloat(apt.lat), parseFloat(apt.lng));
    apts[i]['point'] = point;
    var pin = new GIcon(basePin);
    pin.image = "/gfx/map/pin-"+apts[i]['class']+".png";
    apts[i]['pin'] = pin;
    
    var marker = new GMarker(point, pin);
    // MAKE PIN LARGER ON MOUSEOVER
    GEvent.addListener(marker, "mouseover", function() {
        aptMarkerOver(i);
    });
    // MAKE IT SMALL ON MOUSEOUT
    GEvent.addListener(marker, "mouseout", function() {
        aptMarkerOut(i);
    });
    // MAKE IT OPEN INFO WINDOW ON CLICK
    GEvent.addListener(marker, "click", function() {
        aptInfoWindow(i);
    });
    
    apts[i]['marker'] = marker;
    
    map.addOverlay(marker);
}

function aptInfoWindow(i) {
    apts[i]['marker'].openInfoWindowHtml(getAptInfoWindow(i));
}

function aptMarkerOver(i) {
    apts[i]['marker'].setImage("/gfx/map/pin-"+apts[i]['class']+"-large.png");
    showTooltip(i);
}

function aptMarkerOut(i) {
    apts[i]['marker'].setImage("/gfx/map/pin-"+apts[i]['class']+".png");
    hideTooltip();
}













