﻿//declarations - these need to be set for this to work
var _map;
var _geocoder;
var _latlng;
var _latTxt;
var _lngTxt;
var _gmarkers = [];
var _bounds = new GLatLngBounds();

function createMap(mapId) {
    _map = new GMap2($(mapId)[0]);
}

function setBasicMap(latlng, zoom) {
    _map.setCenter(latlng, zoom);
    _map.setUIToDefault();
}

function setFixedMap(latlng, zoom) {
    _map.setCenter(latlng, zoom);
    _map.disableDragging();
}

function createGeocoder() {
    _geocoder = new GClientGeocoder();
}

function clearMarkers() {
    _map.clearOverlays();
}

function addBasicMarker(latlng, title) {
    markerOptions = { icon: getNavIcon() };

    marker = new GMarker(latlng, markerOptions);
    _map.addOverlay(marker);

    if (title != "" && title != null)
        marker.openInfoWindowHtml(title);
}

function addBrowseMarker(latlng, title, id, desc, img) {
    markerOptions = { icon: getNavIcon() };

    marker = new GMarker(latlng, markerOptions);
    _map.addOverlay(marker);
    
    if(img == "" || img == null)
        addMarkerWindow(marker, getWindowHtml(title, id, desc));
    else
        addMarkerWindow(marker, getWindowHtml(title, id, desc, img));

    _gmarkers.push(marker);
}

function addMarkerWindow(a_marker, a_html) {
    GEvent.addListener(a_marker, "click", function() {
        a_marker.openInfoWindowHtml(a_html);
    });
}

function getWindowHtml(title, id, desc) {

    var html = "<div class=\"navWindow\">" +
        "<div class=\"navWindowText\"><b><a href=\"/spot/show/" + id + "\">" + title + "</a></b>" + "<br />" + desc + "</div>" +
        "</div>";
    
    return html;
}

function getWindowHtml(title, id, desc, img) {

    var html = "<div class=\"navWindow\">" +  
        "<div class=\"navWindowImg\"><img src=\"" + img + "\" /></div>" +
        "<div class=\"navWindowText\"><b><a href=\"/spot/show/" + id + "\">" + title + "</a></b>" + "<br />" + desc + "</div>" +
        "</div>";

    return html;
}

function getNavIcon() {
    var navicon = new GIcon(G_DEFAULT_ICON);
    navicon.image = "/Content/Images/marker.png";
    navicon.iconSize = new GSize(16, 36);

    return navicon;
}

function setMapByText(textId) {
    setAddress($(textId)[0].value);
}

function setCenterByText(textId) {
    if($(textId)[0].value != '')
        setMapCenterByAddress($(textId)[0].value);
}

function setMapCenterByAddress(address) {
    _map.clearOverlays();

    if (_geocoder == null)
        createGeocoder();

    _geocoder.getLatLng(address, setMapCenter);
}

function setMapCenter(point) {
    if (!point) {
        alert("Address not found");
    } else {
        _map.setCenter(point, 15);
    }
}

function setAddress(address) {
    _map.clearOverlays();

    if (_geocoder == null)
        createGeocoder();

    _geocoder.getLatLng(address, setPoint);
}

function setPoint(point) {
    if (!point) {
        alert("Address not found");
    } else {
        _map.setCenter(point, 15);

        var marker = new GMarker(point);
        _map.addOverlay(marker);

        //var latlng = marker.getLatLng();

        _latTxt.value = point.lat();
        _lngTxt.value = point.lng();

        //Try and get an address from the coordinates to fill in the hidden textboxes
        _geocoder.getLocations("(" + point.lat() + ", " + point.lng() + ")", getPlace);
    }
}

function getPlace(response) {
    if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
    } else {
        place = response.Placemark[0];
    }

    //This function is in the ExtendedLoc control
    setPlace(place, false);
}


function showMapWindow(num) {
    GEvent.trigger(_gmarkers[num], "click");
}

function setMapBoundaries() {

    for (var i = 0; i < _gmarkers.length; i++) {
        _bounds.extend(_gmarkers[i].getLatLng());
    }

    _map.setZoom(_map.getBoundsZoomLevel(_bounds));
    _map.setCenter(_bounds.getCenter());
}


//********************************************************************
//Get location by Browser function
//********************************************************************
function getUserLocation(a_function) {
    tryByGeoApi(a_function);
}

function tryByGeoApi(a_function) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            _latlng = new GLatLng(position.coords.latitude, position.coords.longitude);
            eval(a_function);
        },
                function(error) {
                    tryByGears(a_function);
                },
                { timeout: 100 });
    } else {
    tryByGears(a_function);
    }
}

function tryByGears(a_function) {
    if (typeof (google.gears) == 'object') {
        var geo = google.gears.factory.create("beta.geolocation");
        geo.getCurrentPosition(function(position) {
            _latlng = new GLatLng(position.latitude, position.longitude);
            eval(a_function);
        },
                function() {
                    tryByIP(a_function);
                });
    } else {
    tryByIP(a_function);
    }
}

function tryByIP(a_function) {
    var ipLat = geoip_latitude();
    var ipLng = geoip_longitude();

    if (ipLat != null && ipLng != null) {
        _latlng = new GLatLng(ipLat, ipLng);
        eval(a_function);
    } else {
        tryByCookie(a_function);
    }

}

function tryByCookie(a_function) {
    var clat = $.cookie("lat");
    var clng = $.cookie("lng");

    if (clat && clng) {
        _latlng = new GLatLng(clat, clng);
        eval(a_function);
    } else {
        tryByHomeBase(a_function);
    }
}

function tryByHomeBase(a_function) {
    jQuery.ajax({
        type: "GET",
        async: false,
        url: "/NavMember/HomeBase/",
        success: function(data, status, req) {
            if (data != null && data != "") {
                if (_geocoder == null)
                    createGeocoder();

                if (_geocoder) {
                    _geocoder.getLatLng(data, function(point) {
                        _latlng = point;
                        eval(a_function);
                    });
                }

            }
        },
        error: function(req, status, error) {
            _latlng = null;
        }
    });
}

