$(document).ready(function (){
	// set JS class for CSS
	$('HTML').addClass('JS');
	
	
	// rotating books on home page
	if ($("#carousel").length) {
		function next_item() {
			// fade out current item
			$("#carousel .item").eq(current_item - 1).animate({top:"0", opacity:"0"}, 700, function() {
				$(this).hide();
			});
			// advance to next item
			current_item++; if (current_item > $("#carousel .item").length) current_item = 1;
			// save current item to cookie
			$.cookie('saved_home_item', current_item);
			// show new item under old fading out one
			$("#carousel .item").eq(current_item -1).css({top:"0", opacity:"0"}).show().animate({top:"0", opacity:"1"}, 700);
		}

	   	var current_item = 1;
		if ($.cookie('saved_home_item')) { // if we've saved a cookie...
    		current_item = $.cookie('saved_home_item'); // load in saved item
			current_item++; if (current_item > $(".rotating-items").length) current_item = 1; // advance to the next one
    	}
		$.cookie('saved_home_item', current_item); // resave item
    	$("#carousel .item").hide().eq(current_item - 1).show(); // display item
	   	var item_timer = setInterval(next_item, 5000);
	}
	
	/* IMAGE GALLERY */
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="tip7-title"><span><a href="javascript:;" onclick="$.fancybox.close();">Close</a></span>' + (title && title.length ? '<strong>' + title + '</strong>' : '' ) + '(Image ' + (currentIndex + 1) + ' of ' + currentArray.length + ')</div>';
}
	
	$("ul.gallery a").fancybox({
		'speedIn'		:	400,
		'speedOut'		:	200,
		'overlayColor'	:	'#000',
		'showCloseButton'	: false,
		'titlePosition' 		: 'inside',
		'titleFormat'		: formatTitle
	});

	
	/* LOAD AUDIO PLAYERS */
	$('a.audio').each(function() {
		var filename = $(this).attr("href"); // grab the file name from the link
		var my_player_id = $(this).attr("id"); // get unique id for this player
		// replace link with div
		$(this).replaceWith('<div class="audio-player" id="' + my_player_id + '"></div>');
		// load in flash player
		$(".audio-player#" + my_player_id).flash(
			{
				swf: '/scripts/audio-player.swf',
				height: 29,
				width: 29,
				wmode: 'transparent',
				flashvars: {
					file: filename,
					player_id: my_player_id
				}
			}
		);
		$(this).attr("href", "");
	});

	/* CONTACT FORM */
	
	$("body#contact form").submit(function() {
		var error = "";
		
		if ($("input#name").val()=="") {
			error = error + "\nPlease enter your name";
		}
		
		var reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		email = $("input#from").val();
		if (!email) {
			error = error + "\nPlease enter your email address";
		} else if (!reg.test(email)) {
			error = error + "\nPlease enter a valid email address";
		}
		
		if ($("input#subject").val()=="") {
			error = error + "\nPlease enter a subject";
		}

		if ($("textarea#message").val()=="") {
			error = error + "\nPlease enter a message";
		}

		if (error) {
			alert ("Your message could not be sent:\n" + error);
			return false;
		}
	});

});

/* AUDIO CONTROLS - KEEP OUT OF JQUERY READY LOOP */
function stop_all_audio(omit) {
	$(function(){
		$(".audio-player").not("#" + omit).children("object").flash(
			function() {
				this.GotoFrame(0);
			}
		);
	});
}
function advance_track(last_played) {
	var my_player = $("div.audio-player#" + last_played); // get last player (ie. one that called this function)
	var players = $("div.audio-player");  // get all players
	var idx = players.index(my_player);  // get the index position of last played one
	if (idx == players.length - 1) return; // if we're on the last one, return
	var next = players.eq( idx + 1 ).children("object"); // grab the flash object from the incremented index
	// and finally, set our new one to play
	$(next).flash(
		function() {
			this.GotoFrame(1);
		}
	);
}
