﻿var popupEnum = { info: 0, loading: 1, success: 2, warning: 3, error: 4, popup: 5, dHeight: 100, dWidth: 350, confirm: 6 }
var popup = { status: 0, keepAlive: false, state: popupEnum.info, isOk: false, ok: null, cancel: null, panel: null, height: 100, width: 350, imgOk: null, imgCancel: null, focus: null, lastfocus: null, tab: false };
function loadPopup()
{
    if (popup.status == 0) { popup.mask.css({ "opacity": "0.6" }); popup.mask.fadeIn("slow"); popup.popup.fadeIn("slow"); popup.status = 1; if (popup.focus) { popup.focus.focus(); popup.mask.bind('click', function() { popup.focus.focus(); }) } }
}

function disablePopup()
{
    if (popup.status == 1)
    {
        if (popup.keepAlive && popup.panel && popup.state == popupEnum.warning)
        {
            showPanel(popup.panel.attr('id'), popup.panelfirst, popup.panellast, popup.height, popup.width)
        }
        else
        {
            popup.mask.fadeOut("slow"); popup.popup.fadeOut("slow"); if (popup.state == popupEnum.popup) { if (popup.panel) popup.panel.fadeOut('slow'); popup.keepAlive = false; popup.panel = null; };
            popup.status = 0;
            popup.close.hide();
        }
    }
    if (popup.state == popupEnum.confirm)
    {
        if (popup.isOk && typeof (popup.ok) == 'function') { popup.ok(); }
        if (!popup.isOk && typeof (popup.cancel) == 'function') { popup.cancel(); }
        popup.close.hide();
    }
    else if ((popup.state == popupEnum.popup || popup.state == popupEnum.success || popup.state == popupEnum.error) && typeof (popup.ok) == 'function')
    {
        popup.ok();
        popup.close.hide();
    }
    else if (popup.state == popupEnum.loading && !popup.isOk && typeof (popup.cancel) == 'function')
    {
        popup.cancel();
        popup.cancel = null;
    }
    popup.ok = null;
    popup.cancel = null;
}

//centering popup   
function centerPopup()
{
    var st = document.documentElement.scrollTop;
    var l = document.documentElement.clientWidth / 2 + document.documentElement.scrollLeft;
    var t = (document.documentElement.clientHeight) / 2;
    var h = popupEnum.dHeight;
    var w = popupEnum.dWidth;
    if (popup.state == popupEnum.popup)
    {
        h = popup.height;
        w = popup.width;
    }
    t = t > (h / 2) ? (t - (h / 2)) : 0;
    t = (t > 0 ? t : 0) + st;
    l = l - (w / 2);
    l = l > 0 ? l : 0;
    if (popup.state == popupEnum.popup)
    {
        popup.panel.css({ 'display': 'inline', "position": "fixed", width: popup.width - 20, "top": t + 35, "left": l + 5, 'z-index':10000 });

    }

    popup.content.css({ 'min-height': h - 55 });
    $("#popuptype").css({ 'min-height': h - 55 });
    popup.popup.css({ 'width': w, 'min-height': h });

    //centering
    popup.popup.css({ "position": "absolute", "top": t, "left": l });

    //only need force for IE6
    popup.mask.css({ "minheight": document.documentElement.offsetHeight });
}

