(jQuery.noConflict())(function($){
	var interval;

	//CUSTOMIZABLE VALUES							
	var autoplay = 5000; /* SET MILLISECONDS */
	var isLoop = true;
	var alwaysDisplayFullCarousel = true;
	var nb_scroll = 1; /* SET THE VALUE OF NB ITEM TO SCROLL */
	
	var nb_display = 1; /* SET THE VALUE OF NB ITEM DISPLAYED */

	//INIT VARS
	var isBusy = false;
	var isMouseOver = false;
	var width = $("ul#r-content > li").width() 
					+ parseInt($("ul#r-content > li").css("padding-left"), 10)
					+ parseInt($("ul#r-content > li").css("padding-right"), 10)
					+ parseInt($("ul#r-content > li").css("margin-left"), 10)
					+ parseInt($("ul#r-content > li").css("margin-right"), 10)
					+ parseInt($("ul#r-content > li").css("borderRightWidth"), 10)
					+ parseInt($("ul#r-content > li").css("borderLeftWidth"), 10);
	var viewport_width = $("#r-window").width();
	var nb_items = $("ul#r-content > li").size();
	var max = nb_items * width;
	var scroll_width = nb_scroll * width;
	
	//custom vars
	var position = 1; 
	selectTab(position);

	//INIT CONTAINER
	$("#r-content").css({'left': '0px', 'width': max+'px'});

	//FUNCTIONALITIES
	$("#carousel").hover(
			function(){
				isMouseOver=true;
			},
			function(){
				isMouseOver=false;
			}
	);

	$("#navigator-right").click(function(){
		goToNext();
		return false;
	});

	$("#navigator-left").click(function(){
		goToPrev();
		return false;
	});

	if(autoplay > 0)
	{
		interval = setInterval(changeSlide, autoplay);
		isLoop = true;
	}

	function changeSlide(){
		if(!isMouseOver){
			goToNext();
		}
	}

	function goToNext(){
		if(isBusy)
		{
			return;
		}
		isBusy = true;

		var newLeft = parseInt($("#r-content").css('left'), 10) - scroll_width;
		
		//custom
		position += nb_scroll;
		
		if(newLeft > -max)
		{
			if(alwaysDisplayFullCarousel)
			{
				if((newLeft - scroll_width) < -max)
				{
					newLeft += ((nb_scroll-(nb_items%nb_display)) * width);
				}
				$("#r-content").animate({left: newLeft+'px'}, 500);
			}
			else
			{
				$("#r-content").animate({left: newLeft+'px'}, 500);
			}
		}
		else
		{
			if(isLoop)
			{
				$("#r-content").animate({left: '0'}, 150);
				position = 1;
			}
			else
			{
				$("#r-content").animate({left: '-=10'}, 150).animate({left: '+=10'}, 150);
				position -= nb_scroll;
			}
		}
		
		//custom
		selectTab(position);

		isBusy = false;
	}

	function goToPrev(){
		if(isBusy)
		{
			return;
		}
		isBusy = true;
		var newLeft = parseInt($("#r-content").css('left'), 10) + scroll_width;
		
		//custom
		position -= nb_scroll;

		if(newLeft >= scroll_width)
		{
			if(isLoop)
			{
				if(alwaysDisplayFullCarousel)
				{
					newLeft = -max + scroll_width;
					position = 4;
				}
				else
				{
					newLeft = -max + ((nb_scroll-(nb_items%nb_display)) * width);
					position = 1;
				}
				$("#r-content").animate({left: newLeft+'px'}, 500);
			}
			else
			{
				$("#r-content").animate({left: '+=10'}, 150).animate({left: '-=10'}, 150);
			}
		}
		else
		{
			if(isLoop)
			{
				if(alwaysDisplayFullCarousel)
				{
					if(newLeft >= scroll_width)
					{
						newLeft = -max + scroll_width;
						position = 4;
					}
				}
				if(newLeft > 0) 
				{
					newLeft = 0;
					position = 1;
				}
				$("#r-content").animate({left: newLeft+'px'}, 500);
			}
			else
			{
				if(alwaysDisplayFullCarousel)
				{
					if(newLeft >= scroll_width)
					{
						$("#r-content").animate({left: '+=10'}, 150).animate({left: '-=10'}, 150);
					}
				}
				if(newLeft > 0) 
				{
					newLeft = 0;
					position = 1;
				}
				$("#r-content").animate({left: newLeft+'px'}, 500);
			}
		}
		
		//custom
		selectTab(position);

		isBusy = false;
	}
	
	/***************/
	/* CUSTOM CODE */
	/***************/
	$("#navigator a").click(function(){
		var tab_number = $(this).attr("href").replace("#", "");
		
		if(IsNumeric(tab_number))
		{
			var newLeft = (tab_number-1) * -scroll_width;
			if(alwaysDisplayFullCarousel)
			{
				if((newLeft - scroll_width) < -max)
				{
					newLeft += ((nb_scroll-(nb_items%nb_display)) * width);
				}
			}
			$("#r-content").animate({left: newLeft+'px'}, 500);
			
			$("#navigator a").removeClass("selected");
			$(this).addClass("selected");
			position = Number(tab_number);
		}
		else
		{
			if(tab_number == "prev")
			{
				goToPrev();
			}
			else if(tab_number == "next")
			{
				goToNext();
			}
		}
		
		return false;
	});
	
	function selectTab(tab) 
	{
		$("#navigator a").each(function(){
			$(this).removeClass("selected");
			var tab_number = $(this).attr("href").replace("#", "");
			if(tab_number == tab)
			{
				$(this).addClass("selected");
			}
		});		
	}
	
});

function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

