// LAYER CULTURA
// Autor: Gabriel Freitas dos Santos
// Data:  06/10/2008

// CLASSE LAYER
function Layer() {
    
    // PROPIEDADES
    this.status   = 'fechada';
    this.posicao  = 2;
    this.width    = 250;
    this.height   = 200;
    this.top      = null;
    this.left     = null;
    this.interval = null;
    this.objPai   = null;
    
    // METODOS
    this.setTimeout = function(i) {
        if(this.interval && this.interval != i)
            clearTimeout(this.interval);
        this.interval = i;
    }
    
    this.clearTimeout = function() {
        if(this.interval) clearTimeout(this.interval);
    }
    
    this.abrir = function(url, param) {
        if(this.status == 'fechada') {
            this.posicionar();
            this.carregar(url, param);
	        this.mostrar();
        }
    }

    this.abrir = function() {
        if(this.status != 'aberta') {
            this.status = 'aberta';
            $('#Layer').show('fast');
            $('#Layer').fadeIn('fast');
        }
    }
    
    this.fechar = function() {
        if(this.status != 'fechada') {
            this.status = 'fechada';
            this.objPai = null;
            $('#Layer').hide('fast');
            $('#Layer').fadeOut('fast');
        }
    }

    this.carregar = function(url, param) {
        $.get(url, param, function(html) {
            if(html.replace(/\s+/g,'') != '')
                $('#Layer').html(html);
        });
    }
    
    this.posicionar = function() {
        if(!this.objPai) return;
        switch(this.posicao) {
            case 1:
                this.top  = $(this.objPai).offset().top - this.height - 2;
                this.left = $(this.objPai).offset().left;
                break;
            case 2:
                this.top  = $(this.objPai).offset().top;
                this.left = $(this.objPai).offset().left + $(this.objPai).width() + 2;
                break;
            case 3:
                this.top  = $(this.objPai).offset().top + $(this.objPai).height() + 2;
                this.left = $(this.objPai).offset().left;
                break;
            case 4:
                this.top  = $(this.objPai).offset().top;
                this.left = $(this.objPai).offset().left - this.width - 2;
                break;
        }
        // Ajuste para tirar o Scroll Horizontal
        if((this.left + this.width) > $(window).width()) {
            this.left -= (((this.left + this.width) - $(window).width()) + 20);
        }
		
        var t=this.top, l=this.left, w=this.width, h=this.height;
        $('#Layer').css({
            width: w+'px',
            top:t,
            left:l
        });
    }
    
    // CRIA LAYER
    this.criar = function() {
        if(!$('#Layer').length)	{
		    $('body').append('<div id="Layer"></div>');
		    $('#Layer').css({
	            position:'absolute',
	            zIndex:100,
	            backgroundColor:'#FFFFFF',
	            display:'none',
	            border:'1px solid #019CAC'
	        });
	    }
    }
    
    // ATRIBUI ESTILO CSS
    this.css = function(p) {
        $('#Layer').css(p);
    }
    // REMOVE LAYER
    this.remove = function() {
        $('#Layer').remove();
    }
}

function layerResenha(nitem, objPai) {
    // Obj Pai não encontrado
    if(!$(objPai).length) return;
    
    // Seta obj Pai para referência
    l.objPai = $(objPai);
    
    // Seta posição de abertura
    l.posicao = 2;
    
    // Seta largura sugerida
    l.width = 300;
    
    // Cria div#Layer caso não exista na página
    l.criar();
    
    // Limpa Timeouts
    l.clearTimeout();
    
    // Posiciona corretamente a div#Layer próximo do objPai
    l.posicionar();
    
    // Carrega o HTML que será inputado na div#Layer
	if( $("#resenha_"+nitem).length ) {
		$("#Layer").html( $("#resenha_"+nitem).html() );
	} else {
        $.get('/scripts/home/layer/resenha.asp', {nitem:nitem}, function(html) {
            if(html.replace(/\s+/g,'') != '') {
				$('<div></div>').attr('id','resenha_'+nitem).css('display','none').html(html).appendTo('body');
                $('#Layer').html(html);
			}
        });
	}
    
    // Específico para Layer de Resenha
    l.css({padding:'5px'});
	l.css({color:'black'});
    
    // Abre Layer
	l.abrir();
	
	// OnMouseOver e OnMouseOut da Capa
	$(objPai).hover(function(){
	    l.clearTimeout();
	},function(){
	    var i = setTimeout(function(){
	        l.fechar();
	    },1000);
	    l.setTimeout(i);
	});
	
	// OnMouseOver e OnMouseOut da Layer
	$('#Layer').hover(function(){
	    l.clearTimeout();
	},function(){
	    var i = setTimeout(function(){
	        l.fechar();
	    },1000);
	    l.setTimeout(i);
	});
	
}

function layerCadastro(objPai) {
    if(!$(objPai).length) return;
    l.objPai = $(objPai);
    l.width = 300;
    l.posicao = 3;
    l.criar();
    l.clearTimeout();
    l.posicionar();
    l.carregar('/scripts/home/layer/sua_conta.asp', {});
    l.css({padding:0});
	l.abrir();
	
	// OnMouseOver e OnMouseOut da Capa
	$(objPai).hover(function(){
	    l.clearTimeout();
	},function(){
	    var i = setTimeout(function(){
	        l.fechar();
	    },1000);
	    l.setTimeout(i);
	});
	
	// OnMouseOver e OnMouseOut da Layer
	$('#Layer').hover(function(){
	    l.clearTimeout();
	},function(){
	    var i = setTimeout(function(){
	        l.fechar();
	    },1000);
	    l.setTimeout(i);
	});

}


function layerCesta(objPai) {
    if(!$(objPai).length) return;
    l.objPai = $(objPai);
    l.posicao = 3;
    l.width = 500;
    l.css({padding:0});
    l.criar();
    l.clearTimeout();
    
    // Html da cesta
    $('#Layer').html('');
    $('#Cesta').clone().appendTo('#Layer');
    $('#Layer #Cesta').show('fast');
    
    l.posicionar();
    l.abrir();
	
	//OnMouseOver e OnMouseOut da Capa
	$(objPai).hover(function(){
	    l.clearTimeout();
	},function(){
	    var i = setTimeout(function(){
	        l.fechar();
	    },1000);
	    l.setTimeout(i);
	});
	
	// OnMouseOver e OnMouseOut da Layer
	$('#Layer').hover(function(){
	    l.clearTimeout();
	},function(){
	    var i = setTimeout(function(){
	        l.fechar();
	    },1000);
	    l.setTimeout(i);
	});

}



// ATRIBUI A LAYER PARA OS OBJECTOS
$(document).ready(function(){
    l = new Layer();
	
	// LAYER CADASTRO
	$("a#cadastro").mouseover(function() {
	    layerCadastro($(this));
	});
	
	// LAYER RESENHA
	$("img[src*=/imagem/capas4/],.capa,.produtos img[src*=/imagem/capas]").mouseover(function(){
		var src = $(this).attr("src").replace(/\\/g,'/').split('/');
		var nitem = src[src.length-1].replace(/\.jpg/,'');
		layerResenha(nitem, $(this));
	});
	
	// LAYER CESTA
	$("img#cesta").mouseover(function() {
	    layerCesta($(this));
	});
	
	
});