﻿function LightBoxButton(id, text, action) {
    this.id = id
    this.text = text
    this.action = action
}
function showLightBox(Buttons, content, width, height, title, description, closeaction) {
    if (width == 10000) {
        var mes = GetMaxEditorSize(0, 120);
        width = mes.width;
        if (height == 10000) {
            height = mes.height;
        }
    }

    var template = '';
    template = getViewTemplate();

    var data = {
        "editorwidth": width,
        "editorheight": height,
        "title": title,
        "subtitle": description,
        "finishaction": "",
        "closeaction": closeaction,
        "nextaction": "",
        "previousaction": "",
        "Buttons": Buttons
    };
    $('#V2editBackground').show();
    $('#V2editControl').empty();
    var result = TrimPath.parseTemplate(template).process(data).replace("<---CONTENT--->", content);
//    document.getElementById('V2editControl').innerHTML = result;
    $('#V2editControl').html(result);
    $('#V2editControl').show();
    centerObject('V2editControl');
}

function hideLightBox() {
//    document.getElementById('V2editControl').innerHTML = "";
    $('#V2editControl').html('');
    $('#V2editControl').fadeOut('slow', function() {
        $('#V2editBackground').fadeOut().empty();
    });
}

function showLoadingMessage() {
    //$('#V2editBackground').show();
//    document.getElementById('V2editControl2').innerHTML = "<div style='height:40px;width:200px;background-color:#CCCCCC;padding-left:70px;padding-top:14px;'>Loading...</div>";
    $('#V2editControl2').html("<div style='height:40px;width:200px;background-color:#CCCCCC;padding-left:70px;padding-top:14px;'>Loading...</div>");
    $('#V2editControl2').show();
    centerObject('V2editControl2');
    uiLock();
}
function hideLoadingMessage() {
//    document.getElementById('V2editControl2').innerHTML = "";
    $('#V2editControl2').html('');
    $('#V2editControl2').hide();
    //$('#V2editBackground').hide();
    uiUnlock();
}
function GetScrollYPos() {
    var y = 0;
    if (typeof (window.pageYOffset) == 'number') {
        // Netscape
        y = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        // DOM
        y = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        // IE6 standards compliant mode
        y = document.documentElement.scrollTop;
    }
    return y;
}

function GetScrollXPos() {
    var x = 0;
    if (typeof (window.pageYOffset) == 'number') {
        x = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        x = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        x = document.documentElement.scrollLeft;
    }
    return x;
}
function centerObject(divname) {
    var x = GetScrollXPos();
    var y = GetScrollYPos();
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = document.getElementById(divname).offsetHeight;
    var popupWidth = document.getElementById(divname).offsetWidth;
    var top = (windowHeight / 2 - popupHeight / 2) + y;
    var left = (windowWidth / 2 - popupWidth / 2) + x;
    if (top < 0) {
        top = 0;
    }
    if (left < 0) {
        left = 0;
    }
    document.getElementById(divname).style.top = (top+ 50) + 'px';
    document.getElementById(divname).style.left = left + 'px';
    document.getElementById(divname).style.position = 'absolute';
}

function GetMaxEditorSize(widthoffset, heightoffset) {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    if (myWidth > 980) {
        myWidth = 980;
    }
    var maxEditorSize = { "width": myWidth - 10 - widthoffset, "height": myHeight - 10 - heightoffset };
    return maxEditorSize;
}

function getViewTemplate() {
    showLoadingMessage();
    var template = '';
    $.ajax({
        type: "POST",
        url: "/LightBox/views/LightBox.aspx",
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function(msg) {
        if (msg.status == 200) {
            template = msg.responseText;
        }
        },
        error: function(ex) {
            if (ex.status == 200) {
                template = ex.responseText;
            }
        }
    });
    hideLoadingMessage();
    return template;
}

function GetPageContent(url) {
    var template = '';
    $.ajax({
        type: "POST",
        url: url,
        data: {},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function(msg) {
            if (msg.status == 200) {
                template = msg.responseText;
            }
        },
        error: function(ex) {
            if (ex.status == 200) {
                template = ex.responseText;
            }
        }
    });
    return template;
}
function uiLock(content) {
    if (content == 'undefined') {
        content = "";
    }
    $("#V2editControl").append("<div id='uilockId'></div>");
    $("#uilockId").css({
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'z-index': 1000,
        'opacity': 0.6,
        'width': '100%',
        'height': '100%',
        'color': 'white',
        'background-color': 'black'
    });
    $("#uilockId").html(content);
}
function uiUnlock() {
    $('#uilockId').remove();
}
function closeLightbox() {
    hideLightBox();
}
