UniPopup = Class.create();

/**
 * Creates a popup using the content of the given
 * id's container.
 *
 * Popups with a single button (either image or input)
 * are closed on the press of the Return key.
 *
 */
UniPopup.prototype = {
    _visible: false,
    container: null,
    timeout: null,
    initialize: function(id, modal, position, closebox, draghandle, hidden) 
    {
        this.id = id;
        this.container = $('popup-container');
        
        this.moveToContainer();
                
        this.modal = modal;
        this.position = position;
        this.closebox = closebox;
        this.draghandle = draghandle;
        
        /* determines whether the popup has a single action */
        var imgs = $(id).getElementsByTagName('img');
        var buttonCount = 0;
        for (var i = 0; i < imgs.length; i++) {
            if (imgs[i].className && imgs[i].className.indexOf('button') != -1) {
                buttonCount++;
            }
        }
        var inputs = $(id).getElementsByTagName('input');
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].type && (inputs[i].type == 'submit' || inputs[i].type == 'image')) {
                buttonCount++;
            }
        }
        if (buttonCount == 1) {
            this.pseudoModal = true;
        } else {
            this.pseudoModal = false;
        }
        
        if(!hidden) {
            this.show();            
        }
    },
    
    /**
    * If a top-level container is available for pop-ups, the pop-up element is moved to it. This is necessary
    * in order to prevent side effects resulting from positioning
    */
    moveToContainer : function() {
        var popupElement = $(this.id);
        if (this.container !== null && popupElement.parentNode !== this.container) {
            var removedNode = popupElement.parentNode.removeChild(popupElement);
            this.container.appendChild(removedNode);
        }
    },
    
    show: function(event) {    
                       
        this._visible = true;        
        
        /* Centerizing popup please */
        if(this.position == 'center') {
            if(window.innerWidth) { /* FF and Safari */
                var popup_x     = window.innerWidth;
                var popup_y     = window.innerHeight;
                var offset_x    = window.pageXOffset;
                var offset_y    = window.pageYOffset;        
            } else {                /* IE */
                var popup_x     = document.documentElement.clientWidth;
                var popup_y     = document.documentElement.clientHeight;
                var offset_x    = document.documentElement.scrollLeft;
                var offset_y    = document.documentElement.scrollTop;        
            }
            
            var dimensions = $(this.id).getDimensions();
            
            /*alert(dimensions.width + ':' + dimensions.height); */

            popup_x = (popup_x - dimensions.width) * 0.5 + offset_x;
            popup_y = (popup_y - dimensions.height) * 0.5 + offset_y;
            
            var _position = 'exact' + popup_x + ',' + popup_y;
        } else {
            var _position = this.position;
        }
        
        var popup = $(this.id).popup;
        if (!popup) {
            /* create the popup */
            var options = {
                modal: this.modal,
                position: _position,
                closebox: this.closebox,
                draghandle: this.draghandle,
                hidden: true
            };
            popup = new Popup(this.id, null, options);
        } else {
            popup.options.position = _position;
        }
        
        UniPopupStack.push(this);
        $(this.id).popup.show();  
        if (this.onopen) {
            window.setTimeout(
                this.onopen,
                100
            );
        }
    },
    /**
     * Returns true if the popup is visible.
     *
     * @access public
     * @return boolean
     */
    visible: function () {  
        if (this.timeout != null) {
            clearTimeout(this.timeout);
            this.timeout = null;
        }          
        return this._visible;
    },
    hide: function(event) {
        /*Event.stop(event);  */
        $(this.id).popup.hide();
        if (this.onclose) {
            this.onclose();
        }
                
        if (this.pseudoModal) {
            UniPopupStack.remove(this);
        }
        
        var duration = $(this.id).popup.options.duration * 1000;
        var _this    = this;
        this.timeout = setTimeout(function () {_this._visible = false;}, duration);
    },
    /**
     * Returns true if the popup is not visible.
     *
     * @access public
     * @return boolean
     */
    hidden: function () {                         
        return !this._visible;
    }
}

/**
 * Controls the current list of visible pop-ups that can 
 * be closed with a simple press of the Return key.
 *
 * @author Dimitry Zolotaryov
 * @author2 Ahmed Al-Saadi
 */
UniPopupStack = {}
/**
 * Holds the visible popup stack
 */
UniPopupStack.stack = new Array();
/**
 * Add a pop-up to the stack
 *
 * @access public
 * @param UniPopup popup The popup 
 * @return void
 */
UniPopupStack.push = function (popup) {
    var windowEvent = function (e) {
        if ((e.keyCode == 13 || e.keyCode == 27) && UniPopupStack.stack.length > 0) {
            if (!UniPopupStack.testPopupTarget(Event.element(e))) {
                switch(e.keyCode) {
                    case 13:
                        /* Enter key pressed  */
                        var popupElement = UniPopupStack.pop();
                        if(popupElement.actionFn) {
                            popupElement.actionFn();
                            Event.stop(e);
                            return false;
                        }
                        return true;
                    case 27:
                        /* Escape key pressed */
                        UniPopupStack.pop();
                        Event.stop(e);
                        return false;
                }
            }
        }
    }
    Event.observe(document, 'keypress', windowEvent);
    
    /* redeclare the push function   */
    UniPopupStack.push = function (popup) {
        var foundIndex = false;
        for (var i = 0; i < this.stack.length && foundIndex == false; i++) {
            if (this.stack[i] == popup) {
                foundIndex = i;
            }   
        }
        if (foundIndex == false) {
            this.stack.push(popup);
        } else {
            this.stack.splice(foundIndex, 1);
            this.stack.push(popup);
        }
    }
    return UniPopupStack.push(popup);
}
/**
 * Tests to see if the press happened in the popup
 *
 * @access public
 * @return bool test result
 */
 UniPopupStack.testPopupTarget = function (target) {
    /* tests that the action was not taken within the popup */
    var inPopup = false;
    while (target && (target = target.parentNode) && !inPopup) {
        if (target.className && target.className.indexOf('popup') != -1) {
            inPopup = true;
        }
    }
    return inPopup;
 } 


/**
 * Pops the top most visible popup
 * from view.
 *
 * @access public
 * @return moxed Either the popup or false if none is present
 */
UniPopupStack.pop = function () {
    if (this.stack.length > 0) {
        var popup = this.stack[this.stack.length - 1];
        if (popup) {
            popup.hide();
        }
        return popup;
    } else {
        return false;
    }
}
/**
 * Remove a popup from the stack.
 *
 * @access public
 * @param UniPopup popup The popup to remove
 * @return boolean True if the element was found
 */
UniPopupStack.remove = function (popup) {
    var found = false;
    for (var i = 0; i < this.stack.length && found == false; i++) {
        if (this.stack[i] == popup) {
            this.stack.splice(i, 1);
            found = true;
        }
    }
    return found;
}

