$(function() {
	// Include the explicit css setting for bgcolor and border color at the end of the animations because sometimes
	// The animate function doesn't finish its job properly and leaves random borders hanging around
	$('.cd_selection').hover(function(){
		$(this).addClass('cd_selection_hover');
	}, function() {
		$(this).removeClass('cd_selection_hover');
	});
	
	var expand_popout = function($show) {
		$show.addClass('expand');
		$show.fadeIn('medium', function() {
			$show.removeClass('hide');
			window.location.hash = "popouts";
		});
	};
	
	$('.cd_selection').click(function() {
		var show = '#' + 'tab_' + this.id;
		var $show = $(show);

		$('.instructions').fadeOut('slow');
		
		// Don't do any slideup/fade in if the same this is being clicked twice
		if(!$($show).hasClass('expand')) {
			// Close all lyric boxes
			$('li.close').click();

			// Slide up the previous CD and then expand the new one when done
			if($('.cd_popout.expand').length == 0) {
				expand_popout($show);
			}
			else {
				$('.cd_popout.expand').removeClass('expand').fadeOut('medium', function() {
					expand_popout($show);
				});
			}
		}
	});
	
	$('.icons > li').hover(function() {
		$(this).find('img').fadeIn('medium');
	}, function() {
		$(this).find('img').fadeOut('medium');
	});
	
	$('.track-line > .head').hover(function() {
		var $t = $(this);
		$t.addClass('head-hover');
		
		if($t.parent().hasClass('track-line-last')) {
			$t.addClass('head-last-hover');
		}
	}, function() {
		var $t = $(this);
		$t.removeClass('head-hover');

		if($t.parent().hasClass('track-line-last')) {
			$t.removeClass('head-last-hover');
		}
	});
	
	$('li.lyrics').click(function() {
		$t = $(this);
		$song_line = $t.parent().parent().parent().parent();
		$tracks = $song_line.parent();
		$title = $song_line.find('.title');
		
		$tracks.find('.track-line').not($song_line.get(0)).fadeOut('medium', function() {
			$song_line.find('li.lyrics').hide()
				.end().find('li.print, li.close').show()
				.end().find('div.songlyrics').slideDown('medium');
		});
	});
	
	$('li.close').click(function() {
		$t = $(this);
		$song_line = $t.parent().parent().parent().parent();
		$tracks = $song_line.parent();
		$title = $song_line.find('.title');
		
		$song_line.find('li.lyrics').show()
			.end().find('li.print, li.close').hide()
			.end().find('div.songlyrics').slideUp('medium', function() {
			$tracks.find('.track-line').not($song_line.get(0)).fadeIn('medium');
		});
	});
	
	$('.itunes_link').click(function() {
		$('#itunes_message').dialog('open');
		return false;
	});
	
	$('#itunes_message').dialog({
		title: 'iTunes', 
		autoOpen: false,
		draggable: false,
		modal: true,
		resizable: false
	});
});