// JavaScript Document

var images = $("#rotator .imageHolder"); // Array of images
var stopRotate = 'no'; // "no" for rotation, "yes" for rotation to stop
var flipspeed = 7; // Number of seconds between flips
var curImage = 1; // Image to start displaying - recommend to keep at 1
var numImages = images.size(); // Gets the size of the array of images

if (images.length > 1) {
	$(document).ready(function() {
		rotateImages();
		
		images.each(
			function(index, domEle) {
				var curIndex = index + 1;
				//$(domEle).before("<a href='#' class='itemID'>" + curIndex + "</a>");
			}
		);
	
		$('#rotator a').hover(function(){
			$(this).parent().addClass("hover");
		},function(){
			$(this).parent().removeClass("hover");
		});
						   
		$('#rotator a.itemID').click(function(){
			stopRotate = 'yes';
			$('#rotator li').removeClass("active");
			$(this).parent().addClass("active");
			$('#rotator .imageHolder').each(function() {
				if($(this).is(":visible"))
				{
					$(this).fadeOut();
				}
			});
			$('#' + $(this).parent().attr("id") + '_holder').fadeIn();
			return false;
		});
	});
}

function viewSection(id) {
	$('#rotator li').removeClass("active");
	$('#image'+id).addClass("active");
	images.each(function() {
		if($(this).is(":visible"))
		{
			$(this).fadeOut();
		}
	});
	$('#image'+id+'_holder').fadeIn();
}

function rotateImages() {
	if (stopRotate != 'yes') {
		setTimeout(function() { 
			if (curImage < numImages) {
				curImage++;
			} else {
				curImage = 1;
			}
			if (stopRotate == 'no')
			{
				viewSection(curImage);
			}
			rotateImages();
		}, flipspeed*1000);
	}
}
