if (!window.PopupWindow)
    PopupWindow = new Object();


PopupWindow.Methods = {
    height: 710,
    width: 800,
    topOffset:0,
    bottomOffset:0,
    leftOffset:0,
    rightOffset:0,
    popupID: 'divPopupWindow',
    overlayID: 'divOverlay',
    html: 'html not set',
    overlay: false,
    overlayOpacity: 0.75,
    draggable: false,
    dragHandle: null,
    isOpen: false,
    onFinish: '',
    overlayIndex:999,
    popupIndex:1000,
	
    getX: function(pageWidth,width)
    {
        return Math.round((pageWidth - width) / 2 );
    },
	
    getY: function(pageHeight,height)
    {
        return Math.round(this.getTop() + (pageHeight - height) / 2 );
    },
	
    getTop: function()
    {
        var theTop,windowHeight;
        if (document.documentElement && document.documentElement.scrollTop)
        {
            theTop = document.documentElement.scrollTop;
            windowHeight = document.documentElement.offsetHeight;
        }
        else if (document.body)
        {
            theTop = document.body.scrollTop;
            windowHeight = document.body.offsetHeight
        }

        return theTop;
    },
	
    show: function(options)
    {
        //return if already open
        if ($(this.popupID))
        {
            return;
        }
     
        this._setOptions(options);
		
        var aPageSize = getPageSize();
 
        var divPopup = new Element("div", {
            id: this.popupID,
            style:"z-index:" + this.popupIndex + ";display:none;position:absolute;left:" + this.getX(aPageSize[2],this.width) + "px;top:" + this.getY(aPageSize[3],this.height) + "px;" + ((this.draggable && !this.dragHandle) ? "cursor:move;" : "")
        }).update(this.html);
        
        Element.insert(document.body,{
            'top':divPopup
        });

        if (this.className)
        {
            divPopup.className = this.className;
        }
		
        //make draggable
        if (this.draggable)
        {
        	if (this.dragHandle){
				new Draggable(this.popupID,{handle:this.dragHandle});
			}
			else
            	new Draggable(this.popupID);
        }
		
        //reposition
        this.height = Element.getHeight(divPopup);
        this.width = Element.getWidth(divPopup);

                
        //horizontal reposition
        if (this.leftOffset) $(divPopup).style.left = this.leftOffset + "px";
        else if (this.rightOffset) $(divPopup).style.right = this.rightOffset + "px";
        else $(divPopup).style.left =  this.getX(aPageSize[2],this.width) + "px";
		
        //vertical reposition
        if (this.topOffset) {
            $(divPopup).style.top = this.topOffset + "px";
        }
        else if (this.bottomOffset) $(divPopup).style.bottom = this.bottomOffset + "px";
        else 
        {
        	var top = this.getY(aPageSize[3],this.height);
        	$(divPopup).style.top = ((top >= 0) ? top : 0) + "px";
        }
		
        
        //if background overlay
        if (this.overlay)
        {
            var divOverlay = new Element("div",{
                id: this.overlayID,
                style:"z-index:" + this.overlayIndex + ";display:none;position:absolute;left:0px;top:0px;width:100%;background-color:#000;height:" + ((this.height > aPageSize[1]) ? this.height : aPageSize[1]) + "px"
            });
            document.body.insert({
                'top':divOverlay
            });
			
            divOverlay.setOpacity(this.overlayOpacity);
			
            if (this.overlayEffect)
            {
                showPopup(this.overlayEffect,this.overlayID);
            }
            else
                Effect.Appear(this.overlayID,{
                    from:0,
                    to:this.overlayOpacity
                });
			
            setTimeout("showPopup('" + this.effect +"','" + this.popupID + "'," + ((typeof(this.onFinish) == 'function') ? this.onFinish : "''") + ")",2000);
        }
        else //else just show popup
        {
            showPopup(this.effect,this.popupID,this.onFinish);
        }
		
        this.isOpen = true;
               
    },
	
    close: function()
    {
    	
        //return html to element used
        if (this.elementID)
        {
            $(this.elementID).innerHTML = $(this.popupID).innerHTML;
        }
		
        var removeDelay = 1000;
                
        switch (this.closeEffect)
        {
            case 'fade': Effect.Fade(this.popupID);break;
            case 'blindUp': Effect.BlindUp(this.popupID);break;
            case 'blindDown': Effect.BlindDown(this.popupID,{
                scaleFrom:100,
                scaleTo:0
            });
            case 'puff': Effect.Puff(this.popupID);break;
            case 'shrink': Effect.Shrink(this.popupID);break;
            case 'switchOff': Effect.SwitchOff(this.popupID);break;
            default: $(this.popupID).hide();removeDelay = 1;break;
        }
		
		if ( $(this.popupID))
		{
        	setTimeout("$('" + this.popupID + "').remove()",removeDelay);
		}
		
	
        if (this.overlay && $(this.overlayID))
        {
            $(this.overlayID).hide();
			
			setTimeout("$('" + this.overlayID + "').remove()",removeDelay);
			
        }
		
        this.isOpen = false;
		
        if (typeof(this.onClose) == 'function')
        {
            setTimeout('var tmp = ' + this.onClose + ';tmp.call()',removeDelay  + 100);
        }
    },
	
    _setOptions: function(options)
    {
        //override defaults if passed
        if (options.popupID)
        {
            this.popupID = options.popupID;
        }
        else this.popupID = 'divPopupWindow';
		
        if (options.overlayOpacity)
        {
            this.overlayOpacity = options.overlayOpacity
        }
		
        if (options.width)
        {
            this.width = options.width;
        }
		
        if (options.height)
        {
            this.height = options.height;
        }
		
        if (options.draggable)
        {
            this.draggable = options.draggable;
        }
        else this.draggable = false;
        
        if (options.dragHandle)
        {
        	this.dragHandle = options.dragHandle;
		}
		
        if (options.topOffset)
        {
            this.topOffset = (options.topOffset > 0) ? options.topOffset : 1;
        } else this.topOffset = null;

        if (options.bottomOffset)
        {
            this.bottomOffset = (options.bottomOffset > 0) ? options.bottomOffset : 1;
        } else this.bottomOffset = null;
		
        if (options.leftOffset)
        {
            this.leftOffset = (options.leftOffset > 0) ? options.leftOffset : 1;
        } else this.leftOffset = null;
		
        if (options.rightOffset)
        {
            this.rightOffset = (options.rightOffset > 0) ? options.rightOffset : 0;
        } else this.rightOffset = null;
		
        if (options.overlay)
        {
            this.overlay = true;
        }
        else this.overlay = false;
		
        if (options.overlayEffect)
        {
            this.overlayEffect = options.overlayEffect;
        }
		
        if (options.closeEffect)
        {
            this.closeEffect = closeEffect;
        }
		
        if (options.onFinish)
        {
            this.onFinish = options.onFinish;
        }
		
        if (options.onClose)
        {
            this.onClose = options.onClose;
        }
        else this.onClose = '';
		
        if (options.className)
        {
            this.className = options.className;
        }
        else this.className = '';
		
		
        //object id passed, set html to this
        if (options.elementID)
        {
            this.elementID = options.elementID;
            this.html = $(this.elementID).innerHTML;
            $(this.elementID).innerHTML = "";
			
        }
        else
            this.html = options.html;
		
        if (options.effect)
        {
            this.effect = options.effect;
        }
        else this.effect = '';
    }

}

function showPopup (effect,id,onFinish)
{
    switch (effect)
    {
        case 'grow': Effect.Grow(id);break;
        case 'fade': Effect.Appear(id);break;
        case 'blindDown': Effect.BlindDown(id);break;
        case 'blindUp': Effect.BlindUp(id,{
            scaleFrom:0,
            scaleTo:100
        });break;
        default: $(id).show();break;
    }

       
    if (typeof(onFinish) == 'function')
    {
        onFinish.call();
    }
}


function getPageSize(){
	
    var xScroll, yScroll;
	
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
	
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
	
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
	
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }


    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

Object.extend(PopupWindow, PopupWindow.Methods);