function openDivPopup(message, messagetype, ok, cancelCallback)
{

    if (typeof (ok) == 'function') popup.ok = ok; else { ok = null };
    if (typeof (cancelCallback) == 'function') popup.cancel = cancelCallback; else { cancelCallback = null };
    var img = $('#popupimg');
    var imgc = $("#popuptype");
    imgc.show();
    img.show();
    var url = img.attr('name');
    var src = '';
    popup.close.show();
    if (popup.panel) popup.panel.hide();

    popup.state = messagetype;
    popup.imgOk.show();
    popup.imgCancel.hide();
    popup.content.css('margin-left', '50');
    popup.keepAlive = false;
    switch (popup.state)
    {
        case popupEnum.error:
            setTabOrder(popup.imgOk[0].id, '');
            src = 'error.png';
            break;
        case popupEnum.warning:
            setTabOrder(popup.imgOk[0].id, popup.imgOk[0].id);
            src = 'warning.png';
            popup.keepAlive = true;
            break;
        case popupEnum.loading:
            popup.close.hide();
            message = "<div style='text-align:center;padding:0px'><img src='" + url + "pleasewait.gif' title='loading' style='padding:10px 0px 0px 0px;'/><br/>" + message + "</div>";
            imgc.hide();
            popup.content.css('margin-left', '0');
            popup.imgOk.hide();

            if (typeof (cancelCallback) == 'function')
            {
                popup.imgCancel.show();
            }
            setTabOrder(popup.imgCancel[0].id, popup.imgCancel[0].id);
            break;
        case popupEnum.success:
            src = 'success.png';
            setTabOrder(popup.imgOk[0].id, popup.imgOk[0].id);
            break;
        case popupEnum.info:
            src = 'info.png';
            break;
        case popupEnum.confirm:
            setTabOrder(popup.imgOk[0].id, popup.imgCancel[0].id);
            popup.imgCancel.show();
            src = 'question.png';
            break;
        case popupEnum.popup:
            popup.keepAlive = true;
            imgc.hide();
            popup.imgOk.hide();
            message = '';
            break;
    }
    if (!popup.keepAlive)
        popup.panel = null;
    //set image url
    img.attr("src", url + src);
    //centering with css
    centerPopup();
    //load popup
    loadPopup();
    popup.content.html(message);
    if (popup.state == popupEnum.popup)
        popup.panel.css('position', 'absolute');
}

function showPanel(panel, focusId, lastfocusId, height, width, cb)
{
    popup.height = height;
    popup.width = width;
    popup.panel = $('#' + $.trim(panel));
    popup.panelfirst = focusId;
    popup.panellast = lastfocusId;
    setTabOrder(focusId, lastfocusId);
    openDivPopup('', popupEnum.popup);
    if (typeof (cb) == 'function')
        popup.ok = cb;
    else
        popup.ok = null;
}
function setTabOrder(focusId, lastfocusId)
{
    popup.tab = true;
    if (popup.lastfocus) popup.lastfocus.unbind('blur', onLastBlur);
    if (popup.close) popup.close.unbind('blur', onCloseBlur);
    if (popup.focus) popup.focus.unbind('blur', onFirstBlur);

    if ($.trim(focusId) != '')
        popup.focus = $('#' + focusId);
    else { popup.focus = popup.close; popup.tab = false; }
    if ($.trim(lastfocusId) != '')
        popup.lastfocus = $('#' + lastfocusId);
    else { popup.lastfocus = popup.close; popup.tab = false; }

    popup.focus.bind('blur', onFirstBlur);
    popup.lastfocus.bind('blur', onLastBlur);
    popup.close.bind('blur', onCloseBlur);
}
function onLastBlur() { if (popup.status == 1) { popup.close.focus(); } }
function onCloseBlur() { if (popup.status == 1) { popup.focus.focus(); } }
function onFirstBlur(e) { if (e.shiftKey && popup.status == 1) popup.close.focus(); }
function showError(message)
{
    openDivPopup(message, popupEnum.error);
}
function showWarning(message)
{
    openDivPopup(message, popupEnum.warning);
}

function updatePopup(message, messagetype, ok)
{
    openDivPopup(message, messagetype, ok);
}
function showProgress(message, cancel)
{
    if (!cancel)
        cancel = null;
    openDivPopup(message, popupEnum.loading, '', cancel);
}

function showConfirm(message, ok, cancel)
{
    if (typeof (ok) == 'function') popup.ok = ok; else { ok = null }; if (typeof (cancel) == 'function') popup.cancel = cancel; else { cancel = null };
    openDivPopup(message, popupEnum.confirm);
}
function initilializePopup(openId)
{
    popup.open = $('#open');
    popup.close = $('#close');
    popup.imgOk = $('#imgok');
    popup.imgCancel = $('#imgcancel');
    popup.mask = $('#mask');
    popup.popup = $('#divPopup');
    popup.content = $('#content');

    popup.close.bind('click', function() { popup.isOk = false; disablePopup(); });
    popup.imgCancel.bind('click', function() { popup.isOk = false; disablePopup(); });
    popup.imgOk.bind('click', function() { popup.isOk = true; disablePopup(); });
    $(document).keydown(function(e)
    {
        if (e.keyCode == 9 && popup.status == 1 && !popup.tab)
        {
            return false;
        }
        return true;
    });
    $(document).keypress(function(e)
    {
        if (e.keyCode == 27 && popup.status == 1)
        {
            if (popup.state == popupEnum.loading)
                return false;
        }
    });
    //    $("body").bind("scroll", (function(e) {
    //        if (popup.status == 1) centerPopup();
    //    }));
}