var contentElementWidth = 270;
var waitTime = 6000;
var imageIndex1 = 0;
var imageIndex2 = 0;
var imageIndex3 = 0;
var elementCount1 = 5;
var elementCount2 = 5;
var elementCount3 = 5;
var contentIndex1 = 0;
var contentIndex2 = 0;
var contentIndex3 = 0;
var animationTime = 500;

$(document).ready(function() {
	
   $('.productCategoryModule').each(function(i) {
       $(this).attr('id', 'productCategoryModule'+i);
       totalElementsCount = $('.homeModuleContentWrapper', this).children().length;
       totalElementsWidth = totalElementsCount * contentElementWidth; 
       $('.homeModuleContentWrapper', this).width(totalElementsWidth);
       $('.homeModuleContentWrapper', this).css({'position': 'relative'});
       $(this).append('<span class="attributes" style="display: none;"></span>');
       $('.attributes', this).append('<span class="offset">0</span>');
       $('.attributes', this).append('<span class="elementCount">'+totalElementsCount+'</span>');
       $('.attributes', this).append('<span class="elementIndex">1</span>');
       /*$('.homeModuleContentWrapper .homeModContentItem', this).each(function() {
       
       });*/
   });
   
   $('#productCategoryModule0 .leftArrow').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule0 .homeModuleContentWrapper').stop(true, true);
       scrollableElement1 = $(this).parent().parent().parent();
       //scrollableElement1 = $('#productCategoryModule0 .homeModuleContentWrapper');
       currentIndex1 = parseInt($('#productCategoryModule0 .elementIndex', scrollableElement1).html());
       totalElementCount1 = parseInt($('#productCategoryModule0 .elementCount', scrollableElement1).html());
       if(currentIndex1 > 1) {
           $('#productCategoryModule0 .pages a', scrollableElement1).removeClass('selected');
           currentPosition1 = parseInt($('#productCategoryModule0 .offset', scrollableElement1).html());
           $('#productCategoryModule0 .offset', scrollableElement1).html(currentPosition1 - contentElementWidth);
           newPosition1 = parseInt($('#productCategoryModule0 .offset', scrollableElement1).html());
           newIndex1 = currentIndex1-1;
           $('#productCategoryModule0 .elementIndex', scrollableElement1).html(newIndex1);
           $('#productCategoryModule0 .pages a:eq('+(newIndex1-1)+')', scrollableElement1).addClass('selected');
           $('#productCategoryModule0 .homeModuleContentWrapper', scrollableElement1).animate({right: newPosition1+'px'}, 300, "swing");
       }
       if(imageIndex1 == 0) {
			updateContent1(elementCount1-1);
		} else {
		    updateContent1(imageIndex1 - 1);
		}
   });
   
   $('#productCategoryModule1 .leftArrow').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule1 .homeModuleContentWrapper').stop(true, true);
       scrollableElement2 = $(this).parent().parent().parent();
       //scrollableElement1 = $('#productCategoryModule0 .homeModuleContentWrapper');
       currentIndex2 = parseInt($('#productCategoryModule1 .elementIndex', scrollableElement2).html());
       totalElementCount2 = parseInt($('#productCategoryModule1 .elementCount', scrollableElement2).html());
       if(currentIndex2 > 1) {
           $('#productCategoryModule1 .pages a', scrollableElement2).removeClass('selected');
           currentPosition2 = parseInt($('#productCategoryModule1 .offset', scrollableElement2).html());
           $('#productCategoryModule1 .offset', scrollableElement2).html(currentPosition2 - contentElementWidth);
           newPosition2 = parseInt($('#productCategoryModule1 .offset', scrollableElement2).html());
           newIndex2 = currentIndex2-1;
           $('#productCategoryModule1 .elementIndex', scrollableElement2).html(newIndex2);
           $('#productCategoryModule1 .pages a:eq('+(newIndex2-1)+')', scrollableElement2).addClass('selected');
           $('#productCategoryModule1 .homeModuleContentWrapper', scrollableElement2).animate({right: newPosition2+'px'}, 300, "swing");
       }
       if(imageIndex2 == 0) {
			updateContent2(elementCount2-1);
		} else {
		    updateContent2(imageIndex2 - 1);
		}
   });
   
   $('#productCategoryModule2 .leftArrow').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule2 .homeModuleContentWrapper').stop(true, true);
       scrollableElement3 = $(this).parent().parent().parent();
       //scrollableElement1 = $('#productCategoryModule0 .homeModuleContentWrapper');
       currentIndex3 = parseInt($('#productCategoryModule2 .elementIndex', scrollableElement3).html());
       totalElementCount2 = parseInt($('#productCategoryModule2 .elementCount', scrollableElement3).html());
       if(currentIndex3 > 1) {
           $('#productCategoryModule2 .pages a', scrollableElement3).removeClass('selected');
           currentPosition3 = parseInt($('#productCategoryModule2 .offset', scrollableElement3).html());
           $('#productCategoryModule2 .offset', scrollableElement3).html(currentPosition3 - contentElementWidth);
           newPosition3 = parseInt($('#productCategoryModule2 .offset', scrollableElement3).html());
           newIndex3 = currentIndex3-1;
           $('#productCategoryModule2 .elementIndex', scrollableElement3).html(newIndex3);
           $('#productCategoryModule2 .pages a:eq('+(newIndex3-1)+')', scrollableElement3).addClass('selected');
           $('#productCategoryModule2 .homeModuleContentWrapper', scrollableElement3).animate({right: newPosition3+'px'}, 300, "swing");
       }
       if(imageIndex3 == 0) {
			updateContent3(elementCount3-1);
		} else {
		    updateContent3(imageIndex3 - 1);
		}
   });
   
   $('#productCategoryModule0 .rightArrow').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule0 .homeModuleContentWrapper').stop(true, true);
       scrollableElement1 = $(this).parent().parent().parent();
       currentIndex1 = parseInt($('#productCategoryModule0 .elementIndex', scrollableElement1).html());
       totalElementCount1 = parseInt($('#productCategoryModule0 .elementCount', scrollableElement1).html());
       if(currentIndex1 < totalElementCount1) {
           $('#productCategoryModule0 .pages a', scrollableElement1).removeClass('selected');
           currentPosition1 = parseInt($('#productCategoryModule0 .offset', scrollableElement1).html());
           $('#productCategoryModule0 .offset', scrollableElement1).html(currentPosition1 + contentElementWidth);
           newPosition1 = parseInt($('#productCategoryModule0 .offset', scrollableElement1).html());
           newIndex1 = currentIndex1+1;
           $('#productCategoryModule0 .elementIndex', scrollableElement1).html(newIndex1);
           $('#productCategoryModule0 .pages a:eq('+(newIndex1-1)+')', scrollableElement1).addClass('selected');
           $('#productCategoryModule0 .homeModuleContentWrapper', scrollableElement1).animate({right: newPosition1+'px'}, 300, "swing");
       }
       if(imageIndex1 == (elementCount1-1)) {
			updateContent1(0);
		} else {
		    updateContent1(imageIndex1 + 1);
		}
   });
   
   $('#productCategoryModule1 .rightArrow').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule1 .homeModuleContentWrapper').stop(true, true);
       scrollableElement2 = $(this).parent().parent().parent();
       currentIndex2 = parseInt($('#productCategoryModule1 .elementIndex', scrollableElement2).html());
       totalElementCount2 = parseInt($('#productCategoryModule1 .elementCount', scrollableElement2).html());
       if(currentIndex2 < totalElementCount2) {
           $('#productCategoryModule1 .pages a', scrollableElement2).removeClass('selected');
           currentPosition2 = parseInt($('#productCategoryModule1 .offset', scrollableElement2).html());
           $('#productCategoryModule1 .offset', scrollableElement2).html(currentPosition2 + contentElementWidth);
           newPosition2 = parseInt($('#productCategoryModule1 .offset', scrollableElement2).html());
           newIndex2 = currentIndex2+1;
           $('#productCategoryModule1 .elementIndex', scrollableElement2).html(newIndex2);
           $('#productCategoryModule1 .pages a:eq('+(newIndex2-1)+')', scrollableElement2).addClass('selected');
           $('#productCategoryModule1 .homeModuleContentWrapper', scrollableElement2).animate({right: newPosition2+'px'}, 300, "swing");
       }
       if(imageIndex2 == (elementCount2-1)) {
			updateContent2(0);
		} else {
		    updateContent2(imageIndex2 + 1);
		}
   });
   
   $('#productCategoryModule2 .rightArrow').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule2 .homeModuleContentWrapper').stop(true, true);
       scrollableElement3 = $(this).parent().parent().parent();
       currentIndex3 = parseInt($('#productCategoryModule2 .elementIndex', scrollableElement3).html());
       totalElementCount3 = parseInt($('#productCategoryModule2 .elementCount', scrollableElement3).html());
       if(currentIndex3 < totalElementCount3) {
           $('#productCategoryModule2 .pages a', scrollableElement3).removeClass('selected');
           currentPosition3 = parseInt($('#productCategoryModule2 .offset', scrollableElement3).html());
           $('#productCategoryModule2 .offset', scrollableElement3).html(currentPosition3 + contentElementWidth);
           newPosition3 = parseInt($('#productCategoryModule2 .offset', scrollableElement3).html());
           newIndex3 = currentIndex3+1;
           $('#productCategoryModule2 .elementIndex', scrollableElement3).html(newIndex3);
           $('#productCategoryModule2 .pages a:eq('+(newIndex3-1)+')', scrollableElement3).addClass('selected');
           $('#productCategoryModule2 .homeModuleContentWrapper', scrollableElement3).animate({right: newPosition3+'px'}, 300, "swing");
       }
       if(imageIndex3 == (elementCount3-1)) {
			updateContent3(0);
		} else {
		    updateContent3(imageIndex3 + 1);
		}
   });
   
   $('#productCategoryModule0 .pages a').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule0 .homeModuleContentWrapper').stop(true, true);
       scrollableElement1 = $(this).parent().parent().parent();
       //scrollableElement1 = $('#productCategoryModule0 .homeModuleContentWrapper');
       $('#productCategoryModule0 .pages a').removeClass('selected');
       $(this).addClass('selected');
       newIndex1 = $('#productCategoryModule0 .pages a', scrollableElement1).index(this)+1;
       newPosition1 = (newIndex1-1) * contentElementWidth;
       $('#productCategoryModule0 .offset', scrollableElement1).html(newPosition1);
       $('#productCategoryModule0 .elementIndex', scrollableElement1).html(newIndex1);
       $('#productCategoryModule0 .homeModuleContentWrapper', scrollableElement1).animate({right: newPosition1+'px'}, 300, "swing");
       navNumber1 = $(this).html();
	   updateContent1(navNumber1 - 1);
   });
   
   $('#productCategoryModule1 .pages a').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule1 .homeModuleContentWrapper').stop(true, true);
       scrollableElement2 = $(this).parent().parent().parent();
       //scrollableElement2 = $('#productCategoryModule1 .homeModuleContentWrapper');
       $('#productCategoryModule1 .pages a').removeClass('selected');
       $(this).addClass('selected');
       newIndex2 = $('#productCategoryModule1 .pages a', scrollableElement2).index(this)+1;
       newPosition2 = (newIndex2-1) * contentElementWidth;
       $('#productCategoryModule1 .offset', scrollableElement2).html(newPosition2);
       $('#productCategoryModule1 .elementIndex', scrollableElement2).html(newIndex2);
       $('#productCategoryModule1 .homeModuleContentWrapper', scrollableElement2).animate({right: newPosition2+'px'}, 300, "swing");
       navNumber2 = $(this).html();
	   updateContent2(navNumber2 - 1);
   });
   
   $('#productCategoryModule2 .pages a').click(function(e) {
       e.preventDefault();
       $('#productCategoryModule2 .homeModuleContentWrapper').stop(true, true);
       scrollableElement3 = $(this).parent().parent().parent();
       //scrollableElement3 = $('#productCategoryModule2 .homeModuleContentWrapper');
       $('#productCategoryModule2 .pages a').removeClass('selected');
       $(this).addClass('selected');
       newIndex3 = $('#productCategoryModule2 .pages a', scrollableElement3).index(this)+1;
       newPosition3 = (newIndex3-1) * contentElementWidth;
       $('#productCategoryModule2 .offset', scrollableElement3).html(newPosition3);
       $('#productCategoryModule2 .elementIndex', scrollableElement3).html(newIndex3);
       $('#productCategoryModule2 .homeModuleContentWrapper', scrollableElement3).animate({right: newPosition3+'px'}, 300, "swing");
       navNumber3 = $(this).html();
	   updateContent3(navNumber3 - 1);
   });
   
   
   initGallery1();
   setTimeout(function(){ initGallery2(); }, 2000);
   setTimeout(function(){ initGallery3(); }, 4000);
   
});


