var parent;
var outros;
var aberto = false;
var carousel_height = 250;
var deslocamento = 0;
var step = 3;
var current = 0;
function closeCarrossel(callback)
{
    if(aberto)
    {
        aberto = false;
        parent.children().not('.sel').hide();
        parent.addClass('grid_4');
        outros.show('slow', function()
        {
            $('#carrossel').animate({
                height:parent.height()
            }, 100, null, function(){
                                        
                });
        });
    }
            
    return callback();


}
$(function(){
    $('#destaque-mais-detalhes').click(function()
    {
        $('#mais-detalhes').toggle('slow');
    });
    $('.sel').click(function()
    {
        aberto = true;
        parent = $(this).parent();
        if(current)
        {
            outros = $('#carrossel li:nth-child('+(current)+')').nextAll().not(parent);
        }
        else
            outros = $('#carrossel li').not(parent);
								
        outros.hide('slow', function()
        {
            parent.removeClass('grid_4');
            parent.children().fadeIn('slow');

            $('#carrossel').animate({
                height:parent.height()
            }, 50, null);
        });
    });
    $('.sprite-close').click(closeCarrossel);

});
$(function() {




    //var step = 3;
    //var current = 0;
    var maximum = $('#carrossel ul li').size();
    var visible = 3;
    var speed = 200;
    var liSize = 320;

    var ulSize = liSize * maximum;
    var divSize = liSize * visible;
    var mais = 0;
    if(maximum%visible != 0 )
        mais = visible;
    //put elements
    for(i=0;i<((maximum/visible));i++)
        $('#carrossel-state').append('<button class="bullet" id="bul-'+i+'"></button>');
    $('#bul-'+current+'').attr('class', '').addClass('bullet-active');

    $('#carrossel ul').css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute");
							
    $('#carrossel').css("width", divSize+"px").css("height", carousel_height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative");

    $('.btnnext').click(function()
    {
        closeCarrossel(function(){
            if(current + step < 0 || (current + step) > (maximum - visible)+mais)
                return;            
            else
            {
                current = current + step;
                $('#carrossel ul').animate({
                    left: -(liSize * current)
                }, speed, null);
                deslocamento = -(liSize * current) +'px';
                $('#bul-'+(current/visible)).attr('class','bullet-active');
                $('#bul-'+((current/visible)-1)).attr('class','').addClass('bullet');
            }
            return false;
        });


    });


    $('.btnprev').click(function()
    {
        closeCarrossel(function(){
            if(current - step < 0 || current - step > (maximum - visible) + visible) {
                return;
            }
            else {
                current = current - step;
                $('#carrossel ul').animate({
                    left: -(liSize * current)
                }, speed, null);
                deslocamento = -(liSize * current) +'px';
                $('#bul-'+(current/visible)).attr('class','bullet-active');
                $('#bul-'+((current/visible)+1)).attr('class','').addClass('bullet');
            }
            return false;
        });


    });
   
    $("a.fazer_inscricao, a.solicitar_orcamento").colorbox({
        'opacity':'0.6',
        'transition':'elastic',
        'onComplete': function(){

            //deslocamento da arrow default
            obArrow = $('.sprite-arrow-pattern');
            obParent = $('li.active');
            li_top = obParent.top;
            li_height = obParent.height()/2;
            arrow_height = obArrow.height()/2;
            modal_top = $('#cboxLoadedContent').offset().top ;
            $('.sprite-arrow-pattern').delay(900).animate({
                top:(li_top - modal_top + li_height - arrow_height +12)+'px'
                },{
                queue:false,
                duration:1200,
                easing: 'easeInOutExpo'
            });

            //click link
            $('.left-content li a').click(function(e){
            
                e.preventDefault();
                alink = $(this);
                obParent = $(this).parent();
                if(obParent.hasClass('active'))
                    return;
            
                ex_active = $('li.active');
                ex_active.link =$('li.active a');
                ex_active.div =$('li.active div');
                li_top =obParent.offset().top ;
                li_height = obParent.height()/2;
                arrow_height = obArrow.height()/2;
                modal_top = $('#cboxLoadedContent').offset().top ;


                //iteração com os icones (an)
                //seta default o ativo anterior
                ex_active.link.fadeOut('fast', function()
                {
                    ex_active.removeClass('active');
                    ex_active.link.fadeIn('fast');
                });
                //seta o ativo
                obParent.find('div').fadeOut('fast', function()
                {
                    obParent.addClass('active');
                    $(this).fadeIn('slow');
                });
            
            
                //arrow-slide
                $('.sprite-arrow-pattern').animate({
                    top:(li_top - modal_top + li_height - arrow_height +12)+'px'
                    },{
                    queue:false,
                    duration:600,
                    easing: 'easeInOutExpo',
                    complete:function(){
                    
                        //iteração dos icones
                        $('#content-ajax').children().fadeOut().css('display','none');
                        
                        $('.'+alink.attr('location')).fadeIn('slow');
                    //alert('aqui'+ alink.attr('href')+' haieuah');
                    
                    
                    
                    }
                });
            });
    }
    });


    
});
