﻿/* functions related to progress bar */

var g_Progress = 0;
var g_TimeoutId = null;

function fnLoader(state) {
    fnSetSiteBuildProgress();
    fnSetProgressPos();

    if (g_Progress == 100)
        g_Progress = 0;
    TestRun();
    if (state) {
        g_Progress = 100;
        window.setTimeout("fnPause()", 800);

    }
    else {
        g_Progress = 0;
        divLoader.style.display = "block";
        ShowBlackContent();
    }
}

//do pause after progress bar stopped
function fnPause() {
    divLoader.style.display = "none";
    g_Progress = 0;
    document.getElementById('divBlackContent').style.display = 'none';
    window.clearTimeout(g_TimeoutId);
}

// show loading message
function fnLoadingText() {
    document.getElementById("progressText").innerHTML = document.getElementById("loadingtext").value;
    g_Progress = 0;
    fnLoader();
}

// show building web site message
function fnBuildingText() {

    g_Progress = 0;
    var divObj = document.getElementById("divLoader");
    if (fnGetPlace() == "Bar" || fnGetPlace() == "Page")
        ShowBlackContent();
    //  var bodyWidth = document.body.clientWidth;
    //  var bodyHeight = document.body.clientHeight;
    //  divObj.style.left = bodyWidth/2 - parseInt(divObj.currentStyle.width.replace(/px/,''))/2;
    //  divObj.style.top = bodyHeight/2 - parseInt(divObj.currentStyle.height.replace(/px/,''))/2.2;  

    // disable loading message
    if (document.getElementById("progressText").innerHTML == document.getElementById("loadingtext").value)
        fnLoader(true);
    document.getElementById("divLoader").style.top = (parseInt(document.getElementById("divLoader").currentStyle.top.replace(/px/, '')) + document.body.scrollTop) + "px";
    // set building site message
    document.getElementById("progressText").innerHTML = document.getElementById("buildsitetext").value;
    fnLoader();
}

// make background window 'disabled'
function ShowBlackContent() {
    $("#divBlackContent").css({ 'width': document.body.clientWidth, 'height': document.body.scrollHeight, 'top': "0px", 'left': 0 }).show();
}

//main function for progress bar animation
function TestRun(update) {
    try {
        if (update) {
            g_Progress++;
            if (g_Progress > 100) {
                g_Progress = 100;
                //window.setTimeout("fnStopProgressBar()", 1000);
            }
            eo_GetObject("ProgressBar2").setValue(g_Progress);
        }
        else {
            g_Progress = eo_GetObject("ProgressBar2").getValue();
        }

        g_TimeoutId = window.setTimeout("TestRun(true)", fnGetDelayTime());
    }
    catch (e) { }
}

function fnGetDelayTime() {
    var place = fnGetPlace();
    var totalPages = 0;
    var totalItems = 0;
    var totalFdp = 0;
    var pageItems = 0;
    var pageFdp = 0;
    var objDoc = null;
    if (place == "Page" || place == "Bar")
        objDoc = window.opener.document;
    else
        objDoc = document;
    try {
        totalPages = objDoc.getElementById("hidNumItems").value.split(";")[0];
        totalItems = objDoc.getElementById("hidNumItems").value.split(";")[1];
        totalFdp = objDoc.getElementById("hidNumItems").value.split(";")[2];
        pageItems = objDoc.getElementById("hidNumItems").value.split(";")[3];
        pageFdp = objDoc.getElementById("hidNumItems").value.split(";")[4];
    }
    catch (e) { }

    if (place == "Page" || (place == "Default" && !isFullBuild()))
        return 110 + pageFdp * 20 + pageItems * 5;
    else if (place == "Bar" || (place == "Default" && isFullBuild()) || place == "Cat")
        return 100 + totalPages * 8 + totalFdp * 20 + totalItems * 5;
    else if (place == "Login")
        return 50;
    else if (place == "Autopublish")
        return 100 + totalPages * 3 + totalFdp * 2 + totalItems * 2;
    else if (document.location.href.toLowerCase().lastIndexOf("cms") == -1)  //CRM
        return 1000;
    else
        return 100;
}

//place where progress bar applying
function fnGetPlace() {
    if (document.getElementById("itemLocation") != null)
        return document.getElementById("itemLocation").value;
    else
        return "unknown";
}

//function CleanUpDemo()
//{
//    if (g_TimeoutId != null)
//        window.clearTimeout(g_TimeoutId);
//}

function fnStopProgressBar(update) {
    divLoader.style.display = "none";
    g_Progress = 0;
    window.clearTimeout(g_TimeoutId);
}

//runs in default CMS
function fnSetSiteBuildProgress() {
    fullBuild = isFullBuild();
    fnResetProgressBar();
}

//if this is the case of rebuild (pressing button "Save" in CMS
function isFullBuild() {
    if (document.getElementById("hidFullBuild") != null)
        return eval(document.getElementById("hidFullBuild").value);
    else
        return false;
}

//setup progress bar place
function fnSetProgressPos() {
    setProgressBarIndicator();
    var divObj = document.getElementById("divLoader");
    var bodyWidth = document.body.offsetWidth;
    var bodyHeight = document.body.offsetHeight;
    divObj.style.left = (bodyWidth / 2 - parseInt(divObj.currentStyle.width.replace(/px/, '')) / 2 - 10) + document.body.scrollLeft;
    divObj.style.top = (bodyHeight / 2.2 - parseInt(divObj.currentStyle.height.replace(/px/, '')) / 2) + document.body.scrollTop;
}

function fnResetProgressBar() {
    g_Progress = 0;
    try {
        eo_GetObject("ProgressBar2").setValue(0);
        window.clearTimeout(g_TimeoutId);
    } catch (e) { }
}

function setProgressBarIndicator() {
    document.getElementById(prControlID + 'ProgressBar2').style.borderWidth = '1px';
    document.getElementById(prControlID + 'ProgressBar2').style.borderColor = 'gray';
    document.getElementById(prControlID + 'ProgressBar2_pertl').parentNode.style.color = 'black';
    document.getElementById(prControlID + 'ProgressBar2_pertr').parentNode.style.color = 'black';
}
