var whichImage = 1;
var maxImage = 4;
var nextInterval = null;


$(function() {
	function nextImage() {
		if(nextInterval) {
			clearTimeout(nextInterval);
		}
		$(".homeSlivers").removeClass('homeImages-slivers-inactive');
		$(".homeSlivers").removeClass('homeImages-slivers-active');
		$(".homeSlivers").not("#" + whichImage).addClass('homeImages-slivers-inactive');
		$("#" + whichImage).addClass('homeImages-slivers-active');
		$("#homeFull-" + whichImage).css("z-index","11");
		$("#homeFull-" + whichImage).fadeIn(1000, function() {
			$("#homeImages-fullSize div").not("#homeFull-" + whichImage).css("z-index","9");
			$("#homeImages-fullSize div").not("#homeFull-" + whichImage).css("display","none");
			$("#homeFull-" + whichImage).css("z-index","10");
			if(whichImage < maxImage) {
				whichImage = eval(whichImage + 1);
			} else {
				whichImage = 0;
			}
			nextInterval = setTimeout(nextImage,5000);
		});
	}
	
	try {
		if(!nextInterval) {
			nextInterval = setTimeout(nextImage,5000);
		}}catch(e){}
	
	$(".homeSlivers").click(
		function() {
			if ($("#homeImages-fullSize div:animated").size() == 0) {
				if(nextInterval) {
					clearTimeout(nextInterval);
				}
				var whichClicked = $(this).attr("id");
				$(".homeSlivers").removeClass('homeImages-slivers-inactive');
				$(".homeSlivers").removeClass('homeImages-slivers-active');
				$(".homeSlivers").not($(this)).addClass('homeImages-slivers-inactive');
				$(this).addClass('homeImages-slivers-active');
				$("#homeFull-" + whichClicked).css("z-index","11");
				$("#homeFull-" + whichClicked).fadeIn(1000, function() {
					$("#homeImages-fullSize div").not("#homeFull-" + whichClicked).css("z-index","9");
					$("#homeImages-fullSize div").not("#homeFull-" + whichClicked).css("display","none");
					$("#homeFull-" + whichClicked).css("z-index","10");
				});
			}
		}
	)	
});