﻿var _timeoutPeriod = 10 * 60 * 1000;
var _resumePeriod;
var _timeoutHandle;
var _resumeHandle;
function StartTimeoutTimer() {
    clearTimeout(_timeoutHandle);
    clearTimeout(_resumeHandle);
    if (window.location.href.indexOf("OnlineBooking.aspx") < 0) return;
    $("#TimeoutNotice").hide();
    _resumePeriod = 30;
    $("#_ExitCounter").html("Exiting in " + _resumePeriod + " seconds");
    _timeoutHandle = setTimeout("ShowTimeoutNotice()", _timeoutPeriod);
}

function ShowTimeoutNotice() {
    clearTimeout(_timeoutHandle);
    clearTimeout(_resumeHandle);
    $("#TimeoutNotice").height($(document).height());
    $("#TimeoutNotice").show()
    _resumeHandle = setTimeout("DisplayTimeout()", 1000);
}

function DisplayTimeout() {
    $("#TimeoutNotice").height($(document).height());
    $("#_ExitCounter").html("Exiting in " + _resumePeriod + " seconds");
    _resumePeriod--;
    if (_resumePeriod <= 0) {
        $("#TimeoutNotice").hide();
        window.navigate("Logout.aspx");
    }
    _resumeHandle = setTimeout("DisplayTimeout()", 1000);
}

function CallKeepAlive() {
    $("#ifKeepAlive").each(function() {
        this.contentWindow.location.reload(true);
    });
}

$(document).ready(function() {
    $("#form").autoAdvance();

    jQuery.fn.centerScreen = function(loaded) {
        var obj = this; if (!loaded) { obj.css('top', $(window).height() / 2 - this.height() / 2); obj.css('left', $(window).width() / 2 - this.width() / 2); $(window).resize(function() { obj.centerScreen(!loaded); }); } else {
            obj.stop(); obj.animate({ top: $(window).height() / 2 - this.height() / 2, left: $(window).width() / 2 - this.width() / 2 }, 200, 'linear');
        }
    }

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RefreshShadow);
    $("#ContentProgress").height($(document).height());
    $("#ProgressImage").centerScreen();    
    RefreshShadow();
    StartTimeoutTimer();
});

function formatItem(row) {
    if (row.length > 1) return row[1];
    return "";
}

function RefreshShadow() {
    $("#form").autoAdvance();
    attachStreetAJAX();
    $("#ContentProgress").height($(document).height());
    $("#ProgressImage").centerScreen();        
    $(".Shadow").redrawShadow();
}

function attachStreetAJAX() {
    $(".StreetAJAX").autocomplete('VezaAJAX.aspx', {
        minChars: 3,
        max: 50,
        formatItem: formatItem        
    })
    .result(function(event ,data, formatted) {
    $(this).val(data[1]);
    });
}

function SelectStreet(inp, data) {
    inp.value = data[0] + "," + data[1] + ", " + data[2] + ", " + data[3];
}
