﻿(function ($) {
    var options;
    var methods = {
        isVisible: function (options) {
            var display = $("#" + options.ID).css("display");
            if (display == "none") return false;
            return true;
        },
        init: function (options) {
            var defaults = {
                ID: "popup",
                width: "500px",
                maxHeightContent: "200px",
                overlayID: "mypopupOverlay",
                overlayColor: "#000",
                overlayOpacity: 0.5
            };
            var options = $.extend(defaults, options);
            this.each(function () {
                var self = $(this);
                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;
                document.body.appendChild(div);

                $(window).resize(function () {
                    if (methods.isVisible(options)) methods.show(options);
                });
                $("#" + options.ID).find(".close").each(function () {
                    $(this).click(function () {
                        methods.hide(options);
                    });
                });

            });
            return this;
        },
        show: function (options) {
            return $(this).each(function () {
                var pp = $("#" + options.ID);
                var po = $("#" + options.overlayID);
                po.addClass("pOverlay");
                if (navigator.userAgent.toLowerCase().indexOf('iphone') != -1) {
                    po.css("height", "99999999px");
                    po.css("width", "99999999px");
                } else {
                    po.css("height", methods.getWindowSize().height + "px");
                }

                pp.addClass("my-popup-box");
                pp.find(".scroller").each(function () {
                    $(this).css("maxHeight", options.maxHeightContent);
                });
                pp.show();
                po.show();
                pp.css("left", (methods.getWindowSize().width - document.getElementById(options.ID).offsetWidth) / 2 + "px");
                pp.css("top", (methods.getWindowSize().height - document.getElementById(options.ID).offsetHeight) / 2 + "px");
            });
        },
        hide: function (options) {
            $("#" + options.ID).hide();
            $("#" + options.overlayID).hide();
        },
        destroy: function () {
            return this.each(function () {
                var self = $(this);
                var data = self.data('mypopup');
                data.mypopup.remove();
                self.removeData('mypopup');
            });
        },
        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 };
        }
    };

    $.fn.mypopup = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };
})(jQuery);

