(function($) {

	// IE 6
	if(!('console' in window)) {
		window.console = {
			log: function() {}
		};
	}

	var cue = null,
		originalCursor,
		originalPosition,
		setPosition = function(left) {
			left = left || parseInt($('#slider').css('left'), 10);
			try {
				cue.setPosition(Math.round(left / $('#track').width() * cue.duration));
			} catch(err) {
				// 
			}
		},
		mousemove = function(e) {
			e.preventDefault();
			e.stopPropagation();
			var width = $('#track').width(),
				left = Math.max(Math.min((e.screenX - originalCursor + originalPosition), width), 0),
				time = new Date;

			time.setTime(left / width * cue.duration);
			var seconds = time.getSeconds().toString(),
				pad = (seconds.length == 1 ? '0' : '');
			$('#time').html(time.getMinutes() + ':' + pad + seconds);

			$('#slider').css({
				left: left + 'px'
			});
		},
		mouseup = function(e) {
			e.preventDefault();
			e.stopPropagation();
			setPosition();
			$(document).unbind('mousemove', mousemove);
			$(document).unbind('mouseup', mouseup);
			$('#slider').removeClass('dragging');
		};

	$('#slider').mousedown(function(e) {
		e.preventDefault();
		e.stopPropagation();
		if(cue === null || !('destruct' in cue)) {
			return;
		}
		originalCursor = e.screenX;
		originalPosition = parseInt($('#slider').css('left'), 10);
		$('#slider').addClass('dragging');
		$(document).mousemove(mousemove);
		$(document).mouseup(mouseup);
	}).hover(
		function() {
			if(cue === null || !('destruct' in cue)) {
				return;
			}
			$('#time').show();
		},
		function() {
			$('#time').css('display', '');
		}
	);

	$('#track').click(function(e) {
		if(cue === null || !('destruct' in cue)) {
			return;
		}
		var left = (e.screenX - $('#track').offset().left);
		$('#slider').css({
			left: left + 'px'
		});
		setPosition(left);
	});

	soundManager.url = '/etc/';
	soundManager.debugMode = false;
	soundManager.consoleOnly = true;

	var songId = 0,
		songs = {},
		soundManagerReady = false;
	$('#playlist a').each(function(i, link) {
		$(link).attr('id', 'song-' + songId);
		songs['song-' + songId] = $(link).attr('href');
		songId++;
	});
	soundManager.onready(function(status) {
		if(status.success) {
			soundManagerReady = true;
			if(cue !== null) {
				cue.click();
			}
		}
	});

	$('#playlist a').live('click', function(e) {
		e.preventDefault();
		e.stopPropagation();
		var playing = $('#playing'),
			link = $(this),
			listItem = link.parents('li:first');

		if(cue !== null && ('destruct' in cue) && playing[0] == listItem[0]) {
			return;
		} else {
			playing.removeAttr('id');
			listItem.attr('id', 'playing');
		}

		if(!soundManagerReady) {
			cue = link;
		} else {
			if(cue) {
				try {
					cue.destruct();
				} catch(err) {
					// Edge case: cue was set before soundManagerReady
				}
			}

			cue = soundManager.createSound({
				id: link.attr('id'),
				url: link.attr('href'),
				whileplaying: function() {
					if($('#slider').hasClass('dragging')) {
						return;
					}
					$('#slider').css({
						left: Math.floor(this.position / this.duration * $('#track').width()) + 'px'
					});
					var time = new Date;
					time.setTime(this.position);
					var seconds = time.getSeconds().toString(),
						pad = (seconds.length == 1 ? '0' : '');
					$('#time').html(time.getMinutes() + ':' + pad + seconds);
				},
				onjustbeforefinish: function() {
					if(cue.bytesLoaded === null || cue.bytesTotal === null || cue.bytesLoaded < cue.bytesTotal) {
						return;
					}
					var playing = $('#playing a');
					if(playing[0] == $('#playlist a:last')[0]) {
						this.destruct();
						cue = null;
						$('#slider').css({
							left: ''
						});
						$('#playing').removeAttr('id');
					} else {
						var items = $('#playlist ol li'),
							index = items.index(playing.parents('li:first')[0]);
						$(items[index + 1]).find('a').click();
					}
				}
			});
			cue.play();
		}
	});

	$('#play').click(function(e) {
		if(cue && ('paused' in cue)) {
			if(cue.paused) {
				cue.resume();
			} else {
				cue.pause();
			}
		} else {
			$('#playlist a:first').click();
		}
	});

	var previousShowsHeading = $('#show-list h3'),
		previousShowsParent = previousShowsHeading.parents('li:first'),
		previousShows = previousShowsParent.nextAll('li');

	if(previousShowsParent.prevAll('li').length > 0) {
		previousShowsHeading.addClass('clickable');
		previousShows.hide();
	}
	previousShowsHeading.click(function(e) {
		previousShows.show();
		previousShowsParent.hide();
	});

	// $('#playlist ol a:first').click();

})(jQuery);
