﻿// GoogleMap Variables
var map;
var gmarkers = [];
var minLat;
var maxLat;
var minLon;
var maxLon;
var bounds;
var gdir;
var _Address;

function getdirections() {
    if (_Address) {
        gdir.load("from: " + $('.directionsfrom').val() + " to: " + _Address);
    }
    else {
        gdir.load("from: " + $('.directionsfrom').val() + " to: " + $('.directionsto').val());
    }
}

function gdir_handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_BAD_KEY) {
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    }
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    }
    else {
        alert("An unknown error occurred.");
    }
}

function map_refresh() {

    gmarkers = [];
    // initialize max and min lats and longs
    minLat = 90;
    maxLat = -90;
    minLon = 180;
    maxLon = -180;
    bounds = new GLatLngBounds();
}

function clearMarkers() {
    GEvent.clearListeners(marker, 'click');
    clearlisteners('click');
    map.removeOverlay(new GMarker());
}

function createMarker(latitude, longitude, mapHTML, icon) {

    if (icon) {
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "/desktopmodules/rentals/images/shadow-marker.png";
        baseIcon.iconSize = new GSize(23, 28);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.image = icon;

        markerOptions = {
            icon: baseIcon
        };
    } else {
        markerOptions = {};
    }

    var marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
    var point = new GLatLng(latitude, longitude);

    if (bounds) {
        bounds.extend(point);
        map.setZoom(map.getBoundsZoomLevel(bounds));
        map.setCenter(bounds.getCenter());
    }
    
    if (mapHTML !== '') {
        try {
            GEvent.addListener(marker, 'click', function() {
                marker.openInfoWindow(mapHTML);
            });
        }
        catch (Error) { } //its ok to fail here
    }

    gmarkers.push(marker);
    map.addOverlay(marker);
    return marker;
}

function map_init(address, latitude, longitude, zoom, caption) {
    
    if (address != '') {
        _Address = address;
    }
    
    if (typeof GBrowserIsCompatible == 'undefined') {
        alert('Google Maps api is not loaded');
        return;
    }

    if (GBrowserIsCompatible()) {
    
        map = new GMap2(document.getElementById("map_canvas"));
        if (latitude && longitude) {
            map.setCenter(new GLatLng(latitude, longitude), zoom);
            createMarker(latitude, longitude, caption);
        }
        else {
            var geocoder = new GClientGeocoder();
            geocoder.getLatLng(address, function(point) {
                if (!point) {
                    alert(address + " not found");
                }
                else {
                    map.setCenter(point, zoom);
                }
            });

        }
        map.setUIToDefault();

        directionsPanel = document.getElementById("route");
        gdir = new GDirections(map, directionsPanel);
        GEvent.addListener(gdir, "error", gdir_handleErrors);

        $('#getdirections').click(function() {
            getdirections();
        });

        map_refresh();
    }
}