
$.fn.infiniteCarousel=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'),
			singleWidth=$single.outerWidth(),
			visible=Math.ceil($wrapper.innerWidth()/singleWidth),
			currentPage=1,
			pages=$items.length;
		
				
		if(($items.length%visible)!==0){
			$items=$slider.find('> li');
		}
		
		//$items.filter(':first').before($items.slice($items.length-visible,$items.length).clone().addClass('cloned'));
		//$items.filter(':last').after($items.slice(0,visible).clone().addClass('cloned'));
		$items=$slider.find('> li');
	
		var length_of_li=$items.length-$items.filter('.cloned').length;
		//$wrapper.scrollLeft(singleWidth*(visible*1));
		
		function gotoPage(page){
			
			var dir = page < currentPage ? -1 : 1,n=Math.abs(currentPage-page),left=singleWidth*dir;
			//var left = singleWidth;
			$wrapper.filter(':not(:animated)').animate({scrollLeft:'+='+left},1,function(){
				if(page==0){
						$wrapper.scrollLeft(singleWidth*(pages-1));
						//$wrapper.scrollLeft(0);						
						page=pages;
				}
				else if(page>=pages+1){
					//$wrapper.scrollLeft(singleWidth*(visible));
					$wrapper.scrollLeft(0);
					page=1;
				}
			
				currentPage=page;
			});
			return false;
		}


		if(length_of_li>visible){
			$wrapper.after('<a class="arrow back">Previous Promotion</a><a class="arrow forward">Next Promotion</a>');
			//$('a.back',this).mouseover(function(){$('a.back').addClass("backon");});
			//$('a.back',this).mouseout(function(){$('a.back').removeClass("backon");});
			//$('a.forward',this).mouseover(function(){$('a.forward').addClass("fwdon");});
			//$('a.forward',this).mouseout(function(){$('a.forward').removeClass("fwdon");});
			//$('a.back',this).hover(function(self){this.alertTimerId=setInterval(function(self){gotoPage(currentPage-1,self);},3000);},function(self){clearInterval(this.alertTimerId);});
			//$('a.forward',this).hover(function(self){this.alertTimerId=setInterval(function(self){gotoPage(currentPage+1,self);}, 3000);},function(self){clearInterval(this.alertTimerId);});

			var alertTimerId=setInterval(function(self){ gotoPage(currentPage+1);}, carousel_int);
			//var alertTimerId=setInterval(function(self){ $('a.forward').click();}, carousel_int);

			$('a.back',this).click(function(){
				gotoPage(currentPage-1,self);
				clearInterval(alertTimerId);
				alertTimerId=setInterval(function(self){gotoPage(currentPage+1,self);}, carousel_int);
			});

			$('a.forward',this).click(function(){
				gotoPage(currentPage+1,self);
				clearInterval(alertTimerId);
				alertTimerId=setInterval(function(self){gotoPage(currentPage+1,self);}, carousel_int);
			});

			$(this).bind('goto',function(event,page){
				gotoPage(page);
			});
		
		}
	});

};