window.onunload = function() {
    GUnload();
};

/* Global Variables */
var map = null; // the map object

/* GeoCoding Variables */
var geocoder = null; // the geocoding object
var london_bounds = null; // the bounds of london
var places = null; // all the results from an address search
var search_marker = null; // the marker that appears on the map after a search
var search_address = null; // the originally searched address

/* Directions Variables */
var directions = null; // the directions object
var directions_from = null; // the original from address
var directions_to = null; // the original to address

/* Hotels and Apts */
var hotelMarkers = null;
var aptMarkers = null;
var theatreMarkers = null;

/* Map Icons */
var propertyIcon = null; // icon for hotels and apartments
var locationIcon = null; // icon for address searches


/* Default Map Settings */
var initial = new Array();
initial['lat'] = 51.499339;
initial['lng'] = -0.10746;
initial['zoom'] = 12;$(document).ready(function() {
    loadMap();
});

/**
 * Initialises the map
 */
function loadMap() {
    if (GBrowserIsCompatible()) {
        // load the map at the default position
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.enableScrollWheelZoom();
        
        map.setCenter(new GLatLng(initial['lat'], initial['lng']), initial['zoom']);
        // set the bounds of London
        london_bounds = setLondonBounds();
    }
    else {
        $('#map').html("Sorry your browser is not compatible with our maps");
    }
}

/**
 * Sets the bounds of London
 */
function setLondonBounds() {
    var bounds = new GLatLngBounds();
    bounds.extend(new GLatLng(51.339192, -0.532837));
    bounds.extend(new GLatLng(51.684477, 0.156555));
    return bounds;
}

/* Toggle between Options and Directions */
function toggleMapNav(clicked, directions) {
    if (clicked == "options") {
        $('#options').show(); $('#options_tab').removeClass("inactive");
        $('#directions').hide(); $('#directions_tab').addClass("inactive");
        if (directions == true) {
            $('#directions_tab').html("Directions");
        }
    }
    else {
        if ($('#directions_tab > a').size() == 0) {
            $('#directions_tab').html("<a href='#' onclick='toggleMapNav(\"directions\"); return false;'>Directions</a>");
        }
        $('#directions').show(); $('#directions_tab').removeClass("inactive");
        $('#options').hide(); $('#options_tab').addClass("inactive");
    }
    
}

/* Map Nav Toggle */
function hideShowMapNav() {
    var nav = $('#toggle');
    if (nav.attr("class") == "hidenav") {
        $('#tabs').hide();
        $('#nav').animate( { width:"0px" }, 500, "",
            function() {
                nav.attr("class", "shownav").attr("title", "Show the navigation");
            }
        );
        $('#map').animate( { marginLeft:"10px" }, 500);
    }
    else {
        $('#nav').animate( { width:"250px" }, 500, "",
            function() {
                $('#tabs').show();
                nav.attr("class", "hidenav").attr("title", "Hide the navigation");
            }
        );
        $('#map').animate( { marginLeft:"260px" }, 500);
    }
}

/***************************
 DATABASE LOGGING FUNCTIONS
 **************************/

function logAddressSearch(code, number) {
    $.ajax({
        type: "POST",
        url: "/ajax/address.php",
        cache: false,
        data: "address="+search_address+"&code="+code+"&number="+number,
        success: function(data, textStatus) {
            //GLog.write(data);
        }
    });
}

function logDirections() {
    $.ajax({
        type: "POST",
        url: "/ajax/directions.php",
        cache: false,
        data: "from="+directions_from+"&to="+directions_to,
        success: function(data, textStatus) {
            //GLog.write(data);
        }
    });
}