/// <reference path="~/MasterPages/scripts/jtip.js" />

//variables
var _postBackElement;
var _prm;
var _pnLoading;
var _greyBackgroundDiv;

function registerLoadingEvent() {
    _prm = Sys.WebForms.PageRequestManager.getInstance();
    
    _pnLoading = $get('pnLoading');
    _greyBackgroundDiv = $get('greyBackgroundDiv');
    
    _prm.add_initializeRequest(InitializeRequest);
    _prm.add_endRequest(EndRequest);
}

function registerLoadingEventRespondent() {

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(resizeEventLayers);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(resizeEventLayers);
    window.onresize = resizeLayers;

    _prm = Sys.WebForms.PageRequestManager.getInstance();
    _pnLoading = $get('pnLoading');
    _greyBackgroundDiv = $get('greyBackgroundDiv');
    _prm.add_initializeRequest(InitializeRequest);
    _prm.add_endRequest(EndRequestRespondent);
}

function ChangeColor(tableRow, highLight) {
    if (highLight) {
        tableRow.style.backgroundColor = '#608EC4';
    }
    else {
        tableRow.style.backgroundColor = 'white';
    }
}

function NavigateTo(sGoToUrl) {
    document.location.href = sGoToUrl;
}

function showPanel(sControlName, bVisible) {
    if (bVisible) {
        document.getElementById(sControlName).style.display = "visible";
    } else {
        document.getElementById(sControlName).style.display = "none";
    }
}

function showWarningPanel(sHidePanel, sParentControlName, sMasterContentName, e) {
    var sControlName;
    var sParentControlName;
    var sLabel;

    if (sMasterContentName == "") {
        if (sParentControlName == "") {
            sControlName = "ctl00_" + "_" + sHidePanel;
        } else {
            sControlName = "ctl00_" + sParentControlName + "_" + sHidePanel;
        }
    }
    else {
        if (sParentControlName == "") {
            sControlName = "ctl00_" + sMasterContentName + "_" + sHidePanel;

        } else {
            sControlName = "ctl00_" + sMasterContentName + "_" + sParentControlName + "_" + sHidePanel;

        }
    }

    var pnHide = document.getElementById(sControlName);

    if (pnHide.style.display == "none") {
        pnHide.style.display = "block";
        pnHide.innerHTML = "hallo";

        if (document.all) {
            pnHide.style.left = event.clientX + document.body.scrollLeft + "px";
            pnHide.style.top = event.clientY + document.body.scrollTop + "px";
        } else {
            pnHide.style.left = "130px";
            pnHide.style.top = e.pageY + "px";
        }
    }
    else {
        pnHide.style.display = "none";
    }
}

function showLabel(sControl) {
    var sNames = sControl.id.split("_");
    sNames[sNames.length - 1] = "pnWarningMessage";
    var sName = "";

    for (var i = 0; i < sNames.length; i++) {

        sName = sName + sNames[i];
        if (i != sNames.length - 1) {
            sName = sName + "_";
        }
    }

    var pnHide = document.getElementById(sName);

    if (pnHide.style.display == "none") {
        pnHide.style.display = "block";
    } else {
        pnHide.style.display = "none";
    }
}

function showLoader() {
    var iBodyHeight = document.body.clientHeight;

    var iScreenHeight = getScreenHeight();
    var iScreenWidth = getScreenWidth();

    var ShadowFooter = document.getElementById("adminfootershadow");
    if (ShadowFooter == null) { var ShadowFooter = document.getElementById("respfootershadow"); }

    var iBodyHeight = ShadowFooter.offsetHeight + ShadowFooter.offsetTop;
    if (iScreenHeight > iBodyHeight) { iBodyHeight = iScreenHeight; }

    _greyBackgroundDiv.style.left = "0px";
    _greyBackgroundDiv.style.top = "0px";
    _greyBackgroundDiv.style.height = iBodyHeight + "px";
    _greyBackgroundDiv.style.width = iScreenWidth + "px";
    _greyBackgroundDiv.style.display = 'block';
    _greyBackgroundDiv.style.visibility = 'visible';

    iScreenWidth = (iScreenWidth / 2) - 35;

    var iTop = document.documentElement.scrollTop + (iScreenHeight / 2);
    iTop = iTop - 35;

    _pnLoading.style.top = iTop + "px";
    _pnLoading.style.left = iScreenWidth + "px";
    _pnLoading.style.display = 'block';
    _pnLoading.style.visibility = 'visible';
}

function InitializeRequest(sender, args) {
    if (_prm.get_isInAsyncPostBack()) {
        args.set_cancel(true);
    }

    showLoader();
}

function EndRequest(sender, args) {
    _pnLoading.style.display = 'none';
    _pnLoading.style.visibility = 'hidden';
    _greyBackgroundDiv.style.display = 'none';
    _greyBackgroundDiv.style.visibility = 'hidden';

    AttachVisualEvents();
}

function AttachVisualEvents() {
    // Attach textboxes eventhandlers to change event (Survey.js)
    $(document).ready(function () { $(":input[type='text'][class='textbox']").change(function () { aggregateTotal(this); }); });

    //ResizeQuestionnaireItems
    $(document).ready(function () { ResizeQuestionnaireItems(); });

    //CleanAndStoreCounterPlaceholders
    $(document).ready(function () { CleanAndStoreCounterPlaceholders(); });

    //Init jTip Tooltip
    JT_init();
}


function EndRequestRespondent(sender, args) {
    window.scrollTo(0, 0);
    EndRequest(sender, args);
}

$(document).ready(function () { ResizeQuestionnaireItems(); });
function ResizeQuestionnaireItems() {
    if ($("div[id$='_pnlSideMenu']").length > 0) {
        $("#QuestionnaireItems").css("width", "750px");
    }
    else {
        $("#QuestionnaireItems").css("width", "100%");
    }
}
