$(document).ready(function(){
    var wrap_type;
	function initCallback(carousel) {      
        $('.jcarousel-control a').bind('click', function() {
            $('.jcarousel-control a.active').removeClass('active');
            $(this).addClass('active');
            carousel.scroll(jQuery.jcarousel.intval($(this).attr('href')));
            return false;
        });
    }
	function itemVisibleInCallback(carousel, item, index, action) {      
        var total = $('ul.jcarousel-control li').length,
            ind = index % total;

        $('.jcarousel-control a.active').removeClass('active');

        if (ind === 0){
            $('.jcarousel-control a[href="'+total+'"]').addClass('active');
        } else {
            $('.jcarousel-control a[href="'+ ind +'"]').addClass('active');
        }
    }
    if ($('#content #container li').length > 1){
        $("#content #container").jcarousel({
            initCallback: initCallback,
            auto:5,
            scroll:1,
            wrap: 'circular',
            buttonNextHTML: null,
            buttonPrevHTML: null,
            animation: 500,
            itemVisibleInCallback: {
              onAfterAnimation: itemVisibleInCallback
            }
        });
    }
    setInterval (function () {
        var active = $('#slideshow_01 img.active, #slideshow_02 img.active, #slideshow_03 img.active, #slideshow_04 img.active');

        if ( active.length === 0 ) active = $('#slideshow_01 img:last, #slideshow_02 img:last, #slideshow_03 img:last, #slideshow_04 img:last');

        var next =  active.next().length ? active.next()
            : $('#slideshow_01 img:first, #slideshow_02 img:first, #slideshow_03 img:first, #slideshow_04 img:first');

        active.addClass('last-active');

        next.css({opacity: 0.0})
            .addClass('active')
            .animate({opacity: 1.0}, 1000, function() {
                active.removeClass('active last-active');
            });
    }, 9000);
    
   
    function detail_init_callback(carousel) {
        $('.next, .prev').show();
        $('.next').bind('click', function () {
            carousel.next();
            return false;
        });

        $('.prev').bind('click', function () {
            carousel.prev();
            return false;
        });
        $('.next').bind('mouseover', function () {
            carousel.next();
            return false;
        });
        
        $('.prev').bind('mouseover', function () {
            carousel.prev();
            return false;
        });
        
        
    }
    function related_init_callback(carousel) {
        if ($('#related-carousel li').length > 5){
            $('#related-carousel li.last').removeClass('last');
        }
    }
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
        wrap_type = 'both';
        
    } else {
        wrap_type = 'circular';
    }
    if ($('#related-carousel li').length > 5){
        $("#related-carousel").jcarousel({
            initCallback: related_init_callback,
            auto: 3,
            scroll: 2,
            wrap: wrap_type ,
            buttonNextHTML: null,
            buttonPrevHTML: null,
            animation: 500
        });
    }
    if ($('#product-detail ul.thumbs-skin li').length > 3){
        $("#product-detail ul.thumbs-skin").jcarousel({
            initCallback: detail_init_callback,
            auto:0,
            scroll:1,
            wrap: 'circular',
            buttonNextHTML: null,
            buttonPrevHTML: null,
            animation: 500,
            vertical: true
        });
    }
});

