$(document).ready(function() {
    $('.jcarousel-control li:first-child').addClass('active');
    $('.jcarousel-control li:first-child').children('p').show();
    $('.btsNxtPrev a').hide();
    $('.btsNxtPrev').mouseenter(function() {
        $(this).children('a').fadeIn();
    }).mouseleave(function() {
        $(this).children('a').fadeOut();
    });
    resizeNav();

    $('#abreNav').click(function() {
        if (!$(this).hasClass('active')) {
            $('.jcarousel-control').animate({ bottom: '0px' });
            $(this).addClass('active');
            $(this).animate({ bottom: '49px' });
        } else {
            $('.jcarousel-control').animate({ bottom: '-50px' });
            $(this).removeClass('active');
            $(this).animate({ bottom: '0px' });
        }
    });

    $("#webdoorEng").jcarousel({
        auto: 3,
        animation: 2000,
        wrap: 'circular',
        scroll: 1,
        initCallback: mycarousel_initCallback,
        itemVisibleOutCallback: { onAfterAnimation: mycarousel_itemVisibleOutCallback },

        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    $("#depoimentos ul li a.prc, .vejamaisdepo").click(function() {
        var idBox = $(this).attr('rel');
        $('div.depInt ul').fadeIn();
        $('#' + idBox).fadeIn();
        $('#'+idBox).fadeIn();
        $('#wrapAll').fadeIn();
    });
    $("a.tpFech").click(function() {
        $('div.depInt ul').fadeOut();
        $(this).parent('div').parent('li').fadeOut();
        $('#wrapAll').fadeOut();
    });
    $("#depall1 a.tpPrev").hide();
    $("a.tpPrev").click(function() {
        if (!$(this).parent('div').parent('li').is(':first-child')) {
            $(this).parent('div').parent('li').fadeOut();
            $(this).parent('div').parent('li').prev('li').fadeIn();
        } else {
            $(this).hide();
        }
    });
    $("a.tpNext").click(function() {
        if (!$(this).parent('div').parent('li').is(':last-child')) {
            $(this).parent('div').parent('li').fadeOut();
            $(this).parent('div').parent('li').next('li').fadeIn();
        } else {
            $(this).hide();
        }
    });
});

function mycarousel_itemVisibleOutCallback(carousel) {
    if ($('.jcarousel-control li').hasClass('active')) {
        var id = $('.jcarousel-control li.active').index();
        var total = $('.jcarousel-control li').size();

        if (id != total - 1) {
            $('.jcarousel-control li:eq(' + id + ')').children('p').hide();
            $('.jcarousel-control li.active').next().addClass('active');
            $('.jcarousel-control li:eq(' + id + ')').removeClass('active');
            $('.jcarousel-control li.active').children('p').show();
        } else {
            $('.jcarousel-control li:eq(' + id + ')').children('p').hide();
            $('.jcarousel-control li:first-child').addClass('active');
            $('.jcarousel-control li:eq(' + id + ')').removeClass('active');
            $('.jcarousel-control li:first-child').children('p').show();
        }
        resizeNav();
    }
};
function resizeNav() {
    var totalNav = $('.jcarousel-control li').size();
    var tamAct = $('.jcarousel-control li.active').width();
    var totalW = ((totalNav - 1) * 60) + tamAct;
    $('.jcarousel-control').animate({ marginLeft: -(totalW / 2) }, 100);

}

function mycarousel_initCallback(carousel) {

    $('.jcarousel-control li').bind('click', function() {
        var id = $(this).children('a').attr('rel');
        carousel.scroll(jQuery.jcarousel.intval(id));
        return false;
    });

    $('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};
