/*
* infinteRotator - jQuery plugin
* Author: Kyle Gregory
*
* Based on "Automatic Infinite Carouselr" by Remy Sharp
* http://jqueryfordesigners.com/automatic-infinite-carousel/
*
* Built for jQuery library
* http://jquery.com
*
*/ 
(function () {
    $.fn.infiniteRotator = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        return this.each(function () {
            var $wrapper = $('> div',this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first'),
                storedTimeout = null,
				autoScrolling = true,
				autoAnimate = 8200,
				controlitems = []
				
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);

			/* TASKS */
            
            // 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 controls based upon the number of items
			$wrapper.after('<div id="control" class="r' + currentPage + '"><ol></ol></div>');
			$items.each(function(i) {
				i = i+1;
				$("#control ol").append('<li><a href="#' + i +'">' + i + "</a></li>")
			});
			$("#control li").filter(':first').addClass("active");
			
			// 3. 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');
            
            // 4. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 5. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $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;
					$("#control").removeAttr("class").addClass("r" + currentPage);
					$("#control li").removeClass("active").eq(currentPage-1).addClass("active");
                });
            }            
            
            // 6. Bind the control numbers
            $('#control a', this).click(function () {
				suspendAutoScrolling();								  
				var page = parseInt($(this).text());
                gotoPage(page);
                return false;
            });
            
            // 7. Setup the autoscrolling
			function autoScroll () {
				//if(autoScrolling != "false") return true;
				gotoPage(currentPage + 1);
				clearTimeout(storedTimeout);
				storedTimeout = setTimeout(autoScroll, autoAnimate); 
			}
			function resumeAutoScrolling () {
				autoScrolling = true;
				autoScroll();
			};
			function suspendAutoScrolling () {
				autoScrolling = false;
				clearTimeout(storedTimeout); 
				storedTimeout = setTimeout(resumeAutoScrolling, 12000);
			};
			storedTimeout = setTimeout(autoScroll, autoAnimate);
			/*timeout = setInterval(function () {
        		if (autoScrolling) {
            		$('#imagerotator').trigger('next');
        		}
    		}, 20000);
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });*/
        });
    };
})(jQuery);