﻿function hideResultsPane() {
    $('#resultspane').hide('fold', 400, function () {
        $('#resultspane').addClass('panehide');
        $('#searchfunctions').removeClass('panehide');
        $('#searchfunctions').show('blind');
    });
}
function hideFunctionsPane() {
    $('#searchfunctions').hide('fold', 400, function () {
        $('#searchfunctions').addClass('panehide');
        $('#resultspane').removeClass('panehide');
        $('#resultspane').show('blind');
    });
}
//The default initializer for the locale text.
function setHIText() {
    setStaticText();
    $('#csotoggle').attr('checked', false);
    $('#resultspane').empty();
    $('#resultspane').removeClass("activeresults");
    $('#title').html(l('%paycentertitle'));
    $('#maincontent').html("<div id='ph1'></div><div id='ph2'></div><div id='ph3'></div><div id='ph4'></div>");
    $("#ph1").html(l('%paycentertext1'));
    $("#ph2").html(l('%paycentertext2'));
    $("#ph3").html(l('%paycentertext3'));
    $("#ph4").html(l('%paycentertext4'));
    textcallback = setHIText;
}
function setCSOText() {
    setStaticText();
    $('#csotoggle').attr('checked', true);
    $('#title').html(l('%custservicetitle'));
    $('#maincontent').html("<div id='ph1'></div><div id='ph2'></div><div id='ph3'></div><div id='ph4'></div><div id='ph5'></div><div id='ph6'></div>");
    $("#ph1").html(l('%custservice1'));
    $("#ph2").html(l('%custservice2'));
    $("#ph3").html(l('%custservice3'));
    $("#ph4").html(l('%hours'));
    $("#ph5").html(l('%hourstext1'));
    $("#ph6").html(l('%hourstext2'));
    textcallback = setCSOText;
}
//Text that won't change regardless of whether we are in CSO or PC mode
function setStaticText() {
    $('#addresserror').html('');
    $("#wthin").html(l('%wthin'));
    $("#srchhead").html(l('%addresssearch'));
    $("#directionsgo").html(l('%directionsgo'));
    $('#directions').html(l('%directions'));
    $("#dirlink").text(l('%dirlink'));
    $("#resultstitle").text(l('%resultstitle'));
    $("#avail").text(l('%avail'));
    $('#partial').text(l('%partial'));
    $('#unavail').text(l('%unavail'));
    $('#returntomain').html(l('%returntomain'));
    $('#subtable').html(l('%subtable'));
    $("#addresssearch").html(l('%addresssearch'));
    $("#maintitle").text(l('%maintitle'));
    $("#csotext").text(l('%csotext'));
    $("#dirlink").text(l('%dirlink'));
    $("#unknown").text(l('%unknown'));
    $("#language").html(l('%language'));
    $("#legendtext").text(l('%legendtext'));
    $("#directionsgo").button({ label: l('%directionsgo') });
    $("#addresssearch").button({ label: l('%addresssearch') });
    $("#clear").button({ label: l('%clear') });
    if (Modernizr.geolocation) {
        $('#uselocation').html(l('%userlocation'));
    }
    $("#address").addClass("lighttext")
    .val(l("%searchwmtext"))
    .focus(function () {
        $(this).val() == l('%searchwmtext') ? $(this).removeClass("lighttext").val('') : null;
    })
    .blur(function () {
        $(this).val() == "" ? $(this).val(l("%searchwmtext")).addClass("lighttext") : null;
    });
    $("#from").addClass("lighttext")
    .val(l("%fromtext"))
    .focus(function () {
        $(this).val() == l('%fromtext') ? $(this).removeClass("lighttext").val('') : null;
    })
    .blur(function () {
        $(this).val() == "" ? $(this).val(l("%fromtext")).addClass("lighttext") : null;
    });
    $("#to").addClass("lighttext")
    .val(l("%totext"))
    .focus(function () {
        $(this).val() == l('%totext') ? $(this).removeClass("lighttext").val('') : null;
    })
    .blur(function () {
        $(this).val() == "" ? $(this).val(l("%totext")).addClass("lighttext") : null;
    });
}
//Sets the initial defaults
function setHIDefaults() {
    var csocheck = /OfficeLocations/i;
    if (csocheck.test(window.location.toString()) && ftcsoredirect) {
        setCSODefaults();
        ftcsoredirect = false;
    }
    else {
        setHIText();
        getlocnb == 0 ? loadMapDefaults() : loadSingleEntity();
    }
}
function setCSODefaults() {
    setCSOText();
    loadCSOMap();
}
//Add the handler and text for the return menu
function returnText() {
    $('#resultspane').append('<h3>' + l('%resultstitle') + '</h3>');
    $('#resultspane h3').append("<p><a id='rtrn' href='javascript:;'>" + l('%rtrntext') + "</a></p>");
    $('#rtrn').click(setHIDefaults);
}
function loadResultsDiv(res) {
    $('#resultspane').empty();
    hideFunctionsPane();
    returnText();
    var n = 1;
    for (var i = 0; i < res.length; i++) {
        $('#resultspane').append("<div id='rdiv" + n + "'></div>");
        $('#rdiv' + n).html(getResultContent(res[i], n));
        $('#rdiv' + n).click(res[i], function (r) { zoomToPayCenter(r.data); })
        .hover(function () { $(this).css("background-color", "#9B9B9B") },
        function () { $(this).css("background-color", "White") });
        n++;
    }
    $('#resultspane').addClass("activeresults");
}
/*-----------------------------------------------------------------------------
Event Handlers 
-----------------------------------------------------------------------------*/
$('#uselocation').click(function () {
    navigator.geolocation.getCurrentPosition(function (position) {
        var usetheloc = castAndQuery(position);
        loadResultsDiv(usetheloc);
    }, showGeoLocationError);
});
$('#language').click(function () {
    var newloc = l('%languageswitch');
    switchLocale(newloc);
    mapcallback();
    textcallback();
});
$('#csotoggle').click(function () {
    var checkvalue = $(this).attr('checked');
    if (checkvalue) {
        //get entities that are cso offices.
        setCSODefaults();
    }
    else {
        //get all the entities.
        setHIDefaults();
    }
});
$('#addresssearch').click(function () {
    var locparam = $('#address').val();
    if (locparam != l('%searchwmtext') && locparam != "") {
        var distancepar = $("#radius").val();
        var results = performGeoCoding(locparam, distancepar);
        loadResultsDiv(results);
    }
    else {
        $('#addresserror').html(l('%srchblank'));
    }
});
//Removes all results and resets
$("#clear").click(function () {
    setHIDefaults();
    $('#address').val('');
});
//Handler for the directions
$("#directionsgo").click(function () {
    $('#resultspane').empty();
    $('#resultspane').removeClass("activeresults");
    hideFunctionsPane();
    var tp = $('#to').val();
    var fp = $('#from').val();
    if (tp != "" && fp != "") {
        $('#resultspane').fadeOut();
        $('#resultspane').empty();
        getDirections(tp, fp);
        returnText();
        $('#resultspane').fadeIn();
    }
});
//////////////////////////////////////////////////////////////////////////////
function closethebox () {
    if (Modernizr.touch) {
        mapcallback;
    }
    else {
        map.entities.remove(thebox);
    }
}
function loadcre(data) {
    cred = data;
}
//we use this localeswitched value as our "page load" because we don't want our 
//DOM manipulated after a manual locale switch.
$(function () {
    $.ajax({
        url: creurl,
        cache: false,
        async: false,
        type: "POST",
        datatype: 'text',
        success: loadcre
    });
    //We know what we're looking for, no need to abstract it
    var urlswitch = /lang=/i;
    var arr = urlswitch.exec(window.location.toString());
    arr !== null ? switchLocale(window.location.toString().substring(arr.index + 5, arr.index + 7)) : null;
    if (arr !== null) {
        var sub = window.location.toString();
        var start = arr.index + 5;
        var finish = arr.index + 7;
        switchLocale(sub.substring(start, finish));
    }
    if (isIE()) {
        setTimeout(setHIDefaults, 500);
    }
    else {
        setHIDefaults();
    }
});