function initGallery1() {
	rotateTimeout1 = setTimeout(function() {
		rotateImage1();
	}, waitTime);
};

function updateContent1(index) {
    imageIndex1 = index-1;
    contentIndex1 = index-1;
    clearTimeout(rotateTimeout1);
    rotateImage1();
}

function rotateImage1() {
    imageIndex1++;
	contentIndex1 = contentIndex1 == elementCount1-1 ? 0 : contentIndex1+1;	
	//updateText(contentIndex);
    if(imageIndex1 == 6){
        imageIndex1 = 1;
        $('#productCategoryModule0 .homeModuleContentWrapper').css({right: '0px'});
    }
    $('#productCategoryModule0 .homeModuleContentWrapper').stop(true, true);
    $('#productCategoryModule0 .homeModuleContentWrapper').animate({right: (imageIndex1*contentElementWidth)+'px'}, animationTime, "swing");
	rotateTimeout1 = setTimeout(function() {
		rotateImage1();
	}, waitTime);
	$('#productCategoryModule0 .pages a').removeClass('selected');
    if(imageIndex1 == 5){
    	$('#productCategoryModule0 .pages a:eq(0)').addClass('selected');
    } else {
	    $('#productCategoryModule0 .pages a:eq('+(imageIndex1)+')').addClass('selected');
    }
    $('#productCategoryModule0 .offset').html(imageIndex1);
    $('#productCategoryModule0 .elementIndex').html(imageIndex1+1);
};


