﻿google.load("maps", "2", { "other_params": "sensor=true" });

function getIEVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

var ieVersion = getIEVersion();

function gotoDirections(fromLocation, toLocation) {
    if (!fromLocation) {
        if (navigator.geolocation) {
            //alert("using navigator");
            navigator.geolocation.getCurrentPosition(getGeoCallback_gotoDirectionsWithTo(toLocation), geoError);
        } else if (google.loader.ClientLocation) {
            //alert("using cl");
            var cl = google.loader.ClientLocation;

            //format the position to be the same as navigator.getCurrentPosition callback expects
            // cl also has address.city, address.region, and address.country
            var geoObject = new Object();
            geoObject.coords = new Object();
            geoObject.coords.latitude = cl.latitude;
            geoObject.coords.longitude = cl.longitude;
            getGeoCallback_gotoDirectionsWithTo(toLocation)(geoObject);
        } else {
            //couldn't get a location. try and see if user has set a location; if not, we'll just use lincoln and let them change it at google maps
            gotoDirections("Lincoln, IL", toLocation);
        }
    } else {
        //alert("actualy send to gmaps with from:'" + fromLocation + "' to:'" + toLocation + "'");
        //use &mrad= in url for additional destinations
        window.location = "http://www.google.com/maps?saddr=" + fromLocation + "&daddr=" + toLocation;
    }
}

function getGeoCallback_gotoDirectionsWithTo(toLocation) {
    return function geoCallback_gotoDirections(position) {
        //alert("geoCallback_gotoDirection with location '" + toLocation + "'");
        gotoDirections(position.coords.latitude + ", " + position.coords.longitude, toLocation);
    }
}

function geoError(error) {
    //alert("geoLocation error: " + error.message);
}


/*
function getRequestParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
*/
