if (!window.PopupWindow)
    PopupWindow = new Object();


PopupWindow.Methods = {
    height: 710,
    width: 800,
    topOffset:0,
    bottomOffset:0,
    leftOffset:0,
    rightOffset:0,
    popupID: 'popupWindow',
    overlayID: 'overlay',
    html: 'html not set',
    overlay: false,
    overlayOpacity: 0.75,
    draggable: false,
    dragHandle: null,
    isOpen: false,
    onFinish: '',
    overlayIndex:999,
    popupIndex:1000,
    popups:[],



    show: function(id,options)
    {
        //return if already open

        if (!this.popups[id])
            this.popups[id] = [];

        if ($(this.popups[id].popupID))
        {
            return;
        }

        this._setOptions(id,options);

        var aPageSize = getPageSize();

        var divPopup = new Element("div", {
            id: this.popups[id].popupID,
            className:this.popupID,
            style:"z-index:" + this.popups[id].popupIndex + ";display:none;position:absolute;left:" + this.getX(aPageSize[2],this.width) + "px;top:" + this.getY(aPageSize[3],this.height) + "px;" + ((this.popups[id].draggable && !this.popups[id].dragHandle) ? "cursor:move;" : "")
        }).update(this.popups[id].html);

        Element.insert(document.body,{
            'top':divPopup
        });

        if (this.className)
        {
            divPopup.className = this.className;
        }

        //make draggable
        if (this.draggable)
        {
        	if (this.popups[id].dragHandle){
				new Draggable(this.popups[id].popupID,{handle:this.popups[id].dragHandle});
			}
			else
            	new Draggable(this.popups[id].popupID);
        }

        this._reposition(id);


        //if background overlay
        if (this.popups[id].overlay)
        {
            var divOverlay = new Element("div",{
                id: this.popups[id].overlayID,
                style:"z-index:" + this.popups[id].overlayIndex + ";display:none;position:absolute;left:0px;top:0px;width:100%;background-color:#000;height:" + ((this.popups[id].height > aPageSize[1]) ? this.popups[id].height : aPageSize[1]) + "px"
            });
            document.body.insert({
                'top':divOverlay
            });

            divOverlay.setOpacity(this.popups[id].overlayOpacity);

            this._showOverlay(id);

           // setTimeout("showPopup('" + this.effect +"','" + this.popupID + "'," + ((typeof(this.onFinish) == 'function') ? this.onFinish : "''") + ")",2000);
        }
        else //else just show popup
        {
            this._showPopup(id);
        }

        this.isOpen = true;

    },

    close: function(id)
    {

        //return html to element used
        if (this.popups[id].elementID)
        {
            $(this.popups[id].elementID).innerHTML = $(this.popups[id].popupID).innerHTML;
        }

        var removeDelay = 1000;

        switch (this.closeEffect)
        {
            case 'fade': Effect.Fade(this.popups[id].popupID);break;
            case 'blindUp': Effect.BlindUp(this.popups[id].popupID);break;
            case 'blindDown': Effect.BlindDown(this.popups[id].popupID,{
                scaleFrom:100,
                scaleTo:0
            });
            case 'puff': Effect.Puff(this.popups[id].popupID);break;
            case 'shrink': Effect.Shrink(this.popups[id].popupID);break;
            case 'switchOff': Effect.SwitchOff(this.popups[id].popupID);break;
            default: $(this.popups[id].popupID).hide();removeDelay = 1;break;
        }

		if ( $(this.popups[id].popupID))
		{
        	setTimeout("$('" + this.popups[id].popupID + "').remove()",removeDelay);
		}


        if (this.popups[id].overlay && $(this.popups[id].overlayID))
        {
            $(this.popups[id].overlayID).hide();

			setTimeout("$('" + this.popups[id].overlayID + "').remove()",removeDelay);

        }

        //remove scroll listener - must be exactly same as observe call
        Event.stopObserving(window,'scroll');//,'scroll',function(){
            //PopupWindow._reposition(id);
        //});

        this.isOpen = false;

        if (typeof(this.popups[id].onClose) == 'function')
        {
            setTimeout('var tmp = ' + this.popups[id].onClose + ';tmp.call()',removeDelay  + 100);
        }
    },

    //private functions
    _showOverlay: function(id){

        switch (this.popups[id].overlayEffect)
        {
            case 'grow':
                Effect.Grow(this.popups[id].overlayID,{
                    afterFinish:function(){
                        PopupWindow._showPopup(id);
                    }
                });
                break;
            case 'fade':
                
                Effect.Appear(this.popups[id].overlayID,{
                	duration:2,
                    from:0,
                    to:PopupWindow.overlayOpacity,
                    afterFinish:function(){
                        PopupWindow._showPopup(id);
                    }
                });
                break;
            case 'blindDown':

                Effect.BlindDown(this.popups[id].overlayID,{
                    afterFinish:function(){

                        PopupWindow._showPopup(id);
                    }
                });
                break;
            case 'blindUp':
                Effect.BlindUp(this.popups[id].overlayID,{
                    scaleFrom:0,
                    scaleTo:100,
                    afterFinish:function(){
                            PopupWindow._showPopup(id);
                    }
                });
                break;
            default:
                 
                $(this.popups[id].overlayID).show();
                
                this._showPopup(id);
                break;
        }
    },

    _showPopup:function(id){

       
        switch (this.popups[id].effect)
        {
            case 'grow':
                Effect.Grow(this.popups[id].popupID,{
                    afterFinish:function(){
                        PopupWindow._showPopupFinish(id);
                    }
                });
                break;

            case 'fade':
                Effect.Appear(this.popups[id].popupID,{
                	duration:2,
                    afterFinish:function(){
                        PopupWindow._showPopupFinish(id);
                    }
                });
                break;
            case 'blindDown':
                Effect.BlindDown(this.popups[id].popupID,{
                    afterFinish:function(){
                        PopupWindow._showPopupFinish(id);
                    }
                });
                break;
            case 'blindUp':
                Effect.BlindUp(this.popups[id].popupID,{
                    scaleFrom:0,
                    scaleTo:100,
                    afterFinish:function(){
                        PopupWindow._showPopupFinish(id);
                    }
                });
                break;
            default: 
                $(this.popups[id].popupID).show();
                PopupWindow._showPopupFinish(id);

                break;
        }

        
    },

    _showPopupFinish:function(id){
        //move on scroll
        Event.observe(window,'scroll',function(){
            PopupWindow._reposition(id);
        });

        
        if (typeof(this.popups[id].onFinish) == 'function')
        {
        	
            this.popups[id].onFinish.call();
        }
    },

    _reposition:function(id){
        var aPageSize = getPageSize();
        
        var divPopup = this.popups[id].popupID;
        
        //reposition
        this.popups[id].height = Element.getHeight(divPopup);
        this.popups[id].width = Element.getWidth(divPopup);


        //horizontal reposition
        if (this.popups[id].leftOffset) $(divPopup).style.left = this.popups[id].leftOffset + "px";
        else if (this.popups[id].rightOffset) $(divPopup).style.right = this.popups[id].rightOffset + "px";
        else $(divPopup).style.left =  this.getX(aPageSize[2],this.popups[id].width) + "px";

        //vertical reposition
        if (this.popups[id].topOffset) {
            $(divPopup).style.top = this.popups[id].topOffset + "px";
        }
        else if (this.popups[id].bottomOffset) $(divPopup).style.bottom = this.popups[id].bottomOffset + "px";
        else
        {
        	var top = this.getY(aPageSize[3],this.popups[id].height);
        	$(divPopup).style.top = ((top >= 0) ? top : 0) + "px";
        }
    },

    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;
    },

    _setOptions: function(id,options)
    {
        //override defaults if passed
        if (options.popupID)
        {
            this.popups[id].popupID = options.popupID;
        }
        else this.popups[id].popupID = 'divPopupWindow' + this.popups.length;

        if (options.overlayOpacity)
        {
            this.popups[id].overlayOpacity = options.overlayOpacity
        }

        if (options.width)
        {
            this.popups[id].width = options.width;
        }

        if (options.height)
        {
            this.popups[id].height = options.height;
        }

        if (options.draggable)
        {
            this.popups[id].draggable = options.draggable;
        }
        else this.popups[id].draggable = false;

        if (options.dragHandle)
        {
        	this.popups[id].dragHandle = options.dragHandle;
		}

        if (options.topOffset)
        {
            this.popups[id].topOffset = (options.topOffset > 0) ? options.topOffset : 1;
        } else this.popups[id].topOffset = null;

        if (options.bottomOffset)
        {
            this.popups[id].bottomOffset = (options.bottomOffset > 0) ? options.bottomOffset : 1;
        } else this.popups[id].bottomOffset = null;

        if (options.leftOffset)
        {
            this.popups[id].leftOffset = (options.leftOffset > 0) ? options.leftOffset : 1;
        } else this.popups[id].leftOffset = null;

        if (options.rightOffset)
        {
            this.popups[id].rightOffset = (options.rightOffset > 0) ? options.rightOffset : 0;
        } else this.popups[id].rightOffset = null;

        if (options.overlay)
        {
            this.popups[id].overlay = true;
            this.popups[id].overlayID = this.overlayID + this.popups.length;

        }
        else this.popups[id].overlay = false;

        if (options.overlayEffect)
        {
            this.popups[id].overlayEffect = options.overlayEffect;
        }

        if (options.overlayOpacity){
            this.popups[id].overlayOpacity = options.overlayOpacity;
        }
        else this.popups[id].overlayOpacity = this.overlayOpacity;

        if (options.closeEffect)
        {
            this.popups[id].closeEffect = closeEffect;
        }

        if (options.onFinish)
        {
            this.popups[id].onFinish = options.onFinish;
        } else if (this.onFinish){
        	this.popups[id].onFinish = this.onFinish;
        }

        if (options.onClose)
        {
            this.popups[id].onClose = options.onClose;
        }
        else this.popups[id].onClose = '';

        if (options.className)
        {
            this.popups[id].className = options.className;
        }
        else this.popups[id].className = '';


        //object id passed, set html to this.popups[id]
        if (options.elementID)
        {
            this.popups[id].elementID = options.elementID;
            this.popups[id].html = $(this.popups[id].elementID).innerHTML;
            $(this.popups[id].elementID).innerHTML = "";

        }
        else
            this.popups[id].html = options.html;

        if (options.effect)
        {
            this.popups[id].effect = options.effect;
        }
        else this.popups[id].effect = '';

        //set z-index - only first time
        if (!this.popups[id].popupIndex){
            this.popups[id].overlayIndex = this.overlayIndex + 2*this.popups.length;
            this.popups[id].popupIndex = this.popupIndex + 2*this.popups.length + 1;
        }
    }

}

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);




