var currentSound = "";
var sliding = false;

function formattedTime(ms) {
	hours   = Math.floor(ms/1000/60/60);
	minutes = Math.floor(ms/1000/60   ) % 60;
	seconds = Math.floor(ms/1000      ) % 60;
	function leadingZero(n) {
		return (n < 10 ? '0' : '') + n
	};
	return leadingZero(hours) + ':'
		+ leadingZero(minutes) + ':'
		+ leadingZero(seconds);
};

$(document).ready(function(){
	soundManager.url = '/assets/swf/soundmanager2.swf';
	soundManager.debugMode = false;
	soundManager.defaultOptions.volume = 33;

	soundManager.beginDelayedInit();
	$(window).unload(soundManager.destruct);

	soundManager.onload = function() {
		// For each link to an MP3
		$("a[href$='mp3']").each(function() {
			// Register the audio file with soundManager
			anchor = $(this);
			soundurl = anchor.attr('href');
			soundManager.createSound({
				id: soundurl,
				url: soundurl,
				slider: null,
				autoLoad: false,
				whileplaying: function() {
					if (!sliding) {
						this.slider.sliderInstance().moveTo(this.position);
						this.slider.parent().children('.audioTiming')
						.text(
							formattedTime(this.position) + ' / '
								+ formattedTime(this.duration)
						);
					}
				},

				onload: function() {
					this.slider = 
						$('div.audioPlayer[ref="'+this.sID+'"] .audioProgress')
					.removeClass('audioProgressLoading')
					.children('.audioPoint').show().end()
					.slider({
						minValue: 0,
						maxValue: this.duration,

						change: function(e, ui) {
							soundManager.getSoundById($(this).attr('ref'))
							.setPosition(ui.slider.curValue);
						},

						slide: function(e, ui)
						{
							$(this).parent().children('.audioTiming')
							.text(
								formattedTime(ui.slider.curValue) + ' / '
									+ formattedTime(ui.slider.options.maxValue)
							);
						},

						start: function(e, ui) { sliding = true; },

						stop: function(e, ui) { sliding = false; }
					});
				},

				onfinish: function()
				{
					$(this.slider).parent()
					.children('.audioPlay').show().end()
					.children('.audioPause, .audioProgress, .audioTiming')
					.hide().end()
					.removeClass('audioPlayerActive');
					this.setPosition(0);
				}
			});

			// Follow the link with a title and a set of controls
			controls =
				$('<div class="audioPlayer" ref="'+soundurl+'">'
				  + ' <div class="audioPlay" ref="'+soundurl+'"></div>'
				  + ' <div class="audioPause" ref="'+soundurl+'"></div>'
				  + ' <div class="audioProgress audioProgressLoading'
				  + '             ui-slider"'
				  + '      ref="'+soundurl+'">'
				  + '   <div class="audioPoint ui-slider-handle"></div>'
				  + ' </div>'
				  + ' <div class="audioTiming">Loading</div>'
				  + '</div>'
				 );

			controls.insertAfter(anchor);
		});
		
		// If a main play button is pushed
		$('div.audioPlay').click(function(){
			// Stop the playing sound if any, and start new sound
			if (currentSound != "") {
				soundManager.stop(currentSound);
				$('.audioPaused').click();
			}
			currentSound = $(this).attr('ref');
			soundManager.play(currentSound);

			// Hide play button, show pause/play, progress and timing,
			// and adjust player background
			$('.audioPlayer[ref="'+currentSound+'"]')
			.children('.audioPlay').hide().end()
			.children('.audioPause, .audioProgress, .audioTiming')
			.show().end()
			.addClass('audioPlayerActive');

			// Reverse of above commands for all other players
			$('.audioPlayer[ref!="'+currentSound+'"]')
			.children('.audioPlay').show().end()
			.children('.audioPause, .audioProgress, .audioTiming')
			.hide().end()
			.removeClass('audioPlayerActive');
		});

		// If a pause/play button is pushed
		// Toggle between paused and playing states
		$('div.audioPause').toggle(
			function(){
				soundManager.pause(currentSound);
				$(this).addClass('audioPaused');
			}, function(){
				soundManager.resume(currentSound);
				$(this).removeClass('audioPaused');
			}
		);
	};
});
