(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(99999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;

                navpage = page;
                if (page > pages) {
                    navpage = 1;
                } else if (page == 0) {
                    navpage = pages;
                }
                
                $('.jcarousel-control a.current').removeClass('current');
                $('#carousel-'+navpage).addClass('current'); 

                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    currentPage = page;                   
                });               
            }
            
            // 5. insert the back and forward link
            //$wrapper.after('<div id="arrows"><a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a></div>');
            
            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            
            $(this).bind('next', function () {
                gotoPage(parseInt(currentPage) + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {

    var autoscrolling = true;
    
    $('.jcarousel-control span').show();
    $('.highlight .synopsis' ).css('position', 'absolute');
    $('.infiniteCarousel ul li').css('cursor', 'pointer');
    
    $('.infiniteCarousel').not('.no-carousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = true;
    });

    $('.jcarousel-control a').click(function(){
        autoscrolling = false;
        $('.jcarousel-control a.current').removeClass('current');
        $(this).addClass('current');
    
        $('.infiniteCarousel').trigger('goto', $(this).text());
        return false;
    });

    setInterval(function () {
        if (autoscrolling) {
            $('.infiniteCarousel').trigger('next');
        }
    }, 6000);

    $(this).children().find('a.read-more').hide();
    $('.infiniteCarousel li').click(function(){
        var readMoreUrl = $(this).children().find('a.read-more').attr('href');
        if(readMoreUrl){
            window.location = readMoreUrl;
        }
        return false;
    });

    $('.infiniteCarousel ul li').each(function(){
        if($(this).hasClass('carousel-video')){
           $('<div class="carousel-video-button"></div>').prependTo($(this));
        }
    });

});

