
var SequentialPlayer = {};

(function() {
	var random_playing, trackList = [], sounds = [];
	
	function addButton( id, index, name ) {
		var img = document.getElementById( name + '_' + id );
		img.innerHTML = '<img id="button_' + name + '_' + id + '" title="' + name + '" src="control_' + name + '_blue.png" onclick="SequentialPlayer.buttonClick_' + name + '( ' + index + ' );" />';
	}
	
	function random_play( index ) {
		var id = trackList[ index ];
		document.getElementById( id ).style.backgroundColor = '#FFEEFF';
		addButton( id, index, 'skip', 'skip', 'skip' );
		addButton( id, index, 'repeat', 'repeat', 'repeat' );
		addButton( id, index, 'pause', 'pause', 'pause' );
		
		random_playing = sounds[ index ];
		sounds[ index ].play(); // unless we're still waiting for it...?
	};
	
	function random_stop( id ) {
		document.getElementById( id ).style.backgroundColor = '#ABA1A9';
		document.getElementById( 'skip_' + id ).innerHTML = '';
		document.getElementById( 'repeat_' + id ).innerHTML = '';
		document.getElementById( 'pause_' + id ).innerHTML = '';
	};
	
	function buttonClick( index, name, handler, score ) {
		var id = trackList[ index ], img = document.getElementById( 'button_' + name + '_' + id );
		img.src = 'control_' + name + '.png';
		handler( id );
		doAjaxSync( 'http://improvizone.org/improphone/_score.php', 'id=' + id + '&score=' + score );
		img.src = 'control_' + name + '_blue.png';
	}
	
	function onSoundLoad( index, detail, duration ) {
		detail.innerHTML = '(' + ( duration / 1000 ) + 's)';
		if ( index == 0 ) {
			random_play( 0 );
		}
	}

	SequentialPlayer.buttonClick_skip = function( index ) {
		buttonClick( index, 'skip', function( id ) {
			random_playing.stop(); 
			random_stop( id );
			random_play( index + 1 );
		}, 2.4 );
	};
		
	SequentialPlayer.buttonClick_repeat = function( index ) {
		buttonClick( index, 'repeat', function( id ) {
			random_playing.stop(); 
			random_playing.play();
		}, 4.8 );
	};
		
	SequentialPlayer.buttonClick_pause = function( index ) {
		var id = trackList[ index ], img = document.getElementById( 'button_pause_' + id );
		if ( img.title === 'pause' ) {
			random_playing.pause();
			img.src = 'control_play_blue.png';
			img.title = 'play';
		} else {
			random_playing.play();
			img.src = 'control_pause_blue.png';
			img.title = 'pause';
		}
	};
		
	SequentialPlayer.addTrack = function( t ) {
		trackList.push( t );
	};
		
	SequentialPlayer.load = function( index ) {
		if ( trackList.length > index ) {
			var id = trackList[ index ], row = document.getElementById( id ), detail = document.getElementById( 'detail_' + id );
			row.style.backgroundColor = '#948A92';
			detail.innerHTML = '<span style="text-decoration:blink;">Loading...</span>';
			
			sounds[ index ] = soundManager.createSound( { 
				id: 'track_' + id, url: 'http://improvizone.com/_audio.mp3?id=' + id + '&client=b', autoLoad: true, multishot: false,
				onload: function() { onSoundLoad( index, detail, this.duration ); },
				onplay: function() { SequentialPlayer.load( index + 1 ); },
				onfinish: function() { random_stop( id ); random_play( index + 1 ); }
			} );
		}
	};

})();

function startPlaying( allTracks ) {
	for ( var n in allTracks.childNodes ) {
		var node = allTracks.childNodes[ n ];
		if ( node.id ) {
			SequentialPlayer.addTrack( node.id );
		}
	}
	SequentialPlayer.load( 0 );
}

soundManager.debugMode = false; 
soundManager.url = '../player/';

soundManager.onload = function() {
	startPlaying( document.getElementById( 'allTracks' ) );
};
