/* modal com galerias */
function galeria_mundo_fiat(){
    var zoom = J('#zoom div img'),
        leg = J('#legenda'),
        t = J('#thumbs li'),
        t_total = t.size();
        a = J('#thumbs li a'),
        p = J('#player'),
        prev = p.find('.bt_slide_prev a'),
        next = p.find('.bt_slide_next a'),
        p_current = J('.slide_cont').find('span:eq(0)'),
        p_total = J('.slide_cont').find('span:eq(1)'),
        cont = 0;

    zoom.attr({src: t.eq(0).find('a').attr('href'), alt: t.eq(0).find('a').attr('title')});
    leg.text(t.eq(0).find('a').attr('title'));

    p_total.text(t_total);

    t.each(function(i){
        J(this).find('a').click(function(){
            if(J(this).hasClass('active')) return false;

            var new_leg = J(this).attr('title'),
                new_zoom = J(this).attr('href');

                a.removeClass('active');
                J(this).addClass('active');

                zoom.attr('src',new_zoom);
                leg.text(new_leg);

                p_current.text(parseInt(i+1));

                cont = i;
            return false;
        });
    });

    prev.click(function(){
        if(cont==0) return false;
        cont--;

        var e = t.eq(cont).find('a'),
            current_leg = e.attr('title'),
            current_zoom = e.attr('href');

        a.removeClass('active');
        e.addClass('active');

        zoom.attr('src',current_zoom);
        leg.text(current_leg);

        p_current.text(parseInt(cont-1));
        return false;
    });

    next.click(function(){
        if(cont==(t_total-1)) return false;
        cont++;

        var e = t.eq(cont).find('a'),
            current_leg = e.attr('title'),
            current_zoom = e.attr('href');

        a.removeClass('active');
        e.addClass('active');

        zoom.attr('src',current_zoom);
        leg.text(current_leg);

        p_current.text(parseInt(cont+1));
        return false;
    });
};

/* lista de projetos �rvore da vida  */
function projetos_arvore(){
    J('#programas dt a').click(function(){
        var e = J(this).find('span'), t = e.text();
        J(this).parent().next('dd').slideToggle('slow', function(){
            t=='[+]' ? e.text('[-]') : e.text('[+]');
        });
        return false;
    });

    J('#programas dd h4 a').toggle(
        function(){
            J(this).find('span').text('[-]').end().parent().next('div').removeClass('hidden');
            return false;
        },
        function(){
            J(this).find('span').text('[+]').end().parent().next('div').addClass('hidden');
            return false;
        }
    );

    J('a[rel="play_video"]').click(function(){
        var flashvars = {videoURL: '/upload/ProjetoArvoreDaVida_BAIXA.flv'};
        var params = {wmode: 'transparent', allowFullScreen: 'true'};
        swfobject.embedSWF("/swf/player_video.swf", "video_arvore", "330", "250", "8", "expressInstall.swf", flashvars, params, null);
        return false;
    });
};

/* listagem das ultimas noticias no arvore da vida */
function ultimas_noticias(){
    var div = J('#ultimas_noticias div ul'),
        ul = J('#ultimas_noticias div ul'),
        li = J('#ultimas_noticias div ul li'),
        h_li = li.height()+1,
        q_li = li.size(),
        h_area = parseInt((li.height())*3),
        up = J("a[@rel='up_hot_list']"),
        down = J("a[@rel='down_hot_list']"),
        limite = parseInt((q_li)-3),
        cont = 0;

    up.css('opacity',0.6);

    if(limite>0){
        down.click(function(){
            if(cont!=limite){
                up.css('opacity')==0.6 ? up.css('opacity',1) : '';
                ul.animate({top: '-='+h_li+'px'}, 300);
                cont++;
            }else{
                J(this).css('opacity',0.6);
                return false;
            };
            return false;
        });
        /* desce lista */
        up.click(function(){
            if(cont!=0){
                down.css('opacity')==0.6 ? down.css('opacity',1) : '' ;
                ul.animate({top: '+='+h_li+'px'}, 300);
                cont--;
            }else{
                J(this).css('opacity',0.6);
                return false;
            };
            return false;
        });
    }else{
        down.remove();
        up.remove();
    };
};

function player_zeropeia(){
    if(J('#player_zeropeia').size()==1){
        swfobject.embedSWF("player-audio-fiat.swf", "player_zeropeia", "241", "200", "8", null, null, {wmode: 'opaque'}, null);
    };
}

/* Lista de estapas automobilismo */
function etapas_automobilismo(){
    J('.etapas dt a').click(function(){
        var e = J(this).find('span.mais'), t = e.text();
        J(this).parent().next('dd').slideToggle('slow', function(){
            t=='[+]' ? e.text('[-]') : e.text('[+]');
        });
        return false;
    });

    J('.etapas dd h4 a').toggle(
        function(){
            J(this).find('span.mais').text('[-]').end().parent().next('div').removeClass('hidden');
            return false;
        },
        function(){
            J(this).find('span.mais').text('[+]').end().parent().next('div').addClass('hidden');
            return false;
        }
    );
};

J(document).ready(function(){
    window.setTimeout(function(){
        projetos_arvore();
        etapas_automobilismo();
        ultimas_noticias();
        player_zeropeia();
    }, 100);
});


