var pagePopupWindow = {

	popup_window: null,
	overlay : null,
	popup_window_id : "",
    close_disable : false,

	Show : function(id,params)
	{
		if (this.popup_window_id != id)
		{
			if (this.popup_window_id)
			{
                this.close_disable = false;
				this.Close();
			}
			this.popup_window = null;
			this.popup_window_id = id;
            this.close_disable = false;
		} else {
			return false;
		}
		if (!this.popup_window)
		{
			this.popup_window = document.getElementById(this.popup_window_id);
			if (!this.popup_window)
				return false;

			try {document.body.appendChild(this.popup_window);}
			catch (e){}
		}
        
        //require jquery
        $("#"+this.popup_window_id).trigger("onPopupShowBefore",params);

		this.popup_window.style.display = "block";

		/*var windowSize = this.GetWindowScrollSize();
        var marginTop = windowSize.scrollTop - Math.round(this.popup_window.offsetHeight / 2);
		this.popup_window.style.marginTop = marginTop + "px";
        //require jquery
        $("#"+this.popup_window_id).css("marginTop",(marginTop - Math.round($("#"+this.popup_window_id).height() / 2)) + 'px');
        */
        this.UpdatePosition();
        
		if (this.GetOpacityProperty()) {
			this.CreateOverlay();
        }
        
        //require jquery
        $("#"+this.popup_window_id).trigger("onPopupShowAfter",params);

		return false;
	},

	Close : function()
	{
        if (!this.close_disable) {
    		if (this.popup_window)
    			this.popup_window.style.display = "none";
    
    		if (this.overlay)
    			this.overlay.style.display = "none";
            
            //require jquery
            if (this.popup_window_id != '') {
                $("#"+this.popup_window_id).trigger("onPopupCloseAfter");
                
                this.popup_window_id = "";
            }
        }
		return false;
	},
    
    DisableClose : function()
	{
		if (this.popup_window)
			this.close_disable = true;

		return false;
	},
    
    EnableClose : function()
	{
		if (this.popup_window)
			this.close_disable = false;

		return false;
	},
    
    SetTitle : function(title)
	{
        if (this.popup_window) {
            //require jquery
            $("div.popup-title-contaner","#"+this.popup_window_id).html(title);
        }
		return false;
	},
    
    UpdatePosition : function()
	{
		if (this.popup_window) {
            var windowSize = this.GetWindowScrollSize();
            this.popup_window.style.marginTop = (windowSize.scrollTop - Math.round(this.popup_window.offsetHeight / 2)) + "px";
		}
		return false;
	},

	CreateOverlay : function()
	{
		if (!this.overlay)
		{
			this.overlay = document.body.appendChild(document.createElement("DIV"));
			this.overlay.className = "popup-overlay";

			var _this = this;
			this.overlay.onclick = function() {_this.Close()};
		}

		var windowSize = this.GetWindowScrollSize();

		this.overlay.style.width = windowSize.scrollWidth + "px";
		this.overlay.style.height = windowSize.scrollHeight + "px";
		this.overlay.style.display = "block";
	},

	GetOpacityProperty : function()
	{
		if (typeof document.body.style.opacity == 'string')
			return 'opacity';
		else if (typeof document.body.style.MozOpacity == 'string')
			return 'MozOpacity';
		else if (typeof document.body.style.KhtmlOpacity == 'string')
			return 'KhtmlOpacity';
		else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5)
			return 'filter';

		return false;
	},

	GetWindowScrollSize : function(pDoc)
	{
		var width, height, scroll_y;
		if (!pDoc)
			pDoc = document;

		if ((pDoc.compatMode && pDoc.compatMode == "CSS1Compat"))
		{
			width = pDoc.documentElement.scrollWidth;
			height = pDoc.documentElement.scrollHeight;
			scroll_y = pDoc.documentElement.scrollTop;
		}
		else
		{
			scroll_y = pDoc.body.scrollTop;
			if (pDoc.body.scrollHeight > pDoc.body.offsetHeight)
				height = pDoc.body.scrollHeight;
			else
				height = pDoc.body.offsetHeight;

			if (pDoc.body.scrollWidth > pDoc.body.offsetWidth || 
				(pDoc.compatMode && pDoc.compatMode == "BackCompat") ||
				(pDoc.documentElement && !pDoc.documentElement.clientWidth)
			)
				width = pDoc.body.scrollWidth;
			else
				width = pDoc.body.offsetWidth;
		}
		if (!scroll_y)
		{
			scroll_y = window.scrollY;
            if (!scroll_y) {
                scroll_y = 0;
            }
		}
		return {scrollWidth : width, scrollHeight : height, scrollTop : scroll_y};
	},

	Submit : function(id)
	{
		var form = document.getElementById(id);

		if (form)
			form.submit();

		return false;
	},
    
    AutoShow : function(id)
	{
		setTimeout('this.Show(id)',500);

		return false;
	}
}