﻿/// <reference path="DesignStudio.StandardIncludes.js" />

var scrollLockX = 0;
var scrollLockY = 0;
var dialogX = -9999;
var dialogY = -9999;
var currentDialog = null;
function makeModalDialog(obj, fcnEnter) {
    if ($("#modalDialogOverlay").length == 0) {
        $('<div />')
        .attr({ 'id': 'modalDialogOverlay' })
        .css({ 'position': 'absolute',
            'top': '0px',
            'left': '0px',
            'z-index': '9998',
            '-moz-opacity': '0.5',
            '-khtml-opacity': '0.5',
            'opacity': '0.5',
            'height': $(document).height(),
            'width': $(document).width(),
            'background-color': 'Black',
            'display': 'none'
        })
        .appendTo('body');
    }
    $("#modalDialogOverlay").show();

    if (fcnEnter != undefined) {
        submitFunction = fcnEnter;
    }

    if (currentDialog != null) { nextModalDialog(obj, fcnEnter); return; };

    if ($.browser.msie) {
        document.body.scroll = 'no';
        document.body.style.overflow = 'hidden';
    } else {
        $("body").attr({ 'scrolling': 'no' }).css({ 'overflow': 'hidden', 'height': $(window).height(), 'width': $(window).width() });
    }

    showModalDialog(obj, fcnEnter);
    if ($.browser.msie) {
        $('body').keypress(function(e) {
            e.ReturnValue = !isTrappedKeyCode(obj, e.keyCode);
        });
        $('body').keydown(function(e) {
            //alert(e.keyCode)
            if (isTrappedKeyCode(obj, e.keyCode)) {
                e.ReturnValue = false;
                e.preventDefault();
                return false;
            }
        });
    }
    else {
        window.onkeypress = function(e) {
            if (e.which == null || e.which == 0) {
                return !isTrappedKeyCode(obj, e.keyCode);
            }
            else {
                return !isTrappedKeyCode(obj, e.which);
            }
        }
    }

    scrollLockX = $(window).scrollLeft();
    scrollLockY = $(window).scrollTop();
    $(window).scroll(function(e) { $(window).scrollLeft(scrollLockX).scrollTop(scrollLockY); });
}

function makeModalDialogAt(obj, fcnEnter, x, y) {
    dialogX = x;
    dialogY = y;
    makeModalDialog(obj, fcnEnter);
}

function isTrappedKeyCode(obj, keyCode) {
    //alert(keyCode);
    switch (keyCode) {
        case 13:
            if (submitFunction != undefined) {
                $(currentDialog).hide();
                submitFunction();
            }
            return true;
            break;
    }
    return false;
}

var submitFunction = null;

function showModalDialog(obj) {
    $(window).resize(function() { adjustOverlay(); adjustModalDialogPosition(obj); });
    SaveDocumentTabIndex(obj);
    if ($.browser.msie && parseInt($.browser.version) < 7) {
        $("select").css("display", "none");
    }
    adjustModalDialogPosition(obj);
    $(obj).show();
    var x = $(obj).css("top");
    if ($(obj).find("input").length > 0) {
        $(obj).find("input:first").focus();
    }
    currentDialog = obj;
}

function closeModalDialog(obj) {
    submitFunction = null;
    currentDialog = null;
    RestoreDocumentTabIndex();
    currentDialog = (currentDialog != null) ? currentDialog : obj;
    $(currentDialog).hide();
    $('#modalDialogOverlay').hide();
    if ($.browser.msie) {
        document.body.scroll = 'yes';
        document.body.style.overflow = '';
        $('body').unbind("keypress");
        $('body').unbind("keydown");
        if (parseInt($.browser.version) < 7) {
            $("select").css("display", "");
        }
    }
    else {
        $("body").css({ 'overflow': '', 'height': 'auto', 'width': 'auto' });
        window.onkeypress = null;
    }
    $(window).unbind("scroll").unbind("resize");
}

function nextModalDialog(objNew, fcnEnter) {
    if (fcnEnter != undefined) {
        submitFunction = fcnEnter;
    } else {
        submitFunction = null;
    }

    RestoreDocumentTabIndex();
    if (currentDialog != null) {
        $(currentDialog).hide();
    }
    showModalDialog(objNew);
}

function adjustOverlay() {
    $('#modalDialogOverlay').css({ 'height': $(document).height(), 'width': $(document).width() });
}

function adjustModalDialogPosition(obj) {
    var x = ($(window).width() - $(obj).width()) / 2 + $(window).scrollLeft();
    var y = ($(window).height() - $(obj).height()) / 2 + $(window).scrollTop();
    if (dialogX > -9999) { x = dialogX };
    if (dialogY > -9999) { y = dialogY };
    $(obj).css({ 'position': 'absolute',
        'top': y,
        'left': x,
        'z-index': '9999'
    });
    dialogX = -9999;
    dialogY = -9999;
}

//TAB CONTROL FUNCTIONS
function DocumentTabIndex() {
    this.node = null;
    this.tabIndex = null;
}
var DocumentTabIndexList = null;
function SaveDocumentTabIndex(obj) {
    DocumentTabIndexList = [];
    $("*").each(function() {
        if ($(this).attr("tabindex") != undefined) {
            var ti = new DocumentTabIndex();
            ti.node = this;
            ti.tabIndex = $(this).attr("tabindex");
            ti.tabIndex = ($(this).attr("tabindex") == undefined) ? "" : $(this).attr("tabindex");
            DocumentTabIndexList.push(ti);
            $(this).attr("tabindex", -1);
        }
    });
    $(obj).find("input").add(obj + " a").attr("tabindex", 0);
}
function RestoreDocumentTabIndex() {
    if (DocumentTabIndexList != null && DocumentTabIndexList.length > 0) {
        for (var i = 0; i < DocumentTabIndexList.length; i++) {
            $(DocumentTabIndexList[i].node).attr("tabindex", DocumentTabIndexList[i].tabIndex);
        }
    }
    DocumentTabIndexList = null;
}
