// 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');
			mouse_is_opened_basket = false;
        }
    }

    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;
			case 60:
                this.top  = ($('div.mainMenu').offset().top - 3);
                this.left = ($('div.container').offset().left + 950 - 502 - 11);
                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,
	            display:'none'
	        });
	    }
    }
    
    // ATRIBUI ESTILO CSS
    this.css = function(p) {
        $('#Layer').css(p);
    }
    // REMOVE LAYER
    this.remove = function() {
        $('#Layer').remove();
    }
}

function layerCesta(objPai) {
	if(mouse_is_opened_basket) return;
    if(!$(objPai).length) return;
    l.objPai = $(objPai);
    l.posicao = 60;
    l.width = 514;
    l.css({padding:0});
    l.criar();
    l.clearTimeout();
    
    // Html da cesta
    $('#Layer').html('').css('border','0');
    $('#Cesta').clone().appendTo('#Layer');
	$('#Layer #Cesta').show('fast');
    
    l.posicionar();
    l.abrir();
	mouse_is_opened_basket = true;
	mouse_is_inside_basket = true;
	
	/*
	// OnMouseOver e OnMouseOut do Link
	$(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);
	});
	*/
	
	$("div.header div.itensCarrinho, div.header div.itensCarrinho a, #Layer").hover(function() {
		mouse_is_inside_basket = true;
		l.clearTimeout();
	}, function() {
		mouse_is_inside_basket = false;
		l.clearTimeout();
	});
	
	$("body").mouseover(function() {
		if(!mouse_is_inside_basket) {
			var i = setTimeout(function() {
				l.fechar();
			}, 200);
			l.setTimeout(i);
		}
	});
	

}

var mouse_is_inside_basket = false;
var mouse_is_opened_basket = false;

// ATRIBUI A LAYER PARA OS OBJECTOS
$(document).ready(function(){
    l = new Layer();
	
	// LAYER CESTA
	$("div.header div.itensCarrinho").mouseover(function() {
	    layerCesta($(this));
	});
	
	
});
