var carouselSize = 0;
var carouselCounter = 0;
var carouselList;
var carouselItems;

function initCarousel (strListId, intSize) {
	carouselSize = intSize;
	carouselList = $("#" + strListId);
	carouselItems = $("#" + strListId + " li");
	carouselAnimate();
}

function carouselAnimate() {

	carouselItems.each(function(){
		if (carouselCounter >= (carouselItems.length)/carouselSize) carouselCounter = 0;
		$(this).hide();
		var index = carouselItems.index($(this));
		if (index >= carouselCounter*carouselSize && index < carouselSize*(carouselCounter+1)) {
			$(this).show();
		}
	});

	var currentItems = $("#brands li:visible");
	var delay = 0;
	
	carouselList.css("opacity", "1");
	currentItems.css("opacity", "0");
	
	currentItems.each(function() {
		delay += 400;
		$(this).fadeTo(delay, 0, function()	{
			$(this).fadeTo(400, 1)
		});
	});
	
	carouselList.fadeTo((carouselSize*600)+4000, 1,
		function() {
			$(this).fadeTo(200, 0, function() {
				carouselCounter ++;
				carouselAnimate();
			});
		});
}
