﻿/**
 * Autor: Bruno Soares / Ogilvy Interactive
 * Data:  03/09/2007
 * 
 * Dependências:
 *               scriptaculous/prototype.js
 *               scriptaculous/scriptaculous.js
 * 
*/

var JanelaDhtml = {
    
    // Propriedades.
    strIdDiv  : 'JanelaPop',
    strTarget : 'JanelaPopTarget',
    strUrl    : 'about:blank',
    objDrag   : null,
    objJanela : null,
    intI      : 0,
    objPropriedades : {
        width      : 100,
        height     : 100,
        scrollbars : 'Yes',
        title      : '::::'
    },
    strIdDivOriginal  : 'JanelaPop',
    strTargetOriginal : 'JanelaPopTarget',
    strDivParaJanela  : 'divParaJanelaPop',
    
    // Inicia.
    init : function () {
        $( this.strDivParaJanela ).appendChild( this.construir() );
        $( this.strIdDiv ).style.width = this.objPropriedades.width + 4;
        $( this.strIdDiv ).style.float = 'left';
        Element.setOpacity( this.strIdDiv, 0 );
        this.objDrag = new Draggable (
            $( this.strIdDiv ), {}
        );
        new Effect.Opacity( this.strIdDiv, { duration : 0.5, from : 0, to : 1.0 } );
    },
    
    // Abre a janela na tela.
    abrir : function ( strUrl, objPropriedades ) {
        this.destruir();
        this.intI++;
        this.strIdDiv  = this.strIdDivOriginal  + "_" + this.intI;
        this.strTarget = this.strTargetOriginal + "_" + this.intI;
        this.strUrl    = strUrl;
        this.checarPropriedades( objPropriedades );
        this.init();
        window.open( this.strUrl, this.strTarget, '' );
    },
    
    // Fecha a janela e limpa o HTML gerado por ela.
    fechar : function () {
        new Effect.Opacity( this.strIdDiv, { duration : 0.5, from : 1.0, to : 0 } );
        setTimeout( this.destruir, 800 );
    },
    
    // Costroi o HTML do Pop.
    construir : function () {
        this.objJanela = Builder.node( 'div', { id : this.strIdDiv, 'class' : 'JanelaPop' }, [
            Builder.node( 'div', { id : 'JanelaPopTitulo' }, [
                Builder.node( 'div', { id : 'JanelaPopCmdFechar', onclick : 'JanelaDhtml.fechar();' }, 'x' ),
                this.objPropriedades.title
            ]),
            Builder.node( 'div', { id : 'JanelaPopConteudo' }, [
                Builder.node( 'iframe', {
                    name         : this.strTarget,
                    id           : 'JanelaPopFrame',
                    src          : this.strUrl,
                    width        : this.objPropriedades.width,
                    height       : this.objPropriedades.height,
                    frameborder  : '0',
                    marginheight : '0',
                    marginwidth  : '0',
                    scrolling    : this.objPropriedades.scrollbars
                })
            ])
        ]);
        return ( this.objJanela );
    },
    
    // Remove o HTML da Janela Pop.
    destruir : function () {
        try {
            document.getElementById( 'divParaJanelaPop' ).innerHTML = '';
        } catch ( e ) { }
        this.objDrag   = null;
        this.objJanela = null;
    },
    
    // 
    checarPropriedades : function ( objPropriedades ) {
        if ( objPropriedades.width != null ) {
            this.objPropriedades.width = objPropriedades.width;
        }
        if ( objPropriedades.height != null ) {
            this.objPropriedades.height = objPropriedades.height;
        }
        if ( objPropriedades.scrollbars != null ) {
            this.objPropriedades.scrollbars = objPropriedades.scrollbars;
        }
        if ( objPropriedades.title != null ) {
            this.objPropriedades.title = objPropriedades.title;
        }
    },
    
    // 
    strUrlNova : '',
    reloadPagina : function () {
        window.parent.location.replace( JanelaDhtml.strUrlNova );
    },
    
    // 
    intTempo : '',
    reloadAposTempo : function () {
        setTimeout( this.reloadPagina, this.intTempo );
    }
};
