﻿var map = null;
var shapeLayer = null;
var idArray = new Array();

function CreateMap() {
    map = new VEMap('RSLMap');
    map.onLoadMap = AddLocations;
    map.LoadMap(new VELatLong(-19.634293, 145.469727));
    map.SetZoomLevel(5);
    map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
    shapeLayer.SetClusteringConfiguration(VEClusteringType.Grid);
}

function ShowLocation(lat, lon, marker, locName) {
    map.SetCenterAndZoom(new VELatLong(lat, lon + 0.02), 13);
    map.ShowInfoBox(marker);
    document.getElementById('txtAddressRSL').value = locName;
}

function findValue(li) {
    if (li == null) return alert("No match!");
    if (!!li.extra) var sValue = li.extra[0];
    else var sValue = li.selectValue;
    var pinObject = idArray[sValue].split("|");
    ShowLocation(parseFloat(pinObject[1]), parseFloat(pinObject[2]), eval(pinObject[0]))
}

function selectItem(li) {
    findValue(li);
}

function formatItem(row) {
    return row[0] + " (id: " + row[1] + ")";
}

function lookupLocal() {
    var oSuggest = $("#CityLocal")[0].autocompleter;
    oSuggest.findValue();
    return false;
}

function doClick(e) {
	var key;
	if (navigator.appName.indexOf("Internet Explorer") > 0) key = e.keyCode; else key = e.which;
    if (key == 13) lookupLocal();
}

function doDirections(e) {
	var key;
	if (navigator.appName.indexOf("Internet Explorer") > 0) key = e.keyCode; else key = e.which;
    if (key == 13) ShowDirections();
}

function setDirectionText() {
    setTimeout("document.getElementById('txtAddressRSL').value = document.getElementById('CityLocal').value", 1000)
}

function ShowDirections() {
    var fValue = document.getElementById('txtAddress').value;
    var tValue = document.getElementById('txtAddressRSL').value;
    var pinObject = idArray[tValue].split("|");
    var options = new VERouteOptions();
    options.RouteCallback = onGotRoute;
    map.GetDirections([fValue, new VELatLong(parseFloat(pinObject[1]), parseFloat(pinObject[2]))], options);
}

function onGotRoute(route) {
    var legs = route.RouteLegs;
    var turns = "Total distance: " + route.Distance.toFixed(1) + " mi\n";
    var numTurns = 0;
    var leg = null;

    for (var i = 0; i < legs.length; i++) {
        leg = legs[i];
        var turn = null;
        for (var j = 0; j < leg.Itinerary.Items.length; j++) {
            turn = leg.Itinerary.Items[j];
            numTurns++;
            turns += numTurns + ".\t" + turn.Text + " (" + turn.Distance.toFixed(1) + " km)\n";
        }
    }
    alert(turns);
}

animatedcollapse.addDiv('rslDirections', 'fade=1,height=100')
animatedcollapse.init()

$(document).ready(function() {
    $("#CityAjax").autocomplete(
		"autocomplete_ajax.cfm",
		{
		    delay: 10,
		    minChars: 2,
		    matchSubset: 1,
		    matchContains: 1,
		    cacheLength: 10,
		    onItemSelect: selectItem,
		    onFindValue: findValue,
		    formatItem: formatItem,
		    autoFill: true
		}
	);

    $("#CityLocal").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Type the RSL Sub Branch name or location here"
        }).removeClass("watermarkOn").val("");
    });

    $("#CityLocal").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).addClass("watermarkOn").val("Type the RSL Sub Branch name or location here");
    });
});
