﻿/// <reference path="jquery-vsdoc.js" />

(function($) {
    $.fn.overlay = function(settings) {
        settings = $.extend({
            title: '',
            content: '',
            actions: [],
            onShow: null,
            height: null,
            width: null,
            className: null,
            titleClassName: null,
            showClose: true,
            fixedHeight: true,
            showTitle: true
        }, settings);

        this.removeOverlay();

        var maxZ = Math.max.apply(null, $.map($("body *"), function(e, n) {
            //if ($(e).css("position") == "absolute") {
            if (e.tagName == "DIV") {
                return parseInt($(e).css("z-index")) || 1;
            }
        }));

        maxZ = (maxZ == -Infinity) ? 0 : maxZ;

        return this.each(function() {
            var thisEl = $(this);
            var existingjOverlay = thisEl.attr("jOverlay");

            var overlayWrapper = $("<div></div>");
            overlayWrapper.addClass("jOverlay");
            overlayWrapper.height(thisEl.height());
            var visibleSelectInputs = $("select:visible").css("visibility", "hidden");
            thisEl.data("jOverlayVisibleSelectInputs", visibleSelectInputs);

            var overlayContent = $("<div></div>");
            overlayContent.addClass("jOverlayContent");

            overlayContent.css({ "z-index": maxZ + 2 });

            if (settings.showClose) {
                var close = $("<a><img src=\"" + applicationPath + "images/close.gif\" alt=\"Close\"/></a>").appendTo(overlayContent);
                close.addClass("jOverlayClose");
            }

            if (settings.showTitle) {
                var title = $("<h2></h2>").appendTo(overlayContent);
                title.addClass("jOverlayTitle");
                if (settings.titleClassName) {
                    title.addClass(settings.titleClassName);
                }
                title.html(settings.title);
            }

            var customContent = $("<div></div>").appendTo(overlayContent);
            customContent.html(settings.content);
            customContent.addClass("customContent");

            if (settings.className) {
                if (typeof (settings.className) === "string") {
                    customContent.addClass(settings.className);
                } else if (typeof (settings.className) === "object" && typeof (settings.className.length) !== "undefined") {
                    for (var i = 0; i < settings.className.length; i++) {
                        customContent.addClass(settings.className[i]);    
                    }
                }
            }

            var actionsLength = settings.actions.length;

            if (actionsLength > 0) {
                var actions = $("<ul class='jOverlayActions'></ul>").appendTo(overlayContent);

                for (var i = 0; i < actionsLength; i++) {
                    var action = settings.actions[i];
                    var anchor = $("<li><input id=\"overlay_{0}\" class=\"".format(action.Name) + (action.CssClass || "") + "\" type='" + (action.Type || "button") + "' value='" + action.Name + "' /></li>").appendTo(actions).find(":button");
                    if (action.Click != undefined) {
                        anchor.bind("click", { overlay: this }, action.Click);
                    }
                }
            }

            var overlayBackground = $("<div></div>");
            overlayBackground.addClass("jOverlayBackground");

            overlayBackground.css({ "z-index": maxZ + 1 });

            //disable the popup when background is clicked or the esc button is pressed
            if (settings.showClose) {
                overlayBackground.click(function() {
                    thisEl.removeOverlay();
                });
                $(document).keypress(function(e) {
                    if (e.keyCode == 27 && this.popupStatus != 0) {
                        thisEl.removeOverlay();
                    }
                });
            }

            thisEl.append(overlayContent);
            thisEl.append(overlayBackground);

            if (settings.height !== null) {
                if (settings.fixedHeight) {
                    customContent.height(settings.height);
                }
                else {
                    customContent.css("min-height", settings.height);
                }
            }

            if (settings.width !== null) {
                overlayContent.width(settings.width);
            }

            function setOverlaySize() {
                var windowWidth = document.documentElement.clientWidth;
                var windowHeight = document.documentElement.clientHeight;
                var popupHeight = overlayContent.height();
                var popupWidth = overlayContent.width();

                var top = (popupHeight >= windowHeight) ? 0 : windowHeight / 2 - popupHeight / 2;

                //centering
                overlayContent.css({
                    "position": "absolute",
                    "top": top,
                    "left": windowWidth / 2 - popupWidth / 2,
                    "margin-top": $(window).scrollTop()
                });
                //only need force for IE6

                overlayBackground.css({
                    "height": document.body.clientHeight,
                    "width": document.body.clientWidth
                });
            }

            setOverlaySize();

            /*
            if (settings.width !== null) {
            if (!settings.fixedWidth) {
            var paddingLeft = parseInt(customContent.css("padding-left"));
            var paddingRight = parseInt(customContent.css("padding-right"));
            var childWidth = parseInt(customContent.children().attr("clientWidth"));
            overlayContent.width(childWidth + paddingRight + paddingLeft);
            setOverlaySize(); // ensures the overlay is positioned in the center
            }
            }
            */

            if (settings.showClose) {
                close.click(function() {
                    thisEl.removeOverlay();
                });
            }

            overlayBackground.css({
                "opacity": "0.7"
            });

            var overlayInstance = this;
            overlayBackground.fadeIn("fast");
            overlayContent.fadeIn("fast", function() {
                customContent.find(":input:first").focus();
                if (typeof settings.onShow !== "undefined" && settings.onShow !== null) {
                    settings.onShow.call(overlayInstance, { data: { overlay: thisEl, customContent: customContent} });
                }
            });

            $(window).resize(function() {
                setOverlaySize();
            });
        });
    };

    $.fn.removeOverlay = function() {
        return this.each(function() {
            var thisEl = $(this);
            var visibleSelectInputs = thisEl.data("jOverlayVisibleSelectInputs");
            if (visibleSelectInputs) {
                visibleSelectInputs.css("visibility", "visible");
            }
            thisEl.children(".jOverlayBackground").add(thisEl.children(".jOverlayContent")).fadeOut("fast", function() {
                $(this).remove();
            });
        });
    };
})(jQuery);

function overlayClose_Handler(e) {    
    $(e.data.overlay).removeOverlay();
}
