
function destaque_wide(options) { 
    //options( 1 - ON , 0 - OFF)
    options = options || {};
    
    var auto_slide = 1;
    var hover_pause = 1;
    var key_slide = 1;
    var auto_slide_seconds = options.autoSlide || 10000;
    
    var qtdItens = $('.destaque-wide li').length;
        
    $('.navegacao-destaque strong').html('1/'+qtdItens);
    
    var itemClicado = 1;
    
    
    // Mouse Over Destaque Total
    
    $('.destaque-total .chamada-destaque .bg-hover').fadeTo(0, 0.2);
    $('.navegacao-chamada > a img').fadeTo(0, 0.1);
    $('.navegacao-chamada').fadeTo(0, 0);

    $('.conteudo-destaque').mouseenter(function(){
	    $(this).find('.navegacao-chamada').stop().fadeTo(400, 1);
	    $(this).find('.bg-hover').stop().fadeTo(200, 0.5);
    }).mouseleave(function(){
	    $(this).find('.navegacao-chamada').stop().fadeTo(400, 0);
	    $(this).find('.bg-hover').stop().fadeTo(200, 0.2);
    }); 
    
    
    $('.navegacao-chamada > a').mouseenter(function(){
        $('.navegacao-chamada a img').stop().fadeTo('fast', 0.1);
        $(this).children('img').stop().fadeTo('fast', 1);
    }).mouseleave(function(){
        $('.navegacao-chamada a img').stop().fadeTo('fast', 0.1);
    }); 
    
    
    var exibeDestaque = function() {
        $('.conteudo-destaque').css('visibility','visible');
    }
    
    
    if( qtdItens == 1 ){
        $('.navegacao-chamada, .navegacao-destaque').remove();
        $('.destaque-wide').css({'left' : '0'});       
        exibeDestaque();
        return false;
    }
  
    
    //speed of auto slide(
     for (i=0; i<qtdItens; i++){
        var item = $('.destaque-wide .chamada-destaque')[i];
        $(item).attr('rel',i+1)
    }
    if( qtdItens == 2 ){
        $('.destaque-wide li:last').clone().appendTo('.destaque-wide');
    }
    $('.destaque-wide li:first').before( $('.destaque-wide li:last') );
    
    //check if auto sliding is enabled
    if(auto_slide == 1){
        var timer = setInterval("slide('right')", auto_slide_seconds); 
        $('#hidden_auto_slide_seconds').val(auto_slide_seconds);
    }
    

    //check if hover pause is enabled
    if(hover_pause === 1){
        //when hovered over the list 
        $('.destaque-wide').hover(function(){
            //stop the interval
            clearInterval(timer)
        },function(){
            //and when mouseout start it again
            timer = setInterval("slide('right')", auto_slide_seconds); 
        });

    }
    

    //check if key sliding is enabled
    if(key_slide === 1){
        //binding keypress function
        $(document).bind('keypress', function(e) {
            //keyCode for left arrow is 37 and for right it's 39 '
            if(e.keyCode == 37){
                    //initialize the slide to left function
                    slide('left');
            }else if(e.keyCode == 39){
                    //initialize the slide to right function
                    slide('right');
            }
        });
    }
    
    // Customized click
    $('.navegacao-chamada .nav-left').bind('click', function(){
        slide("left");
        return false;
    });
    $('.navegacao-chamada .nav-right').bind('click', function(){
        slide("right");
        return false;
    });
    
    slide = function(where) {
        var item_width = $('.destaque-wide li').outerWidth();
        
        var $destaqueProximo = $('.destaque-wide li:first').next().clone();
        
        if(where == 'left'){
            var left_indent = parseInt($('.destaque-wide').css('left')) + item_width;
        }else{
            var left_indent = parseInt($('.destaque-wide').css('left')) - item_width;
        }
        
        $('.destaque-wide:not(:animated)').stop().animate({'left' : left_indent},700,function(){    
            if(where == 'left'){
                if(qtdItens == 2){
                    $destaqueProximo.appendTo('.destaque-wide');
                    $('.destaque-wide li:last').before($('.destaque-wide li:first'));
                    $('.destaque-wide li:last').prev().remove();
                }else{
                    $('.destaque-wide li:first').before($('.destaque-wide li:last'));
                }
            }else{
                if(qtdItens == 2){ 
                    $destaqueProximo.appendTo('.destaque-wide');
                    $('.destaque-wide li:last').before($('.destaque-wide li:first'));
                    $('.destaque-wide li:last').prev().remove();
                }else{
                    $('.destaque-wide li:last').after($('.destaque-wide li:first'));
                }
            }
            itemClicado = $('.destaque-wide .chamada-destaque')[1];
	        marcaBullets();
            $('.destaque-wide').css({'left' : '-940px'});
        });
        
        
    }

    // BULLETS
    criaBullets = function(){
	    var tpl = '';
	    
	    //$('.navegacao-destaque strong').html(1+'/'+qtdItens)
	    for (i=0;i<qtdItens;i++){
	        tpl += "<span>&bull;</span>";	   
	    }
	    $('.nav-bullets').append(tpl);	
	    $('.nav-bullets span:first').addClass('on');
	}
	
	marcaBullets = function(){
	    var posicao = $(itemClicado).attr('rel');
	    $('.navegacao-destaque strong').html(posicao+'/'+qtdItens);
		var bulletAtual = $('.nav-bullets span')[posicao - 1];
		$('.nav-bullets span').removeClass('on');
		$(bulletAtual).addClass('on');
		try {
		    Cufon.replace('.navegacao-destaque strong', { fontFamily: 'VAGRounded Lt' });
        } catch (error) {};
	}
	
	criaBullets();   
    exibeDestaque();
};

