function homepageGridCarousel() 
{
	//Set up needed markup
	$('table.grid').wrap('<div class="hompageGridProductsCarousel" />');
	$('.hompageGridProductsCarousel').wrap('<div class="hompageGridProductsCarouselWrapper" />');
	$('.hompageGridProductsCarousel').before('<span id="carouselGoLeftButton">Left</span>');
	$('.hompageGridProductsCarousel').after('<span id="carouselGoRightButton">Right</span>');
	
	
	var carouselHeight = $('table.grid .product-details').outerHeight() + $('table.grid .product-image').outerHeight();
	var carouselStepWidth =  $('table.grid .product-image').outerWidth();
	
	$('.hompageGridProductsCarousel').css('height', carouselHeight);
	
	//Transform Grid products table for carousel
	$('.hompageGridProductsCarousel table.grid td').each(
		function()
		{
			$(this).css('width', $(this).width());
		}
	);
	$('.hompageGridProductsCarousel table.grid > tr ').css('float', 'left');
	
	var divs = $('.hompageGridProductsCarousel table.grid tbody > tr ');
	for(var i = 0; i < divs.length; i+=2) {
	  divs.slice(i, i+2).wrapAll("<tbody class='carouselMerrgedRows'></tbody>");
	}
	var carouselTableWidth = 0;
	$('.carouselMerrgedRows').each(
		function() 
		{
			carouselTableWidth = carouselTableWidth + $(this).width();
		}	
	);
	
	$('.hompageGridProductsCarousel table.grid').css('width', carouselTableWidth);
	countEmptyGrid = 0
	$('.hompageGridProductsCarousel table.grid td.grid-empty').each(function(){
		countEmptyGrid = countEmptyGrid + 1;
	});
	
	//Carousel 
	var carouselMoveWidth = carouselTableWidth - ($('.hompageGridProductsCarousel table.grid td.grid-empty').outerWidth()*countEmptyGrid) - $('.hompageGridProductsCarousel').width(); 
	var carouselStepTime = 2000;
	var carouselMovedPosition = 0;
	
	function carouselStepLeft() {
		if ( carouselMovedPosition > -carouselMoveWidth) {
			carouselStep = '-=' + carouselStepWidth;
			$('table.grid').animate({
					left: carouselStep
				}, 
				carouselStepTime,
				function() 
				{
					carouselMovedPosition = carouselMovedPosition - carouselStepWidth;	
					console.log(carouselMoveWidth);
					console.log(carouselMovedPosition);
				}
			);
		}
	}
	
	function carouselStepRight() {
		if ( carouselMovedPosition < 0 ) {
			carouselStep = '+=' + carouselStepWidth;
			$('table.grid').animate({
					left: carouselStep
				}, carouselStepTime,
				function() 
				{
					carouselMovedPosition = carouselMovedPosition + carouselStepWidth;	
					console.log(carouselMovedPosition);
				}
			);
		}
	}
	
	//startCarousel();
	$('#carouselGoLeftButton').click(function(){
		carouselStepLeft();
	}) 
	$('#carouselGoRightButton').click(function(){
		carouselStepRight();
	}) 
} 

$(function() {
	if ($('body').hasClass('dynamic-page-index')) {
		homepageGridCarousel();
	}
});

