﻿/// <reference path="jquery-1.4.1-vsdoc.js"

$.fn.showDialog = function() {

    var overlay; var showDialogs; var overlayWidth; var overlayHeight; var overlayTimerId;
    var hideDialogs; var showDialog; var hideDialog;

    var sel = this;

    var showOverlay = function() {

        overlayWidth = $(document).width();
        overlayHeight = $(document).height();

        overlay = $("<div />").css({
            position: "absolute",
            top: "0px",
            left: "0px",
            width: overlayWidth,
            height: overlayHeight,
            zIndex: 1000,
            backgroundColor: "#000",
            display: "none"
        });

        overlay.appendTo("body");
        overlay.show().fadeTo(0, 0.8, showDialogs);

    };

    var bindEvents = function() {

        overlayTimerId = window.setInterval(function() {
            var w = $(document).width();
            var h = $(document).height();

            if (w != overlayWidth || h != overlayHeight) {

                overlay.css({ width: w, height: h });
                overlayHeight = h;
                overlayWidth = w;
            }

        },
         250);
        overlay.click(hideDialogs);
    };

    var unbindEvents = function() {
        window.clearInterval(overlayTimerId);
        overlay.unbind("click", hideDialogs);
    };




    var showDialogs = function() {
        $.each(sel, showDialog);
    };
    var showDialog = function() {
        var dialog = $(this);
        dialog.css("z-index", 1001);
        $(".closeClass").live("click",hideDialogs);
        dialog.show();
    };



    hideDialogs = function() {
        unbindEvents();
        $.each(sel, hideDialog);
        overlay.hide();
        overlay.remove();
        overlay = null;
    };

    hideDialog = function() {
        var dialog = $(this);
        $(".closeClass").die("click", hideDialogs);
        dialog.hide();
    };

    showOverlay();
    bindEvents();

    return this;
};

$.fn.centerDialogs = function() {
    $.each(this, function() {
    var dlg = $(this);
        var t = ($(window).height() - dlg.outerHeight()) / 2+$(window).scrollTop();
        var l = ($(window).width() - dlg.outerWidth()) / 2+$(window).scrollLeft();
        dlg.css({ top: t, left: l });
    });

};
