﻿function SetMyLocation(address){
    ClearAllPolylines();
    MyLocationAddress = address;
    if (MyLocationMarker != undefined){
        MyLocationMarker.remove();
    }
    MyLocationChangeDiv.style.display = "none";
    MyLocationChangeAddressDiv.style.display = "none";
    MyLocationDiv.style.display = "block";
    document.getElementById("MyLocationAddressText").innerHTML = address;
    //MyLocationSearchVal.value = '';
    document.getElementById("LocationSetMessage").style.display = "block";
    document.getElementById("MapLegendHolder").style.display = "block";
    geocoder.getLatLng(address,function(point) {
        PerformGeoCode_MyLocation(point);
	});
}

function ChangeMyLocation(){
    map.closeInfoWindow();
    MyLocationChangeDiv.style.display = "block";
    MyLocationChangeAddressDiv.style.display = "none";
    MyLocationDiv.style.display = "none";
    MyLocationSearchVal.value = '';
    MyLocationSearchVal.focus();
}

function showAddressMatches(addressVal) {
    var htmlVal = "";
    if (addressVal ==""){
        alert("Please enter an address to begin.");
        MyLocationSearchVal.focus();
    }else{
        MyLocationSearchCloseBtn.style.display = "inline";
        directionsPanel.innerHTML = "";
        directionsPanel.style.display = "none";
        var search = addressVal;
        // ====== Perform the Geocoding ======        
        geocoder.getLocations(search, function (result){
            PerformGeoCode_AddressMatch(result, search);
        });
    }
}

function PerformGeoCode_MyLocation(point){
    var gi = new GIcon(baseIcon, "assets/images/google_home_red_X.png", null, null);
    gi.iconSize=new GSize(84,43);
    gi.iconAnchor=new GPoint(42,23);
    MyLocationPoint = point;
    MyLocationMarker = new GMarker(point, {icon: gi,zIndexProcess:importanceOrder,clickable: false});
           
    MyLocationMarker.importance = -100;
    ClearDirections();
    map.setCenter(point);
    map.setZoom(10);
    map.addOverlay(MyLocationMarker);
    if (mgr == undefined){
        document.getElementById("Loading").style.display = "block";
        window.setTimeout(setupMarkers, 500);
    }else{
        setTimeout('CheckForOnScreenLocations()',300);
    }
}

function PerformGeoCode_AddressMatch(result, search){
    if (result.Status.code == G_GEO_SUCCESS) {

        // ===== If there was more than one result, "ask did you mean" on them all =====
        if (result.Placemark.length > 1) { 
            MyLocationChangeDiv.style.display = "none";
            MyLocationChangeAddressDiv.style.display = "block";
            MyLocationDiv.style.display = "none";
            oldHTML = MyLocationDiv.innerHTML;

            htmlVal = "<div class='title'>Did you mean:</div><div id='addressChangeList'>";
            // Loop through the results
            for (var i=0; i<result.Placemark.length; i++) {
                if (i>0){
                    htmlVal += "<br>";
                }
                htmlVal += (i+1)+": <a href='javascript:SetMyLocation(" + AddDoubleQuotes(ClearUSA(result.Placemark[i].address)) + ")'>"+ ClearUSA(result.Placemark[i].address)+"</a>";
            }
            htmlVal += "<br><a href='javascript:SearchAgainForAddressMatches(oldHTML);'>Search Again</a></div><div class='separator'></div>";
            MyLocationChangeAddressDiv.innerHTML = htmlVal;
        }
        // ===== If there was a single marker =====
        else {
            SetMyLocation(ClearUSA(result.Placemark[0].address));
        }
    // ====== Decode the error status ======
    }else {
        var reason="Code "+result.Status.code;
        if (Array_AddressFailureReasons[result.Status.code]) {
            reason = Array_AddressFailureReasons[result.Status.code]
        } 
        alert('Could not find "'+search+ '" ' + reason);
        MyLocationChangeDiv.style.display = "block";
        MyLocationChangeAddressDiv.style.display = "none";
        MyLocationDiv.style.display = "none";
    }
}

function ClearUSA(text){
    return text.replace(", USA","");
}

function HideAddressMatchesSearch(){
    MyLocationChangeDiv.style.display = "none";
    MyLocationChangeAddressDiv.style.display = "none";
    MyLocationDiv.style.display = "block";
}

function SearchAgainForAddressMatches(html){
    MyLocationDiv.innerHTML = html;
    MyLocationSearchVal.value = '';
    MyLocationChangeDiv.style.display = "block";
    MyLocationChangeAddressDiv.style.display = "none";
    MyLocationDiv.style.display = "none";
}