$(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');
	});

	
	$('.icons > li').hover(function() {
		var $msg = $(this).closest('.right').find('.icon-msg');
		var txt = '';
		$t = $(this);
		
		if($t.hasClass('play')) {
			txt = 'Listen';	
		}
		else if($t.hasClass('lyrics')) {
			txt = 'Lyrics';	
		}
		else if($t.hasClass('print')) {
			txt = 'Print';	
		}
		else if($t.hasClass('close')) {
			txt = 'Close';	
		}
		
		$msg.hide();
		$msg.text(txt);
		$msg.fadeIn('fast');
		$(this).find('img').fadeIn('medium');
	}, function() {
		var $msg = $(this).closest('.right').find('.icon-msg');
		$msg.fadeOut('fast');
		$(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
	});
	
	$('.cdbaby_link').click(function() {
		$('#cdbaby_message').dialog('open');
		return false;
	});
	
	$('#cdbaby_message').dialog({
		title: 'cdBaby', 
		autoOpen: false,
		draggable: false,
		modal: true,
		resizable: false
	});
	
	// Allow browsing back button and changing hash (xyz.html#hash) to show appropriate CD's
	$(window).bind('hashchange', function() {
		var hash = window.location.hash;
		
		ok_hash = {
			'#cd1': true, '#cd2': true, '#cd3': true, '#cd4': true
		};
		
		if(typeof(ok_hash[hash]) != 'undefined') {
			var show = '#tab_' + hash.substr(1, hash.length - 1);
			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, this.id);
				}
				else {
					$('.cd_popout.expand').removeClass('expand').fadeOut('medium', function() {
						expand_popout($show, this.id);
					});
				}
			}
		}
		else {
			$('.instructions').show();
			$('.cd_popout.expand').removeClass('expand').fadeOut('medium');
			$('li.close').click();
		}
	});
	

	var expand_popout = function($show, new_hash) {
		$show.addClass('expand');
		$show.fadeIn('medium', function() {
			$show.removeClass('hide');
		});
	};

	var cd_select = function() {};
	
	$('.cd_selection').click(function() {
		var new_hash = this.id;
		window.location.hash = new_hash;

		if($.browser.version.match(/^[67]\./) && $.browser.msie) {
			window.location.reload();
		}
	});

	$(window).trigger('hashchange');
});
