﻿// JScript File

var GoogleMap = null;
var GoogleGeoCoder = null;

var SearchLocationCountry = "";
var SearchLocationRegion = "";
var SearchLocationCity = "";
var SearchLocationAddress = "";
var SearchLocationPostCode = "";

function InitGoogleMaps(){

    if (GBrowserIsCompatible()) {
        GoogleMap = new GMap2(document.getElementById("GoogleMap"));
        GoogleMap.addControl(new GLargeMapControl());
        GoogleMap.setCenter(new GLatLng(GlobalLat, GlobalLong), 13);
        GoogleGeoCoder = new GClientGeocoder();
        
        GEvent.addListener(
            GoogleMap, 
            "click", 
            function(overlay, point) {
                if(overlay == null){
                    GoogleMap.clearOverlays();
                    objGLatLng = SetMarkerPoint(point, null);
                    parent.MarkerSet(objGLatLng.lat(), objGLatLng.lng());
                }
            }
        );
    }
    
}

function GetIcon(mode){
    var icon = new GIcon();
    icon.image = "/images/map-marker" + mode + ".png";
    icon.iconSize = new GSize(39, 48);
    icon.iconAnchor = new GPoint(22, 42);
    icon.infoWindowAnchor = new GPoint(0, 0);
    return icon;
}

function clsOhhMarker(mLat, mLong, mode, linkId){
    this.oLat = mLat;
    this.oLong = mLong;
	this.linkRef = linkId;
	this.events = new Array();

    this.inheritFrom = GMarker;
    this.inheritFrom(new GLatLng(this.oLat, this.oLong), {zIndexProcess:GetOrder, icon:GetIcon(mode)});
    
    GEvent.bind(this, "mouseover", this, this.onMouseOver);
    GEvent.bind(this, "mouseout",  this, this.onMouseOut);
    GEvent.bind(this, "click",  this, this.onClick);
}

clsOhhMarker.prototype = new GMarker(new GLatLng(1, 1));

clsOhhMarker.prototype.onClick = function() {
    
}

var tempZIndex;

clsOhhMarker.prototype.onMouseOver = function() {
    parent.HighlightLink(this.linkRef);
    this.setImage("/images/map-marker.png");
};

clsOhhMarker.prototype.onMouseOut = function() {
    parent.DimLink(this.linkRef);
    this.setImage("/images/map-marker-off.png");
};

clsOhhMarker.prototype.onClick = function() {
    parent.ClickLink(this.linkRef);
};

var vMarkers = new Array();

function AddMarker(mLat, mLong, linkId){
    var marker = new clsOhhMarker(mLat, mLong, "-off", linkId);
    vMarkers.push(marker);
     
    GoogleMap.addOverlay(marker);
}

function HighLightMapMarker(markerId){
    for(i=0; i<vMarkers.length; i++){
        if(vMarkers[i].linkRef == markerId){
            vMarkers[i].onMouseOver();
        }
    }
}

function KillMapMarker(markerId){
    for(i=0; i<vMarkers.length; i++){
        if(vMarkers[i].linkRef == markerId){
            vMarkers[i].onMouseOut();
        }
    }
}

function GetOrder(marker,b) {
    return -GOverlay.getZIndex(marker.getPoint().lat());
}




function AttemptAutoLocate(){
    if(SearchLocationPostCode != "" || SearchLocationCountry != "" || SearchLocationRegion != "" || SearchLocationCity != "" || SearchLocationAddress != ""){
    
        var strAddress = SearchLocationAddress;
        strAddress += (SearchLocationAddress == "") ? "" : ", ";
        strAddress += SearchLocationCity;
        strAddress += (SearchLocationCity == "") ? "" : ", ";
        if(SearchLocationCity != SearchLocationRegion){
            strAddress += SearchLocationRegion;
            strAddress += (SearchLocationRegion == "") ? "" : ", ";
        }
        strAddress += SearchLocationPostCode;
        strAddress += (SearchLocationPostCode == "") ? "" : ", ";
        strAddress += SearchLocationCountry;
        
    
        if(window.confirm("Do you wish to try to auto-locate your map position with the following address?\n\n" + strAddress)){
            showAddress(strAddress);
        }
        
    } else {
        var strError = "";
        if(SearchLocationCountry == "") strError += "Country\n";
        if(SearchLocationRegion == "") strError += "Region\n";
        if(SearchLocationCity == "") strError += "City\n";
        if(SearchLocationAddress == "") strError += "Address\n";
        if(SearchLocationPostCode == "") strError += "Postcode/ZIP\n";
        
        alert("Please specify some of the following first:\n\n" + strError);
    }
}



function showAddress(address) {

    if (GoogleGeoCoder) {
    
        GoogleGeoCoder.getLocations(
            address,
            function(response) {
                
                if(response.Placemark){

                    if(response.Placemark.length > 1){
                        //erk, multiple addresses, let user pick
                        
                        var addresses = "";
                        
                        for(i=0; i<response.Placemark.length; i++){
                            addresses += "(" + (i+1) + ") " + response.Placemark[i].address.toString() + ". ";
                        }
                        
                        var chosenOption = "";
                        
                        while (chosenOption == "")
                        {
                            chosenOption = window.prompt("Multiple results were retrieved, please type the number of the address that matches your location: " + addresses, 1);
                        }

                        try{
                            SetMarkerPlacemark(response.Placemark[parseInt(chosenOption) - 1]);
                        } catch (e) {
                            alert("Not Valid");
                        }
                         
                    } else {
                        if(window.confirm("Address found - do you wish to set this as your location?")){
                            SetMarkerPlacemark(response.Placemark[0]);
                        }
                    }
                } else {
                    alert("Unfortunately, the address could not be found. Please use the map controls to locate it yourself");
                }
            }
            
        );
    }
}

function SetMarkerPlacemark(placemark){

    point = new GLatLng(placemark.Point.coordinates[1], placemark.Point.coordinates[0]);
    parent.MarkerSet(placemark.Point.coordinates[1], placemark.Point.coordinates[0]);

    SetMarkerPoint(point);
    GoogleMap.setCenter(point);
}

var TrueCenter;

function SetMarkerPosition(latitude, longtitude, zoomlevel){
    GoogleMap.clearOverlays();
    TrueCenter = new GLatLng(latitude, longtitude);
    SetMarkerPoint(TrueCenter, zoomlevel);
    GoogleMap.setCenter(TrueCenter, zoomlevel);
}


function SetMarkerPoint(point, zoomLevel){
    var marker = new GMarker(point, {zIndexProcess:GetOrder, icon:GetIcon("")} );
    GoogleMap.addOverlay(marker);
    
    return marker.getPoint();
}

function SetMapPosition(latitude, longtitude, zoomlevel){
   GoogleMap.setCenter(new GLatLng(latitude, longtitude)); //, zoomlevel
}
