<!--

/**
 * @author Ferdy Perdaan
 * @copyright Copyright (c) 2010, WFIT
 * @version 1.0
 */


/** POPUP --------------------------------------------------------------------------------------------------------------------------------------- **/
var Popup = {
    element   : {container : document.createElement('DIV'),
                 screen    : document.createElement('DIV'),
                 box       : document.createElement('DIV'),
                 content   : document.createElement('DIV')},
    animation : {screen : null, box : null, content : null},
    meta      : {show : false, config:{screen:null, box:null, content:null}},
    content   : new Array(),

    _init : function(){
        //PREPARE ELEMENTS
        Popup.element.container.id = 'PopupContainer';
        Popup.element.screen.id = 'PopupScreenLayer';
        Popup.element.box.id    = 'PopupBoxLayer';
        document.body.appendChild(Popup.element.container);
        Popup.element.container.appendChild(Popup.element.screen);
        Popup.element.container.appendChild(Popup.element.box);
        Popup.element.box.appendChild(Popup.element.content);

        Element.addEvent(Popup.element.screen, 'click', Popup.hide);

        //PREPARE ANIMATION
        Popup.animation.screen = new FadeAnimation(Popup.element.screen, {fade:{max:60, amount:15}},
                                 {
                                    beforeStart : function(animation){
                                        if(Popup.meta.show){                                                    //POPUPSCREEN APPEARING
                                            Popup.element.screen.style.display = 'block';
                                            Popup.element.box.style.display    = 'block';
                                            Popup.element.box.style.visibility = 'hidden';
                                            Popup.element.content.style.visibility = 'hidden';
                                        }else if(Element.getStyle(Popup.element.screen, 'display') == 'block'){          //POPUPSCREEN DISAPPEARING
                                            Popup.element.screen.appendChild(Popup.element.box);                //FADE ANIMATION HACK, THE BOX LAYER WILL FADE TO
                                        }
                                    },

                                    afterStop : function(animation){
                                        if(!Popup.meta.show){                                                   //POPUPSCREEN FINISHED DISAPPEARING
                                            Popup.element.screen.style.display = 'none';
                                            Popup.element.box.style.display    = 'none';
                                            Popup.element.container.appendChild(Popup.element.box);
                                        }else if(Element.getStyle(Popup.element.screen, 'display') == 'block'){          //POPUPSCREEN FINISHED APPEARING
                                            if(Element.getStyle(Popup.element.box, 'background-color').indexOf('rgba') >= 0 || !Popup.content[Popup.meta.ID].config.stretchEnabled){
                                                Popup.element.box.style.visibility = 'visible';
                                                Popup.animation.box.meta.handler.afterStop();
                                            }else{
                                                Popup.animation.box.start();
                                            }
                                        }
                                    }
                                 });
        Popup.animation.box    = new StretchAnimation(Popup.element.box, {},
                                 {
                                    beforeStart : function(animation){
                                        //PREPAIRE ANIMATION
                                        Popup.animation.box.meta.expand = true;
                                        Popup.element.box.style.width   = '0px';
                                        Popup.element.box.style.height  = '0px';

                                        Popup.element.box.style.visibility     = 'visible';
                                        Popup.element.content.style.visibility = 'hidden';
                                    },

                                    afterStop : function(animation){
                                        //START BOX CONTENT ANIMATION
                                        Popup.element.content.style.visibility = 'visible';
                                        Element.setOpacity(Popup.element.content, Popup.animation.content.config.fade.min);
                                        Popup.animation.content.meta.fadeAway = false;
                                        Popup.animation.content.start();
                                    }
                                 });

        Popup.animation.content = new FadeAnimation(Popup.element.content, {speed:100, fade:{amount:20}});

        //STORE DEFAULT CONFIG
        Popup.meta.config.screen  = Popup.animation.screen.config;
        Popup.meta.config.box     = Popup.animation.box.config;
        Popup.meta.config.content = Popup.animation.content.config;

        //ATTATCH RESIZE/(SCROLL?) EVENT TO RE CENTER THE POPUP BOX
    },

    _centerPopupBox : function(){
        var viewport = getViewport();
        Popup.element.box.style.top  = ((viewport.height + viewport.scrollY) / 2 - Popup.content[Popup.meta.ID].size.height / 2) + 'px';
        Popup.element.box.style.left = ((viewport.width + viewport.scrollX) / 2 - Popup.content[Popup.meta.ID].size.width / 2) + 'px';
    },

    _changeContent  : function(ID){
        if(Popup.content[ID] && ID != Popup.meta.ID){
            //SET DATA
            Popup.meta.ID = ID;

            Popup.element.box.style.width  = 'auto';
            Popup.element.box.style.height = 'auto';

            //SET THE CONTENT
            if(typeof(Popup.content[ID].content) != 'string'){
                for(var i = 0; i < Popup.element.content.childNodes.length; i++)
                    Popup.element.content.removeChild(Popup.element.content.childNodes[0]);

                Popup.element.content.appendChild(Popup.content[ID].content);
            }else{
                Popup.element.content.innerHTML = Popup.content[ID].content;
            }

            //FETCH BOX SIZE
            if(!Popup.content[ID].size){
                Popup.element.box.style.display        = 'block';
                Popup.element.content.style.display    = 'block';
                Popup.element.box.style.visibility     = 'hidden';
                Popup.element.content.style.visibility = 'hidden';

                Popup.content[ID].size = Element.getDimension(Popup.element.box);
            }

            //SET SIZE
            Popup._centerPopupBox();

            //SET CONFIG
            Popup.animation.screen.config  = Popup.content[ID].config.screen;
            Popup.animation.box.config     = Popup.content[ID].config.box;
            Popup.animation.content.config = Popup.content[ID].config.content;

            //SET STRETCH SIZE
            Popup.animation.box.meta.size = Popup.content[ID].size;
        }
    },

    show : function(ID){
        Popup._changeContent(ID);
        if(Popup.content[Popup.meta.ID]){
            Popup.meta.show = true;

            if(Popup.animation.screen.inAnimation() && Popup.animation.screen.meta.fadeAway)
                Popup.animation.screen.meta.fadeAway = false;
            else if(!Popup.animation.screen.inAnimtion)
                Popup.animation.screen.start();
        }
    },

    hide : function(){
        Popup.meta.show = false;
        if(Popup.animation.screen.inAnimation() && !Popup.animation.screen.meta.fadeAway)
            Popup.animation.screen.meta.fadeAway = true;
        else if(!Popup.animation.screen.inAnimtion)
            Popup.animation.screen.start();
    },

    registerContent : function(ID, content, config){
        Popup.content[ID] = {content : content, config : {stretchEnabled:true, screen:Popup.meta.config.screen, box:Popup.meta.config.box, content:Popup.meta.config.content}.merge(config)};
   
        if(typeof(content) == 'object' && content.parentNode)
            content.parentNode.removeChild(content);
    }
}

addLoadEvent(Popup._init);
//-->