function SliderDestaque() {
    this.qtdItens = $('.chamada-destaque-principal').length;    // quantidade inicial
    this.liAtual = 1;   // qual <li> está sendo mostrado
    this.posicaoAtual = 1;
    this.durantePaginacao = false;  // não tenta paginar quando a página está passando
    
    if(this.qtdItens>1)
        this.autoPaginar = true;
    else
        this.autoPaginas = false;
    
    this.intervaloPaginacao = 10000;
    
    this.paginaComSetas = true
    
    $('.navegacao-chamada-principal > a img').fadeTo(0, 0.1);
    
    if (this.qtdItens > 1) {
        var ultimo = $('.chamada-destaque-principal:last').clone();
        $('.chamada-destaque-principal:first').before(ultimo);
        this.resetaMargem();
    };
    
    this.desabilitaSeVazio();
    this.setLarguraUl();
    this.conectaHoverPaginacao();
    this.conectaPaginacao();
    this.atualizaTextos();
    this.criaBullets();
    this.atualizaNumeros();
    this.conectaClickImagem();
    if (this.autoPaginar) this.conectaAutoPaginar(this.intervaloPaginacao);
    if (this.paginaComSetas) this.conectaSetas();
};

SliderDestaque.prototype.desabilitaSeVazio = function() {
    if( this.qtdItens == 1 ) {
        $('.navegacao-destaque-principal, .navegacao-chamada-principal').remove();
        $('.destaque-principal').css({'left' : '0'});
    };
};

SliderDestaque.prototype.atualizaNumeros = function() {
    $('.navegacao-destaque-principal strong').html(this.posicaoAtual + '/' + this.qtdItens);
    try {
        Cufon.replace('.navegacao-destaque-principal strong', { fontFamily: 'VAGRounded Lt' });
    } catch (err) {};
};

SliderDestaque.prototype.criaBullets = function() {
    var html = '';
    for (var i = 0; i < this.qtdItens; i++) {
        html += "<span>&bull;</span>";
    };
    $('.nav-bullets-principal').html(html);	
    $('.nav-bullets-principal span:first').addClass('on');
};
SliderDestaque.prototype.atualizaBullets = function() {
    $('.nav-bullets-principal span').each(function() { 
        $(this).removeClass('on') 
    });
    $( $('.nav-bullets-principal span')[this.posicaoAtual-1] ).addClass('on');
};

