﻿jQuery.fn.mypopup = function(options){
  var options = jQuery.extend({
    ID: "popup",
    width: "500px",
    maxHeightContent: "200px",
    overlayID: "mypopupOverlay",
    overlayColor: "#000",
    overlayOpacity: 0.5
  }, options);
  
  jQuery.extend(options, options);
  var popupInitialized = false;
  var popupShowed = false;
  
  
  jQuery.fn.getScrollXY = function(){
    var scrOfX=0,scrOfY=0;
    if (typeof(window.pageYOffset) == 'number') {
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } 
    else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return {x:scrOfX, y:scrOfY};
  }
  
  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.CorrectPosition = function() {
    if(document.getElementById(options.overlayID) != null) {
        document.getElementById(options.overlayID).style.top = jQuery.fn.getScrollXY().y + "px";
        document.getElementById(options.ID).style.top = (jQuery.fn.getWindowSize().height - document.getElementById(options.ID).offsetHeight)/2 + jQuery.fn.getScrollXY().y + "px";
    }
  }
  jQuery.fn.InitializePopup = function() {
    if(document.getElementById(options.overlayID) == null) {
        var div = document.createElement("div");
        div.id = options.overlayID;
        div.style.display = "none";
        div.style.backgroundColor = options.overlayColor;
        div.style.width = "100%";
        
        if (navigator.userAgent.toString().indexOf("MSIE 6.0") != -1) {
            div.style.top = jQuery.fn.getScrollXY().y + "px";
            div.style.position = "absolute";
            div.style.top = (jQuery.fn.getWindowSize().height - div.offsetHeight)/2 + jQuery.fn.getScrollXY().y + "px";
        } else {  
            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);
    }
    jQuery(window).resize(function() {  
        if (popupShowed) jQuery.fn.showPopup();
    });
    jQuery("#" + options.ID).find(".close").each(function(){
        jQuery(this).click(function(){
            jQuery.fn.hidePopup();
        });
    });
    if (navigator.userAgent.toString().indexOf("MSIE 6.0") != -1) {
        jQuery(window).scroll(function() {  
            jQuery.fn.CorrectPosition();
        });
    } 
    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 - 60 + "px");
    pp.css("top", (jQuery.fn.getWindowSize().height - document.getElementById(options.ID).offsetHeight)/2 + "px");
    if (navigator.userAgent.toString().indexOf("MSIE 6.0") != -1) {
        jQuery.fn.CorrectPosition();
    }
    popupShowed = true;
  }  
  jQuery.fn.hidePopup = function() {
    jQuery("#" + options.ID).hide();
    jQuery("#" + options.overlayID).hide();
    popupShowed = false;
  }
  jQuery.fn.InitializePopup();
  return this;
};
