var lastNoticia = "";
var nContaListaItem = 0;
var nTotalListaItem = 0;
var nWMaxItem = 0;

$(document).ready(function(){
    //noticias all
    $(".itemNoticia a.mais").click(function(){

        var id = $(this).attr("rel");

        //fecho o anterior
        if(lastNoticia != "" && lastNoticia != id){
            //total
            $("#not_" + lastNoticia + " .texto .all").stop().removeClass("mostra",300);
            //resumo
            $("#not_" + lastNoticia + " .texto .resumo").stop().removeClass("esconde",300);
            //imagem
            $("#not_" + lastNoticia + " .imagem").stop().removeClass("imagemBig",300);
            //h1
            $("#not_" + lastNoticia + " .texto h1").stop().removeClass("open",300);
            
        }

        lastNoticia = id;
        var div = $("#not_" + id + " .imagem");
        var divResumo = $("#not_" + id + " .texto .resumo");
        var divTexto = $("#not_" + id + " .texto .all");
        var h1 = $("#not_" + id + " .texto h1");

        //se ja estiver aberto fecha-o
        if(divTexto.is(".mostra")){
            //total
            divTexto.stop().removeClass("mostra",300);
            //resumo
            divResumo.stop().removeClass("esconde",300);
            //imagem
            div.stop().removeClass("imagemBig",300);
            //h1
            h1.stop().removeClass("open",300);

            restoreScroll(id);

            lastNoticia = "";
        }else{
            //total
            divTexto.stop().delay(300).addClass("mostra",500);
            //resumo
            divResumo.stop().addClass("esconde",500);
            //imagem
            div.stop().addClass("imagemBig",500);
            //h1
            h1.stop().delay(300).addClass("open",500);
            
            restoreScroll(id);
            
        }
        
       return false;

    });

    //noticias last
    nTotalListaItem = $(".itemNoticiaList").length;
    if(nTotalListaItem > 0){

        $(".slideArea").height($(".listaItems").height());

        nWMaxItem = $(".itemNoticiaList:first").innerWidth() + Number($(".itemNoticiaList:first").css("marginRight").substr(0,$(".itemNoticiaList:first").css("marginRight").length-2));
        
        //setInterval(moveLista, 6000)
        $(".slideShowBI").bind("arranca", function(){
            nContaListaItem = nContaListaBI;//variavel do slideshow
            moveLista();
        });

        moveLista();

    }


    //se alguma noticia estiver seleccionada para abrir
    if (typeof nOpenNoticia  != "undefined"){
        $("#not_" + nOpenNoticia + " .resumo a.mais").trigger("click");
    }

});


function restoreScroll(id){
    $("body").stop().animate({left:'0'}, 550, function(){
        var targetOffset = $("a[name=not_" + id + "]").offset().top;
        //alert(targetOffset);
        $('html,body').animate(
            {scrollTop: targetOffset},
            1000);
    })
}


function moveLista(){

    var nXPos = -1*((nContaListaItem+1) * nWMaxItem);

    $(".itemNoticiaList").removeClass("itemNoticiaListActivo");

    var divSel = $(".itemNoticiaList:eq("+ nContaListaItem +")");

    divSel.addClass("itemNoticiaListActivo");


    var aColonar;

    if($(".itemDetalhe").length > 1){
        $(".itemDetalhe:lt(1)").remove();
    }

    if($(".itemDetalhe").length > 0){
        aColonar = $(".itemDetalhe:first");
    }else{
        aColonar = $(".itemDetalhe");
    }

    var clone = $("<div class='itemDetalhe'><h1></h1><div class='texto'></div></div>");

    $("h1",clone).html($(".texto h1",divSel).html());
    $(".texto",clone).html($(".texto .all",divSel).html());


    clone.insertBefore(".slideArea");
    clone.hide();
    clone.delay(300).slideDown(500);

    aColonar.slideUp(500, function(){
        aColonar.remove();
    })

    $(".listaItems").stop().animate({
        left:nXPos,
        border: '1px'
    }, 500, function(){});

    nContaListaItem++;
    if(nContaListaItem>=nTotalListaItem){
        nContaListaItem = 0;
    }



}












