﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="http://maps.google.com/maps?oe=utf-8&file=api&v=2&client=gme-adventurecentral&key=ABQIAAAAEU0ISWVAeBQ7-wROnH1IUxTrJbCN3QXFE2rPv57U0C9XaeIuFRQvb_fLxuWdH616FKv4PMumxQQefQ" />

var gicons;
var aaBounds;
var wrBounds;
var cnBounds;
var mapRef;
var geocoder = new GClientGeocoder();
var zoomLevels = { TL: 0, WR: 1, CN: 2, AA: 3 }
var curZoomLevel = zoomLevels.TL;
var countryName = "";
var aaName = "";
var clickPoint = "";
var resultMarkers = new Array();

function zoomToBounds(bounds) {
    newZoom = mapRef.getBoundsZoomLevel(bounds);
    newZoom = newZoom - 1;
    mapRef.setZoom(newZoom);
    mapRef.setCenter(bounds.getCenter());
}

function zoomAndCenter() {
    //mapRef.setZoom((mapRef.getZoom() < 8) ? mapRef.getZoom() + 1 : 8);
    mapRef.zoomIn();
    mapRef.panTo(clickPoint);
}

function zoomToResultMarker(resultNumber) {
    var markerIndex = resultNumber - 1;
    mapRef.setZoom(8);
    mapRef.removeOverlay(resultMarkers[markerIndex]);
    mapRef.addOverlay(resultMarkers[markerIndex]);
    mapRef.setCenter(resultMarkers[markerIndex].getPoint());
}

function onLocationFound(response) {
    responseLatLong = response.name.split(",");

    if (!response || response.Status.code != 200) {
        // Geocode failed
        zoomAndCenter();
    } else {
        // Geocode successful

        // Placemarks are ordered most to least specific, safe to assume last placemark is country
        country = response.Placemark[response.Placemark.length - 1];
        countryBounds = new GLatLngBounds(new GLatLng(country.ExtendedData.LatLonBox.south, country.ExtendedData.LatLonBox.west), new GLatLng(country.ExtendedData.LatLonBox.north, country.ExtendedData.LatLonBox.east))
        countryName = (country.AddressDetails.Country) ? country.AddressDetails.Country.CountryName.toUpperCase() : "";

        // 2nd to last placemark is administrative area... maybe
        adminArea = (response.Placemark.length > 1) ? response.Placemark[response.Placemark.length - 2] : "";

        if (adminArea != "" && adminArea.AddressDetails.Country && adminArea.AddressDetails.Country.AdministrativeArea) {
            aaName = adminArea.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName.toUpperCase();
            aaBounds = new GLatLngBounds(new GLatLng(adminArea.ExtendedData.LatLonBox.south, adminArea.ExtendedData.LatLonBox.west), new GLatLng(adminArea.ExtendedData.LatLonBox.north, adminArea.ExtendedData.LatLonBox.east))
        } else {
            adminArea = "";
            aaName = "";
            aaBounds = "";
        }

        if (curZoomLevel == zoomLevels.TL) {
            // Top level zoom to world region
            curZoomLevel = zoomLevels.WR;
            for (var wr in wrBounds) {
                if (wrBounds[wr].contains(clickPoint)) {
                    zoomToBounds(wrBounds[wr]);
                    break;
                }
            }
        } else if ((curZoomLevel == zoomLevels.WR && countryName != "") || (curZoomLevel == zoomLevels.AA && aaName == "")) {
            // World Region zoom to country
            curZoomLevel = (countryName == "USA") ? zoomLevels.CN : zoomLevels.WR;
            zoomToBounds(countryBounds);

            if (aaName == "") {
                showResultMarkers();
            }
        } else if (aaName != "") {
            // Country zoom to Administrative Area
            curZoomLevel = zoomLevels.AA;
            zoomToBounds(aaBounds)
            showResultMarkers();

        } else {
            zoomAndCenter();
        }
    }
}

function onMapClick(overlay, latlng) {
    if (latlng != null) {
        clickPoint = latlng;
        geocoder.getLocations(latlng, onLocationFound);
    } else {
        clickPoint = "";
    }
}

function onZoomEnd(oldLevel, newLevel) {
    //alert("old: " + oldLevel + "\nnew: " + newLevel);
    if (oldLevel > newLevel) {
        if (newLevel == 0) {
            mapRef.setCenter(new GLatLng(0, 0), 0);
            mapRef.clearOverlays();
            curZoomLevel = zoomLevels.TL;
        } else if (newLevel == 1) {
            mapRef.clearOverlays();
            curZoomLevel = zoomLevels.WR;
        } else if (newLevel == 2 || newLevel == 3) {
            mapRef.clearOverlays();
            curZoomLevel = zoomLevels.CN;
        } else if (newLevel >= 4) {
            curZoomLevel = zoomLevels.AA;
        }
    }

    if (newLevel > oldLevel) {
        if (newLevel == 1) {
            mapRef.clearOverlays();
            curZoomLevel = zoomLevels.WR;
        } else if (newLevel == 2 || newLevel == 3) {
            mapRef.clearOverlays();
            curZoomLevel = zoomLevels.CN;
        } else if (newLevel >= 4) {
            curZoomLevel = zoomLevels.AA;
            showResultMarkers();
        }
    }
}

