var active = false;
var count = 1;
var slideshowImageCount = 7;

$(document).ready(function() {
	$("#categories li").each(function(i) {
		$(this).removeClass("selected");
	});
	$("#currentSelection").css("top", "-10000px");
	startSlideshow();
	
	$("#slideshowControls li").click(function() {
		var fileName = "";
		var id = $(this).parent().parent().attr("id");
		
		stopSlideshow();
		count = $("#" + id + " li").index($(this)) + 1;
		
		if(count > 9) {
			fileName = count + ".jpg";	
		} else {
			fileName = "0" + count + ".jpg";
		}
		
		$("#categoryPicture img").fadeOut('slow', function() {
			$(this).attr("src", "images/categories/cemeteries/" + fileName);
			$(this).fadeIn('slow');
		});
		
		setSelections($("#slideshowControls li"));
		startSlideshow();
	});
	
	$("#categories li").click(function() {
		var id = $(this).parent().parent().attr("id");
		count = $("#" + id + " li").index($(this)) + 1;
		
		if(count == 1) {
			window.location = "programs/smallercemeteryseminar/index.php";
		} else if(count == 2) {
			window.location = "programs/jcu/index.php";			
		} else if(count == 3) {
			window.location = "programs/convention/index.php";
		} else if(count == 4) {
			window.location = "magazine/introduction.php";
		} else if(count == 5) {
			window.location = "generalInformation/bestPractices.php";
		}
	});
});

function startSlideshow() {
	$("#categoryPicture img").everyTime(6000, 'controlled', function() {
		$(this).fadeOut('slow', function() {
			var fileName = "";
		
			if(count > 9) {
				fileName = count + ".jpg";	
			} else {
				fileName = "0" + count + ".jpg";
			}
			
			$(this).attr("src", "images/categories/cemeteries/" + fileName);
			$(this).fadeIn('slow');
			
			setSelections($("#slideshowControls li"));
		});
		
		if(count == slideshowImageCount) {
			count = 1;
		} else {
			count = count + 1;
		}
	});
}

function setSelections(listItems) {
	$(listItems).each(function(i) {
		$(this).removeClass("selected");
		
		if(i+1 == count) {
			$(this).addClass("selected");
		}
	});
}

function stopSlideshow() {
	$("#categoryPicture img").stopTime('controlled');
}
