var contentElementWidth = 440;
/*var elementCount = 0;*/
var contentIndex = 0;
var imageIndex = 0;
var irWaitTime = 4000;
var animationTime = 500;
var captionWidth = 288; 
var rotateTimeout = new Array();
var elementCount = new Array();
 
$(document).ready(function() {
	
	$('.imageRotator').each(function(index) {
		rotatorIndex = index;
		elementCount[rotatorIndex] = $('.imageGalleryListSet', this).children('li').length;

		$('.imageGalleryListSet li', this).each(function(i) {
		    if(i == 0) {
		        $(this).parent().parent().parent().parent().parent().siblings('.irControls').append('<a href="#" class="selected">'+(i+1)+'</a>');
		    } else {
		        $(this).parent().parent().parent().parent().parent().siblings('.irControls').append('<a href="#">'+(i+1)+'</a>');
		    }
		});
		
		var irNavWidth = $(this).siblings('.irControls').children('a').size();
		var irControlsWidth = 25 * irNavWidth;
		$(this).siblings('.irControls').css({"width": irControlsWidth});	
		
		$('.imageGalleryListSet', this).width((elementCount[rotatorIndex] * contentElementWidth)+440);
		$('.imageGalleryListSet', this).css({'position': 'relative'});
		$('.imageGalleryListSet', this).append('<li>'+$('.imageGalleryListSet li:eq(0)', this).html()+'</li>');
		
		$(this).siblings('.irControls').children('a').click(function(e) {
			rotatorIndex = $('.irControls').index($(this).parent()); 
			e.preventDefault();
			navNumber = $(this).html();
		    updateContent(navNumber - 1, elementCount[rotatorIndex], rotatorIndex);
		});
		
		$('.imageRotatorWrapper', this).css({display: 'block'});
		
	    initGallery($('.imageRotatorWrapper', this), elementCount[rotatorIndex], imageIndex, contentIndex, rotatorIndex);
	});
	
});

function initGallery(selectedElement, elementCount, imageIndex, contentIndex, rotatorIndex) {
	$(selectedElement).animate({opacity: 1}, 1000, "swing", function() {
		rotateTimeout[rotatorIndex] = setTimeout(function() {
			rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex);
		}, irWaitTime);
	});
};

function updateContent(index, elementCount, rotatorIndex) {
    imageIndex = index-1;
    contentIndex = index-1;
    clearTimeout(rotateTimeout[rotatorIndex]);
    rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex);
}

function rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex) {
    imageIndex++;
//    console.log(imageIndex);
//    console.log(elementCount);
	contentIndex = contentIndex == elementCount-1 ? 0 : contentIndex+1;	
    var rotatorLocation = $('.imageRotator:eq('+rotatorIndex+')');
    rotatorLocation.siblings('.irControls').children('a').removeClass('selected');
    rotatorLocation.siblings('.irControls').children('a:eq('+contentIndex+')').addClass('selected');
    if(imageIndex == elementCount+1){
        imageIndex = 1;
        $('.imageGalleryListSet', rotatorLocation).css({right: '0px'});
    }
    $('.imageGalleryListSet', rotatorLocation).stop(true, true);
    $('.imageGalleryListSet', rotatorLocation).animate({right: (imageIndex*contentElementWidth)+'px'}, animationTime, "swing");
	rotateTimeout[rotatorIndex] = setTimeout(function() {
		rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex);
	}, irWaitTime);
}
