﻿jQuery.fn.mypopup = function (options) {
    var options = jQuery.extend({
        ID: "popup",
        width: "500px",
        maxHeightContent: "200px",
        overlayID: "mypopupOverlay",
        overlayColor: "#000",
        overlayOpacity: 0.5,
        overlayParentID: "mypopupParent"
    }, options);

    jQuery.extend(options, options);
    var popupInitialized = false;
    var popupShowed = false;

    jQuery.fn.getWindowSize = function () {
        var mw = 0, mh = 0;
        if (typeof (window.innerWidth) == 'number') {
            mw = window.innerWidth;
            mh = window.innerHeight;
        } else
            if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                mw = document.documentElement.clientWidth;
                mh = document.documentElement.clientHeight;
            } else
                if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                    mw = document.body.clientWidth;
                    mh = document.body.clientHeight;
                }
        return { width: mw, height: mh };
    }
    jQuery.fn.InitializePopup = function () {
        
        var div = document.createElement("div");;
        div.id = options.overlayID;
        div.style.display = "none";
        div.style.backgroundColor = options.overlayColor;
        div.style.width = "100%";
        div.style.left = "0px";
        div.style.top = "0px";
        div.style.position = "fixed";
        div.style.filter = "Alpha(Opacity=" + options.overlayOpacity * 100 + ")";
        div.style.MozOpacity = options.overlayOpacity;
        div.style.opacity = options.overlayOpacity;
        var parent = document.getElementById(options.overlayParentID);
        if (parent != undefined) {
            parent.appendChild(div);
        }
        else
        {
            document.body.appendChild(div);
        }

        jQuery(window).resize(function () {
            if (popupShowed) jQuery.fn.showPopup();
        });
        jQuery("#" + options.ID).find(".close").each(function () {
            jQuery(this).click(function () {
                jQuery.fn.hidePopup();
            });
        });
        popupInitialized = true;
    }
    jQuery.fn.showPopup = function () {
        if (!popupInitialized)
            jQuery.fn.InitializePopup();
        var pp = jQuery("#" + options.ID);
        var po = jQuery("#" + options.overlayID);
        po.addClass("pOverlay");
        po.css("height", jQuery.fn.getWindowSize().height + "px");
        pp.addClass("my-popup-box");
        pp.find(".scrollbox").each(function () {
            jQuery(this).css("max-height", options.maxHeightContent);
        });
        pp.show();
        po.show();
        pp.css("left", (jQuery.fn.getWindowSize().width - document.getElementById(options.ID).offsetWidth) / 2 + "px");
        pp.css("top", (jQuery.fn.getWindowSize().height - document.getElementById(options.ID).offsetHeight) / 2 + "px");
        popupShowed = true;
    }
    jQuery.fn.hidePopup = function () {
        jQuery("#" + options.ID).hide();
        jQuery("#" + options.overlayID).hide();
        popupShowed = false;
    }
    jQuery.fn.InitializePopup();
    return this;
};
