/* Clkients Carousel
*
* Version: 1.0.0
* Author: Sujith Chanaka
*
*/
$.fn.productsCarousel = function (){
 function repeat1(str, num) {
        return new Array( num + 1 ).join( str );
    }
	//$carousel=$('.infiniteCarousel');
    return this.each(function () {
        var $wrapper1 = $('> div', this).css('overflow', 'hidden'),
            $slider1 = $wrapper1.find('> ul'),
            $items1 = $slider1.find('> li'),
            $single1 = $items1.filter(':first'),
            
            singleWidth1 = $single1.outerWidth(), 
            visible1 = Math.ceil($wrapper1.innerWidth() /
			singleWidth1), // note: doesn't include padding or border
            currentPage1 = 1,
            pages1 = Math.ceil($items1.length / visible1); 

        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items1.length % visible1) != 0) {
            $slider1.append(repeat1('<li class="empty" />', visible1 - ($items1.length % visible1)));
            $items1 = $slider1.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items1.filter(':first').before($items1.slice(- visible1).clone().addClass('cloned'));
        $items1.filter(':last').after($items1.slice(0, visible1).clone().addClass('cloned'));
        $items1 = $slider1.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper1.scrollLeft(singleWidth1 * visible1);
        
        // 4. paging function
      setInterval(function gotoPage1() {
			page1=currentPage1-1;//can change direction by editing this statement
            var dir1 = page1 < currentPage1 ? -1 : 1,
                n1 = Math.abs(currentPage1 - page1),
                left1 = singleWidth1 * dir1 * visible1 * n1;
            
            $wrapper1.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left1
            }, 500, function () {
                if (page1 == 0) {
                    $wrapper1.scrollLeft(singleWidth1 * visible1 * pages1);
                    page1 = pages1;
                } else if (page1 > pages1) {
                    $wrapper1.scrollLeft(singleWidth1 * visible1);
                    // reset back to start position
                    page1 = 1;
                } 

                currentPage1 = page1;
				
            });              
            
            return false;
        },2500);
        
    });  
};