SliderDestaque.prototype.conectaHoverPaginacao = function() {
    $('.navegacao-chamada-principal > a')
        .mouseenter(function() {
            $(this).find('img').fadeTo(0, 1);
        })
        .mouseleave(function() {
            $(this).find('img').fadeTo(0, 0.1);            
        });
};

SliderDestaque.prototype.conectaPaginacao = function() {
    var that = this;
    $('.navegacao-chamada-principal .nav-right').click(function () {
        that.paginar('right');
        return false;
    });
    $('.navegacao-chamada-principal .nav-left').click(function () {
        that.paginar('left');
        return false;
    });
};

SliderDestaque.prototype.atualizaTextos = function() {
    this.atualizaTitulo();
    this.atualizaChapeu();
};
SliderDestaque.prototype.atualizaTitulo = function() {
    var li = this.getItemAtual();
    var titulo = li.find('.titulo-chamada').html();
    var titulo_href = li.find('.titulo-chamada').attr('href');
    $('.conteudo-destaque-principal .sidebar .titulo').fadeOut(350, function() {
        $(this).html(titulo).attr('href', titulo_href).fadeIn(350);
    });
};
SliderDestaque.prototype.atualizaChapeu = function() {
    var li = this.getItemAtual();
    var chapeu = li.find('.chapeu-chamada').html();
    $('.conteudo-destaque-principal .sidebar .chapeu').fadeOut(300, function() {
        $(this).html(chapeu).fadeIn(300);
    });
};

SliderDestaque.prototype.paginar = function(destino) {
    
    if (this.durantePaginacao) {
        return;
    };
    this.durantePaginacao = true;
    
    if (destino == 'right') {
        var left = '-=';
        this.liAtual += 1;
    } else {
        var left = '+=';
        this.liAtual -= 1;
    };
    
    left = left + this.getLarguraLi();
    
    this.atualizaTextos();
    
    var that = this;
    $('.conteudo-destaque-principal-total ul:not(:animated)').stop().animate({'left': left}, 300, function () {
        
        if (destino == 'right') {
            $('.chamada-destaque-principal:first').remove();
            var primeiro = $('.chamada-destaque-principal:first').clone();
            $('.conteudo-destaque-principal-total ul').append(primeiro);
            that.posicaoAtual = that.posicaoAtual == that.qtdItens ? 1 : that.posicaoAtual+1;
        } else {
            $('.chamada-destaque-principal:last').remove();
            var ultimo = $('.chamada-destaque-principal:last').clone();
            ultimo.insertBefore( $('.chamada-destaque-principal:first') );
            that.posicaoAtual = that.posicaoAtual == 1 ? that.qtdItens : that.posicaoAtual-1;
        };
        that.liAtual = 1;
        that.resetaMargem();
        that.atualizaNumeros();
        that.atualizaBullets();
        that.durantePaginacao = false;
        
    });
};

SliderDestaque.prototype.setLarguraUl = function() {
    var length = $('.chamada-destaque-principal').length;
    $('.conteudo-destaque-principal-total ul').css('width', (this.getLarguraLi() * length));
};

SliderDestaque.prototype.getMargemUl = function() {
    return parseInt( $('.conteudo-destaque-principal-total ul').css('left') );
};

SliderDestaque.prototype.getLarguraLi = function() {
    return $('.chamada-destaque-principal:first').outerWidth();
};

SliderDestaque.prototype.getItemAtual = function() {
    return $($('.chamada-destaque-principal')[this.liAtual]);
};

SliderDestaque.prototype.getQuantidadeItens = function() {
    // sempre um a mais que o inicial, já que o último é colocado antes de todos
    // pra garantir uma navegação circular
    return this.qtdItens + 1;
};

SliderDestaque.prototype.resetaMargem = function() {
    $('.conteudo-destaque-principal-total ul').css('left', '-'+this.getLarguraLi()+'px');
    $('.conteudo-destaque-principal .sidebar').css('zoom', 1);  // IE 6 sux!
};