function initGallery2() {
	rotateTimeout2 = setTimeout(function() {
		rotateImage2();
	}, waitTime);
};

function updateContent2(index) {
    imageIndex2 = index-1;
    contentIndex2 = index-1;
    clearTimeout(rotateTimeout2);
    rotateImage2();
}

function rotateImage2() {
    imageIndex2++;
	contentIndex2 = contentIndex2 == elementCount2-1 ? 0 : contentIndex2+1;	
	//updateText(contentIndex);
    if(imageIndex2 == 6){
        imageIndex2 = 1;
        $('#productCategoryModule1 .homeModuleContentWrapper').css({right: '0px'});
    }
    $('#productCategoryModule1 .homeModuleContentWrapper').stop(true, true);
    $('#productCategoryModule1 .homeModuleContentWrapper').animate({right: (imageIndex2*contentElementWidth)+'px'}, animationTime, "swing");
	rotateTimeout2 = setTimeout(function() {
		rotateImage2();
	}, waitTime);
	$('#productCategoryModule1 .pages a').removeClass('selected');
    if(imageIndex2 == 5){
    	$('#productCategoryModule1 .pages a:eq(0)').addClass('selected');
    } else {
	    $('#productCategoryModule1 .pages a:eq('+(imageIndex2)+')').addClass('selected');
    }
    $('#productCategoryModule1 .offset').html(imageIndex2);
    $('#productCategoryModule1 .elementIndex').html(imageIndex2+1);
};