function createMarker(point, iconRef, title) {
    markerOptions = { icon: iconRef, title: title, clickable: false };
    var marker = new GMarker(point, markerOptions);

    return marker;
}

var resultMarker = function(resultNum, lat, lng) {
    this.resultNum = resultNum;
    this.lat = lat;
    this.lng = lng;
}

function showResultMarkers() {
    mapRef.clearOverlays();
    var bounds = new GLatLngBounds();

    for (x = 0; x < resultMarkers.length; x++) {
        bounds.extend(resultMarkers[x].getPoint());
        mapRef.addOverlay(resultMarkers[x]);
    }

    // zoom to marker extents
    zoomToBounds(bounds);
}

function initRefArrays() {
    //Reference icon used to define common properties for all icons
    referenceIcon = new GIcon(G_DEFAULT_ICON);
    referenceIcon.shadow = "";
    referenceIcon.iconSize = new GSize(25, 25);

    // Custom marker icons
    gicons = {
        'aclogo': new GIcon(referenceIcon, "/travel/Images/MapIcons/icon_ACLogo2.png"),
        '1': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_01.png"),
        '2': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_02.png"),
        '3': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_03.png"),
        '4': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_04.png"),
        '5': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_05.png"),
        '6': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_06.png"),
        '7': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_07.png"),
        '8': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_08.png"),
        '9': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_09.png"),
        '10': new GIcon(referenceIcon, "/travel/Images/MapIcons/Orange_10.png")
    }

    // World Regions        
    wrBounds = {
        'NA': new GLatLngBounds(new GLatLng(8.407168163601074, -171.5625), new GLatLng(71.85622888185527, -50.2734375)),
        'SA': new GLatLngBounds(new GLatLng(-56.55948248376223, -119.53125), new GLatLng(33.13755119234614, -33.3984375)),
        'AF': new GLatLngBounds(new GLatLng(-35.7465122599185, -21.4453125), new GLatLng(37.43997405227057, 53.7890625)),
        'AS': new GLatLngBounds(new GLatLng(7.710991655433229, 36.5625), new GLatLng(78.06198918665973, -172.96875)),
        'EU': new GLatLngBounds(new GLatLng(35.02999636902565, -14.765625), new GLatLng(71.24435551310674, 44.82421875)),
        'AU': new GLatLngBounds(new GLatLng(-51.618016548773696, 93.1640625), new GLatLng(13.239945499286312, -160.3125)),
        'CA': new GLatLngBounds(new GLatLng(9.795677582829743, -85.78125), new GLatLng(27.76132987450523, -57.744140625)),
        'AN': new GLatLngBounds(new GLatLng(-84.67351256610523, -158.203125), new GLatLng(-63.54855223203643, 174.375)),
        'ME': new GLatLngBounds(new GLatLng(4.565473550710291, 28.125), new GLatLng(55.578344672182055, 93.515625))
    }
}

// A TextualZoomControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).
function TextualZoomControl() {
}
TextualZoomControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
TextualZoomControl.prototype.initialize = function(map) {
    var container = document.createElement("DIV");
    container.style.width = "15px";

    var zoomOutDiv = document.createElement("IMG");
    zoomOutDiv.setAttribute("src", "/travel/images/mapicons/zoomout-16.gif");
    zoomOutDiv.setAttribute("alt", "Zoom Out");
    this.setButtonStyle_(zoomOutDiv);
    GEvent.addDomListener(zoomOutDiv, "click", function() {
        map.zoomOut();
    });

    var zoomInDiv = document.createElement("IMG");
    zoomInDiv.setAttribute("src", "/travel/images/mapicons/zoomin-16.gif");
    zoomInDiv.setAttribute("alt", "Zoom In");
    this.setButtonStyle_(zoomInDiv);
    GEvent.addDomListener(zoomInDiv, "click", function() {
        map.zoomIn();
    });

    container.appendChild(zoomInDiv);
    container.appendChild(zoomOutDiv);

    map.getContainer().appendChild(container);
    //map.getContainer().insertBefore(container,map.getContainer().firstChild);
    return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
TextualZoomControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}

// Sets the proper CSS for the given button element.
TextualZoomControl.prototype.setButtonStyle_ = function(button) {
    button.style.textDecoration = "none";
    button.style.cursor = "pointer";
    //button.style.display = "inline";
}

function initMap(mapDiv) {
    mapRef = new GMap2(document.getElementById(mapDiv));
    mapRef.setCenter(new GLatLng(0, 0), 0);
    mapRef.setMapType(G_PHYSICAL_MAP);
    mapRef.addControl(new TextualZoomControl());

    //map.enableGoogleBar();
    mapRef.enableScrollWheelZoom();
    mapRef.enableContinuousZoom();

    //GEvent.addListener(mapRef, "click", onMapClick);
    //GEvent.addListener(mapRef, "zoomend", onZoomEnd)
}