﻿/**
 * lzModal
 * 
 * @author Alexandre Lenz(lenz.alexandre@ag2.com.br)
 * @version 1.1
 * @date 03/10/2008
 */

/* ################# MANUAL #################

    REQUISITOS:
        - jquery.js
        - bgiframe.js - ocultar os select no ie6
        - lzModal.css
        
    ACAO: 
        Define a forma como o modal sera ativado, estes devem ser ativados da seguinte forma.
        
        eq( $('.teste').lzModal('acao','janela'); ).
        
        - click : aciona o modal ao clicar sobre o item
        - sobre : ao passar o mouse sobre
        - auto  : ao carregar a pagina
        - fechar : fecha o modal ****** IMPORTANTE a funcionalidade precisa ser atribuida a uma classe ID nao funciona.
        
    JANELA:
        Define a o conteudo a ser visualizado no modal.
        
    SCROLL:
        - se a modal tem necessidade de scroll, basta adicionar uma nova variavel no fim com 'sim'
        eq( $('.teste').lzModal('acao','janela','scroll'); ).
            
*/

jQuery.fn.extend({
	lzModal: function(acao, janela, scroll){
	    
	    dh = $(document).height();
	    dw = $(document).width();
	    $('*').click(function(){
	        dh1 = $(document).height();
	        dh = dh1;     
	    });
	    sh = $('body').height();
	    sw = $('body').width();
	    fade = 0; // velocidade dos efeitos de fade e slide
        if (acao != 'fechar'){	    
    	        function showModal (){
                    st = document.documentElement.scrollTop;
            	    a = acao;
	                j = janela;
	                jh = $(j).height();
	                jw = $(j).width();
    	             	            
    	            if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
                	
        	            $('body').append('<div id="bglzModal"><div id="hideSelect"></div></div>');
	                    $('#hideSelect').height(dh + 20 +"px");
	                    $('#hideSelect').width(dw + 20 +"px");    
	                    $('#hideSelect').bgiframe();
	                }
    	            else{
    	                $('body').append('<div id="bglzModal"></div>');
    	            }
            	    
	                $('#bglzModal').height(dh + 20 +"px");
	                $('#bglzModal').width(dw + 20 +"px");
	                if (scroll == 'sim'){
	                    $("html").css("overflow","scroll");
	                    $("html").css("overflow-x","hidden");
	                }else{
	                    $("html").css("overflow","hidden");
	                }
	                
	                $('#bglzModal').show(fade);
	                
	                $(j).addClass('lzJanela');
	                $(j).css('top', st + ((sh/2) - (jh/2)) + 'px');
	                $(j).css('left', (sw/2)+'px');
	                $(j).css('margin-left','-' + (jw/2) + 'px');
	                //$(j).css('margin-top','-' + (jh/2) + 'px');
	                $(j).show(fade);
                
                    // fechar o modal sempre que clicar no fundo
                
                    function closeModal(){
                    
               	    a = acao;
	                j = janela;
	                 
                     $(j).hide(fade);
                     $('#bglzModal').hide(fade);
                     setTimeout(function(){
                            $('#bglzModal').remove();
                            $("html").css("overflow","auto");
                            if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
                                $('body').width(dw -21 +"px");
                                $('#hideSelect').remove();
                            }
                        },fade);
                    }
                    
                    $('#bglzModal').click(function(){ 
                        closeModal();
                    });
                }
        }
        
        switch(acao) { // define o tipo de acao para abrir o modal
            case 'click':
            $(this).click(function(){showModal();});
            break;
            
            case 'sobre':
            $(this).hover(function(){showModal();},function(){return false});
            break;
            
            case 'auto':
            showModal();
            break;
            
            case 'fechar':
            b = $(this).attr('class');
            $("."+b).click(function(){ 
            $(j).hide(fade);
            $('#bglzModal').hide(fade);
            setTimeout(function(){
                $('#bglzModal').remove();
                $("html").css("overflow","auto");
                if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
                    $('body').width(dw -21 +"px");
                    $('#hideSelect').remove();
                }
            },fade);
            });
            break;
	    }   
    }    
});

    