function initGallery3() {
	rotateTimeout3 = setTimeout(function() {
		rotateImage3();
	}, waitTime);
};

function updateContent3(index) {
    imageIndex3 = index-1;
    contentIndex3 = index-1;
    clearTimeout(rotateTimeout3);
    rotateImage3();
}

function rotateImage3() {
    imageIndex3++;
	contentIndex3 = contentIndex3 == elementCount3-1 ? 0 : contentIndex3+1;	
	//updateText(contentIndex);
    if(imageIndex3 == 6){
        imageIndex3 = 1;
        $('#productCategoryModule2 .homeModuleContentWrapper').css({right: '0px'});
    }
    $('#productCategoryModule2 .homeModuleContentWrapper').stop(true, true);
    $('#productCategoryModule2 .homeModuleContentWrapper').animate({right: (imageIndex3*contentElementWidth)+'px'}, animationTime, "swing");
	rotateTimeout3 = setTimeout(function() {
		rotateImage3();
	}, waitTime);
	$('#productCategoryModule2 .pages a').removeClass('selected');
    if(imageIndex3 == 5){
    	$('#productCategoryModule2 .pages a:eq(0)').addClass('selected');
    } else {
	    $('#productCategoryModule2 .pages a:eq('+(imageIndex3)+')').addClass('selected');
    }
    $('#productCategoryModule2 .offset').html(imageIndex3);
    $('#productCategoryModule2 .elementIndex').html(imageIndex3+1);
};