SliderDestaque.prototype.conectaClickImagem = function() {
    $('.conteudo-destaque-principal-total').click(function() {
        location.href = $('.conteudo-destaque-principal .sidebar .titulo').attr('href');
    });
};

SliderDestaque.prototype.conectaAutoPaginar = function(tempo) {
    var that = this;
    var interval = setInterval(function() { that.paginar('right') }, tempo);
    
    $('.sidebar, .conteudo-destaque-principal-total').hover(
        function() {
            clearInterval(interval);
        },
        function() {
            interval = setInterval(function() { that.paginar('right') }, tempo);
        }
    )
    
};

SliderDestaque.prototype.conectaSetas = function() {
    // keyCode para seta esquerda é 37 e para direita é 39
    var that = this;
    $(document).bind('keypress', function(e) {
        if (e.keyCode == 37){
            that.paginar('left');
        } else if(e.keyCode == 39) {
            that.paginar('right');
        };
    });
};

$(document).ready(function(){

    var corNovela = $('.cor-link').css('color');
    var corNovelaHover = $('.cor-link-hover').css('color');
    var corLinha = $('.cor-linha').css('color');
    var fundoClaro = $('.cor-fundo-claro').css('color');
    var fundoClaroHover = $('.cor-fundo-claro-hover').css('color');
	
	// mouseover titulo GERAL
	$('.conteudo-colunas .titulo-colunas').mouseenter(function(){
		$(this).parents('.conteudo-colunas .glb-grid-3').stop().animate({ "backgroundColor": fundoClaro, "borderLeftColor": corNovelaHover }, "fast", "easeInQuad");
	}).mouseleave(function(){
		$(this).parents('.conteudo-colunas .glb-grid-3').stop().animate({ "backgroundColor": "#FFF", "borderLeftColor": corLinha }, "fast", "easeInQuad");
	});

	
	// mouseover titulo COM LINK
	$('.conteudo-colunas a.titulo-colunas').mouseenter(function(){
		$('.conteudo-colunas a.titulo-colunas span').hide();
		$(this).children('span').show();
	}).mouseleave(function(){
		$(this).children('span').hide();
	});

	
	// mouse over box e coluna
	$('.borda-esquerda').fadeTo(0, 0).show();
	$('.box-destaque').mouseenter(function(){
		$(this).parents('.conteudo-colunas .glb-grid-3').stop().animate({ "backgroundColor": fundoClaro, "borderLeftColor": corNovelaHover }, "fast", "easeInQuad");
	    $(this).stop().animate({ "backgroundColor": fundoClaroHover }, "fast", "easeInQuad");
		$(this).children('.borda-esquerda').stop().fadeTo('fast', 1);
	}).mouseleave(function(){
		$(this).parents('.conteudo-colunas .glb-grid-3').stop().animate({ "backgroundColor": "#FFF", "borderLeftColor": corLinha }, "fast", "easeInQuad");
		$(this).children('.borda-esquerda').stop().fadeTo('fast', 0);
		$(this).stop().animate({ "backgroundColor": fundoClaro }, "fast", "easeInQuad", function(){
			$(this).removeAttr('style');
		});
	});

    
    // mouseover titulos destaque primario capitulo
    $('.coluna-capitulos .box-principal a.titulo').mouseenter(function(){
        $(this).css({'color': corNovelaHover});
        try {
            Cufon.replace(this, { fontFamily: 'VAGRounded Lt' }); 
        } catch (error) {};
    }).mouseleave(function(){
        $(this).css({'color': corNovela});
        try {
            Cufon.replace(this, { fontFamily: 'VAGRounded Lt' }); 
        } catch (error) {};
    });
    
	
	//fonte personalizada para chapeu em bastidores
	$('.glb-ultimo .destaque-coluna-primario .chapeu').addClass('fonte-cufon');
	
	alinhaTamanhoEixos = function(){
	    var tamColuna = $('.conteudo-colunas').height()
    	$('.conteudo-colunas .glb-grid-3').css('height',tamColuna);
	}
	window.setTimeout('alinhaTamanhoEixos()', 500);
	
});

