$(document).ready(function() {
	loadRotatingImages();
	loadGalleryImages();
	var InfiniteRotator = {
		init : function() {
			// initial fade-in time (in milliseconds)
			var initialFadeIn = 1000;

			// interval between items (in milliseconds)
			var itemInterval = 4000;

			// cross-fade time (in milliseconds)
			var fadeTime = 2500;

			// count number of items
			var numberOfItems = $('.rotating-item').length;

			// set current item
			var currentItem = fetchRandomNumber(numberOfItems);

			// show first item
			$('.rotating-item').eq(currentItem).fadeIn(initialFadeIn);

			// loop through the items
			var infiniteLoop = setInterval(function() {
				$('.rotating-item').eq(currentItem).fadeOut(fadeTime);

				currentItem = fetchRandomNumber(numberOfItems);
				$('.rotating-item').eq(currentItem).fadeIn(fadeTime);

			}, itemInterval);
		}
	};

	InfiniteRotator.init();
	showMelodrama();
	
	initGalleries();
});

function initGalleries() {
	$("div.makeMeScrollable").smoothDivScroll({
		autoScroll: "always", 
		autoScrollDirection: "endlessloopright", 
		autoScrollStep: 2, 
		autoScrollInterval: 15
	});
														
	$("div.makeMeScrollable a").colorbox({speed:"500"});
	
	// Pause autoscrolling if the user clicks one of the images
	$("div.makeMeScrollable").bind("click", function() {
		$(this).smoothDivScroll("stopAutoScroll");
	});
	
	// Start autoscrolling again when the user closes
	// the colorbox overlay
	$(document).bind('cbox_closed', function(){
		$("div.makeMeScrollable").smoothDivScroll("startAutoScroll");
	});
}

function loadGalleryImages() {
	var imageSource = "";
	for (var i = 1; i <= 34; i++) {
		imageSource="<a href='images/2012/Melodrama/Melodrama_2012 (" + i + ").jpg'><img src='images/2012/Melodrama/Melodrama_2012 (" + i + ").jpg' height='240' width='320' border='0' /></a>";
		$('#galleryMelodrama2012').append(imageSource);
	}
	for (var i = 1; i <= 25; i++) {
		imageSource="<a href='images/2012/Olio/Olio_2012 (" + i + ").jpg'><img src='images/2012/Olio/Olio_2012 (" + i + ").jpg' height='240' width='320' border='0' /></a>";
		$('#galleryOlio2012').append(imageSource);
	}
}

function fetchRandomNumber(numberOfItems) {
	return parseInt(Math.random() * numberOfItems);
}

function loadRotatingImages() {
	var imageSource = "";
	for (var i = 1; i <= 350; i++) {
		imageSource = "<img src='images/2009/Olio/Olio_2009 (" + i + ").JPG' class='rotating-item' />";
		$('#rotating-item-wrapper').append(imageSource);
	}
	for (i = 1; i <= 145; i++) {
		imageSource = "<img src='images/2009/Melodrama/Melodrama_2009 (" + i + ").JPG' class='rotating-item' />";
		$('#rotating-item-wrapper').append(imageSource);
	}
	for (i = 1; i <= 11; i++) {
		imageSource = "<img src='images/rotating/rotating (" + i + ").gif' class='rotating-item' />";
		$('#rotating-item-wrapper').append(imageSource);
	}
	for (i = 1; i <= 13; i++) {
		imageSource = "<img src='images/rotating/rotating (" + i + ").jpg' class='rotating-item' />";
		$('#rotating-item-wrapper').append(imageSource);
	}
}

function showOlioOption(id) {
	hideOptions();
	var optionId = "#" + id;
	var spanId = optionId + "Span";
	$(optionId).show();
	$("#olioMusicSpan").removeClass().addClass("option");
	$("#olioCostumeSpan").removeClass().addClass("option");
	$(spanId).removeClass().addClass("option_current");
}

function showMelodramaOption(id) {
	hideOptions();
	var optionId = "#" + id;
	var spanId = optionId + "Span";
	$(optionId).show();
	$(spanId).removeClass().addClass("option_current");
}

function hideOptions() {
	$("#olioMusic").hide();
	$("#olioCostume").hide();
}

function showMelodrama() {
	hideOptions();
	$('#melodramaDiv').show();
	$('#olioDiv').hide();
	$("#melodramaSubMenu").removeClass().addClass("selected");
	$("#olioSubMenu").removeClass();
}

function showOlio() {
	hideOptions();
	$('#melodramaDiv').hide();
	$('#olioDiv').show();
	$("#melodramaSubMenu").removeClass();
	$("#olioSubMenu").removeClass().addClass("selected");
	showOlioOption("olioMusic");
}
