﻿if(typeof(Prototype) == "undefined")
	throw "RmzPopup requires Prototype to be loaded.";

var RmzPopup = Class.create({
 
    initialize : function(content, width, closeCallback)
    {
        this.width = width;        
        this.closeCallback = function(manualy){};
        if(closeCallback) this.closeCallback = closeCallback;
        
        //this.body = document.getElementsByName('body');
        this.body = $(content).up('body');
        this.selectors = new Array(0);
        
        this.relocatePopupBinded = this._relocatePopup.bindAsEventListener(this);
        this.keyPressBinded = this._keyPress.bindAsEventListener(this);
        
        this.mask = new Element('div', {id: 'popupMask'});
        this.mask.hide();
        //document.body.appendChild(this.mask);
        this.body.insert(this.mask);
        
        var cancel = new Element('a');
        cancel.observe("click", this._closeManually.bindAsEventListener(this));
        cancel.insert("<span></span>");
        
        var titleBar = new Element('div', {id: 'popupTitleBar'});
        var title = new Element('div', {id: 'popupTitle'});
        var controls = new Element('div', {id: 'popupControls'});
        
        controls.insert(cancel);
        titleBar.insert(title);
        titleBar.insert(controls);
        
        var inner = new Element('div', {id: 'popupInner'});        
        inner.insert(titleBar);
        
        var tmp = $(content).remove();
        tmp.setStyle({width: '100%'});
        //tmp.setStyle({height: '100%'});

        tmp.style.display = "block";
        inner.insert(tmp);
        
        this.container = new Element('div', {id: 'popupContainer'});
        this.container.addClassName('shadowed');
        this.container.setStyle({width: this.width});
        this.container.hide();
        this.container.insert(inner);
        //document.body.appendChild(this.container);
        this.body.insert(this.container);
    },

    show : function(focusElement) 
    {
        if (document.body) window.document.body.scroll = 'no'; //hide
      
        this._relocatePopup();
        
        // hide all select elements
        if(Prototype.Browser.IE == true && navigator.appVersion.indexOf("MSIE 7") == -1)
        {
                this.selectors = $(document.body).select('select');
                for(var i= 0; i<this.selectors.length; i++)
                {
                    if(!$(this.selectors[i]).descendantOf(this.container)) $(this.selectors[i]).hide();
                }

        }
                    
        this.body.observe('scroll', this.relocatePopupBinded);
        Event.observe(window, 'scroll', this.relocatePopupBinded);
        Event.observe(window, 'resize', this.relocatePopupBinded);
        Event.observe(document, 'keydown', this.keyPressBinded);
        
        $(this.mask).style.display = "block";
        $(this.container).style.display = "block";
        
        if(focusElement != undefined && focusElement != null)
        {
            var focusTarget = this.getInnerElement(focusElement);
            if(focusTarget != undefined && focusTarget != null) $(focusTarget).activate();
        }
    },
    
    _closeManually : function()
    {
        this._close();
        this.closeCallback(true);
    },
    
    _keyPress : function (ev) {
        if(ev.keyCode == Event.KEY_ESC) this._closeManually();
    },

    _close : function()
    {
        this.body.stopObserving('scroll', this.relocatePopupBinded);
        Event.stopObserving(window, 'scroll', this.relocatePopupBinded);
        Event.stopObserving(window, 'resize', this.relocatePopupBinded);
        Event.stopObserving(document, 'keydown', this.keyPressBinded);

        for(var i= 0; i<this.selectors.length; i++)
        {
            $(this.selectors[i]).show();
        }
        if (document.body) window.document.body.scroll = 'auto'; // show only if needed
        $(this.mask).hide();
        $(this.container).hide();

    },
    
    close : function() 
    {
        this._close();
        this.closeCallback(false);

    },
    
    _relocatePopup : function()
    {
        
        var scrollOffsets = document.viewport.getScrollOffsets();        

        var centerX, centerY;

        //centerX = document.viewport.getWidth();
        //centerY = document.viewport.getHeight();
        centerX = this.getViewportWidth();
        centerY = this.getViewportHeight();        
        var leftOffset = scrollOffsets.left + (centerX - $(this.container).getWidth()) / 2;
        var topOffset = scrollOffsets.top + (centerY - $(this.container).getHeight()) / 2;

        $(this.container).setStyle({top: topOffset + 'px'});
        $(this.container).setStyle({left: leftOffset + 'px'});
        $(this.mask).setStyle({height: centerY + scrollOffsets.top + 'px'});
        $(this.mask).setStyle({width: centerX + scrollOffsets.left + 'px'});        

    },
    
    getViewportHeight : function () {
	    if (window.innerHeight!=window.undefined) return window.innerHeight;
	    if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	    if (document.body) return document.body.clientHeight; 
	    return window.undefined; 
    },

    getViewportWidth : function () {
	    if (window.innerWidth!=window.undefined) return window.innerWidth; 
	    if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	    if (document.body) return document.body.clientWidth; 
	    return window.undefined; 
    },
    
    getInnerElement : function(el){
        var retval = null;
        
        var arr = $(this.container).getElementsBySelector('[id="' + el.toString() + '"]'); 
        
        for(var i= 0; i<arr.length; i++)
        {
            if(arr[i].readAttribute("id") == el.toString())
            {
                retval = arr[i];
                break;
            }
        }
        
        return retval;          
    },
    
    getInnerValue : function(el){
        var retval = null;
        var element = this.getInnerElement(el);
        if(element != null) 
            retval = element.getValue();
        return retval;          
    },
    
    setInnerValue : function(el, value){
        var element = this.getInnerElement(el);
        if(element != null) 
            retval = element.setValue(value);
    }            
});