/*
 * jQuery infCarousel plugin
 * @author magerio@centrum.cz - http://www.magerio.cz
 * @version 1.0
 * @date July 15, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 magerio@centrum.cz (www.magerio.cz)
 * @license CC Attribution-No Derivative Works 3.0 - http://creativecommons.org/licenses/by-nd/3.0/
 * based on http://jqueryfordesigners.com/jquery-infinite-carousel/ - great but too simple
 * based on http://www.catchmyfame.com/2009/06/04/jquery-infinite-carousel-plugin/ - great but ugly code 
 */

(function($){
	$.fn.extend({
infCarousel: function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            
            orcount = $items.length,
            singleWidth = 300, 
            
            speed = 6000,
            transSpeed = 1200,
            pilot = 1;
            
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), 
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            

        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
            
        }

        //$items.find('div').hide();
        
        $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'); // reselect
        
        $wrapper.scrollLeft(singleWidth * visible);

        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            automatic = '';
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left,
                opacity: 0.7,
            }, transSpeed, 'swing', function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset 
                    page = 1;
                }
                currentPage = page;
                $wrapper.animate({opacity: 1.0}, 500)
                automatic = setTimeout(function(){gotoPage(currentPage + 1);},5300);
            });                

            //
            return false;
        }
        
        //alert(orcount);
        if(orcount > 1) $wrapper.after('<a id="prev" title="Předchozí">Předchozí</a>'
        + '<a id="next" title="Další">Následující</a>'
        );
        
        $('#prev', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('#next', this).click(function () {
            return gotoPage(currentPage + 1);
        });

        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });

        automatic = setTimeout(function(){gotoPage(currentPage + 1);},5300);
    });  
}

});

})(jQuery);
