$(document).ready(function (){
	// set JS class for CSS
	$('HTML').addClass('JS');
	
	if ($("body#home").length) {
		// rotate OUT NOW albums
		function next_album() {
			// fade out current album
			$("#out-now .album").eq(current_album - 1).animate({left:"-220px", opacity:"0"}, 700);
			// advance to next album
			current_album++; if (current_album > $("#out-now .album").length) current_album = 1;
			// save current album to cookie
			$.cookie('saved_home_album', current_album);
			// show new album under old fading out one
			$("#out-now .album").eq(current_album -1).css({left:"220px", opacity:"0"}).show().animate({left:"0", opacity:"1"}, 700);
		}

	   	var current_album = 1;
		if ($.cookie('saved_home_album')) { // if we've saved a cookie...
    		current_album = $.cookie('saved_home_album'); // load in saved album
			current_album++; if (current_album > $(".rotating-albums").length) current_album = 1; // advance to the next one
    	}
		$.cookie('saved_home_album', current_album); // resave album
    	$("#out-now .album").hide().eq(current_album - 1).show(); // display album
	   	var album_timer = setInterval(next_album, 6000);
	}
	
	// MAILING LIST FORM
	
	if ($("body#home form#mailing-list-form").length) {
		$("form#mailing-list-form input.name").val("Your name").focus(function() {
			if ($(this).val()=="Your name") $(this).val("");
		}).blur(function() {
			if ($(this).val()=="") $(this).val("Your name");
		})
		$("form#mailing-list-form input.email").val("Your email address").focus(function() {
			if ($(this).val()=="Your email address") $(this).val("");
		}).blur(function() {
			if ($(this).val()=="") $(this).val("Your email address");
		})
	}
	
	// GIG PUBLISH FORM
	
	if ($(".publish-form.gig").length) {

		// if entry date in publish form is set, select relevant drop-downs...
		var ed = $(".publish-form.gig input#entry_date").val();
		$(".date-picker.entry_date select.d").val(ed.substr(8,2));
		$(".date-picker.entry_date select.m").val(ed.substr(5,2));
		$(".date-picker.entry_date select.y").val(ed.substr(0,4));
		// do same for expiration date, if it's set...
		ed = $(".publish-form.gig input#expiration_date").val();
		if (ed != "") {
			$(".date-picker.expiration_date select.d").val(ed.substr(8,2));
			$(".date-picker.expiration_date select.m").val(ed.substr(5,2));
			$(".date-picker.expiration_date select.y").val(ed.substr(0,4));
		}

		$(".publish-form.gig form").submit(function() {
		
			/* set entry and expiry dates from date pickers */
			$(".publish-form.gig input#entry_date").val($(".date-picker.entry_date select.y").val() + "-" + $(".date-picker.entry_date select.m").val() + "-" + $(".date-picker.entry_date select.d").val() + " 10:00 PM");
			if ($(".date-picker.expiration_date select.y").val()=="" || $(".date-picker.expiration_date select.m").val()=="" || $(".date-picker.expiration_date select.d").val()=="") {
				$(".publish-form.gig input#expiration_date").val("");
			} else {
				$(".publish-form.gig input#expiration_date").val($(".date-picker.expiration_date select.y").val() + "-" + $(".date-picker.expiration_date select.m").val() + "-" + $(".date-picker.expiration_date select.d").val() + " 10:00 PM");
			}
		
			// set time to 11:59 PM
		//	$(".publish-form.gig input#entry_date").val($(".publish-form.gig input#entry_date").val().substr(0, 10) + " 10:00 PM");
			// build entry title
			var title = "";
			title += $(".publish-form.gig input#entry_date").val().substr(0, 10) + " ";
			if ($(".publish-form.gig select#gigs-band").val()) {
				title += $(".publish-form.gig select#gigs-band option:selected").text();
			} else {
				title += $(".publish-form.gig input#gigs-quick-band").val();
			}
			// set title
			$(".publish-form.gig input.title").val(title);
			// set url title
			$(".publish-form.gig input.url_title").val(title.replace(/\W/g, "-").toLowerCase());
			
			// clear default values from quick submit form

			if ($("input#gigs-quick-band").val()=="Quick band") $("input#gigs-quick-band").val("");
			if ($("input#gigs-quick-venue").val()=="Quick venue") $("input#gigs-quick-venue").val("");
			if ($("input#gigs-quick-town").val()=="Quick town") $("input#gigs-quick-town").val("");
			if ($("input#gigs-band-name-appendix").val()=="Band name appendix") $("input#gigs-band-name-appendix").val("");

			
		});

		if ($("input#gigs-quick-band").val()=="") $("input#gigs-quick-band").val("Quick band");
		$("input#gigs-quick-band").focus(function() {
			if ($(this).val()=="Quick band") $(this).val("");
		}).blur(function() {
			if ($(this).val()=="") $(this).val("Quick band");
		});
		
		if ($("input#gigs-quick-venue").val()=="") $("input#gigs-quick-venue").val("Quick venue");
		$("input#gigs-quick-venue").focus(function() {
			if ($(this).val()=="Quick venue") $(this).val("");
		}).blur(function() {
			if ($(this).val()=="") $(this).val("Quick venue");
		});	
		
		if ($("input#gigs-quick-town").val()=="") $("input#gigs-quick-town").val("Quick town");
		$("input#gigs-quick-town").focus(function() {
			if ($(this).val()=="Quick town") $(this).val("");
		}).blur(function() {
			if ($(this).val()=="") $(this).val("Quick town");
		});	
		
		if ($("input#gigs-band-name-appendix").val()=="") $("input#gigs-band-name-appendix").val("Band name appendix");
		$("input#gigs-band-name-appendix").focus(function() {
			if ($(this).val()=="Band name appendix") $(this).val("");
		}).blur(function() {
			if ($(this).val()=="") $(this).val("Band name appendix");
		});

	}
	
	/* VENUE PUBLISH FORM */
	if ($(".publish-form.venue").length) {
	
		$(".publish-form.venue form").submit(function() {
			// build entry title
			var title = $("input#venues-town").val() + ": " + $("input#venues-name").val();
			// set title
			$(".publish-form.venue input.title").val(title);
		});

	}
	

	/* ANY PUBLISH FORM */
	
	if ($(".publish-form").length) {
		// if there's a hash, go to that row...
		if (location.hash) {
			var hash = location.hash;
			if (hash.substr(1,1)=="g") {
				hash = hash.substr(1); // remove #
				var myRow = $("a.gig-anchor[name='" + hash + "']").parent().parent();
				var myBg = $(myRow).css("backgroundColor");
				$(myRow).css("backgroundColor", "#ffff99").animate({opacity:1}, 500).animate({ backgroundColor: myBg }, 1000);
			}
		}
		// form feedback
		$("p.delete-feedback").animate({opacity:1},2000).slideUp();
		// focus on first visible form field
		$(".publish-form input:visible, .publish-form select:visible").first().focus();
		
		// toggle publish form
		$("#publish-form-holder #tab").click(function() {
			if ($("#publish-form-holder").hasClass("open")) {
				$("#publish-form-holder").animate({bottom:'-104px'},400).removeClass("open");
			} else {
				$("#publish-form-holder").animate({bottom:'0'},400).addClass("open");
				// focus on first visible form field
				$(".publish-form input:visible, .publish-form select:visible").first().focus();
			}
		})
		// if there's a hash tag, open publish form...
		if (location.hash) {
			$("#publish-form-holder").addClass("open");
		}
	}

	/* album track highlighting */
	$("table.album-tracks tr").filter(":not(.headers)").filter(":even").addClass("odd");
	
	/* popup windows - just for audio player */
	$("a.popup").popupwindow();
	/* from popup player, open any links back in parent window */
	$("body#audio.popup div.album a").click(function() {
		self.opener.location = $(this).attr("href");
		self.opener.focus();
		window.close();
		return false;
	});

	/* LOAD AUDIO PLAYERS */
	$('div.audio-player').each(function() {
		var filename = $(this).attr("title");
		var my_player_id = $(this).attr("id");
		$(this).flash(
			{
				swf: '/scripts/audio-player.swf',
				height: 29,
				width: 29,
				wmode: 'transparent',
				flashvars: {
					file: filename,
					player_id: my_player_id
				}
			}
		);
		$(this).attr("title", "");
	});
	
});

/* 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);
		}
	);
}
