﻿// requires including lytebox.js and lytebox.css prior including this file
function setupLytebox() {
    if (typeof (myLytebox) == "undefined") {
        setTimeout("setupLytebox()", 100);
        return;
    }
    myLytebox.maxOpacity = 40;
    myLytebox.showCloseIframe = false;
    myLytebox.isModal = true;
    myLytebox.theme = "blue";
    myLytebox.disableKeyboardNav();
}

// callback, see FramesetMgr.js
function FM_CustomClose() {
    //myLytebox.end();
    //alert("Custom close started");
    top.onbeforeunload = null;
    //indicates that page was opened by direct link.
    var module = top.document.getElementById("module");
    var discipline = top.document.getElementById("discipline");
    var isByLink = module != null && discipline != null;
    if (this.m_isTrainingComplete) {
        //alert("to test result");
        top.location.href = top.location.href.substr(0, top.location.href.search("/Test/")) + "/Test/Attempt/" + this.m_attemptId;
    } else if (isByLink) {
        //alert("to tests list");        
        top.location.href = top.location.href.substr(0, top.location.href.search("/Test/")) + "/Test/FindTests?moduleId=" + module.value + "&disciplineId=" + discipline.value;
    }
    else {
        top.location.href = top.location.href;
    }
    //alert("back");
}

function OpenBWP(url) {
    if (typeof (myLytebox) == "undefined") {
        setTimeout("OpenBWP('" + url + "')", 100);
        return;
    }
    var el = document.createElement("a");
    el.setAttribute("href", url);
    el.setAttribute("rel", "lyteframe");
    el.setAttribute("style", "display:none");

    var width = Math.round(document.documentElement.clientWidth * 0.95);
    var height = Math.round(document.documentElement.clientHeight * 0.9);

    width = width < 800 ? 800 : width;
    height = height < 600 ? 600 : height;

    el.setAttribute("rev", "width:" + width + "px;height:" + height + "px;scrolling:no;");
    el.innerHTML = "&nbsp;";

    document.body.appendChild(el);
    myLytebox.updateLyteboxItems();
    el.onclick();
    document.body.removeChild(el);
    // todo: hack for MSIE
    if (navigator.userAgent.indexOf("MSIE") == -1) {
        top.onbeforeunload = function(e) {
            var e = e || window.event;
            var msg = "Обучение ещё не закончено. Вы уверены, что хотите покинуть эту страницу?";
            // For IE and Firefox
            if (e) {
                e.returnValue = msg;
            }

            // For Safari
            return msg;
        };
    }
    return false;
}
setupLytebox();

var moduleId = null;

function GetBwpUrl(testElement) {
    moduleId = testElement.module_id;
    urlArr = document.URL.split("/");
    if (urlArr.length > 3 && urlArr[3] != "Test" && urlArr[3] != "Trial")
        folderName = "/" + urlArr[3];
    else
        folderName = "";
    $.ajax({ url: folderName + "/Test/GetBwpLink?s_testElement=" + testElement, success: OnSuccess, error: OnError });
}

function OnSuccess(data, textStatus, XMLHttpRequest) {
    if (data == "") {
        if (moduleId == null) {
            window.location = "/Test/NoAccessView";
        }
        else {
            window.location = "/Test/NoAccessView?moduleId=" + moduleId;
        }
    }
    else { OpenBWP(data); }
}

function OnError(XMLHttpRequest, textStatus, errorThrown) {
    alert("An error occured. Contact server administrator or try again later");
}
