/* ###############################################################################*/
/* MP3 player class
/* (c) 2007 Joris van Montfort <joris@.loft.nu>
/* ###############################################################################*/
//=================================================================================================
// Mp3 player singleton class
Mp3Player = new Object();

// State of the currrent song
Mp3Player.STOPPED = 1;
Mp3Player.LOADING = 2;
Mp3Player.PLAYING = 3;
Mp3Player.ERROR = 4;

// Title labels
Mp3Player.titlePlay = 'Afspelen';
Mp3Player.titleStop = 'Stoppen';
Mp3Player.titleLoading = 'Bezig met laden, klik om te stoppen.';
Mp3Player.titleError = 'Probleem met laden van bestand.'

// Other vars
Mp3Player.pathToRoot = '';
Mp3Player.swfFile = 'scripts/mediaplayer/sample-player.swf';
Mp3Player.playlists = new Array();
Mp3Player.currentPlaylist = 0;
Mp3Player.currentSongIndex = -1;
Mp3Player.currentSongId = null;
Mp3Player.currentState = Mp3Player.STOPPED;
//=================================================================================================
Mp3Player.registerPlaylist = function (playlistDivName){
	var playlist = new Array();
	var subDivs = document.getElementById(playlistDivName).getElementsByTagName('div');
	var playlistIndex = Mp3Player.playlists.length;

	// Build playlist
	for (var i=0; i < subDivs.length; i++){
		if (subDivs[i].id.match('playlistSong')){
			// Mouse behaviour on songs
			subDivs[i].onmouseover = function(){this.className='playlistSong-hover';};
			subDivs[i].onmouseout = function(){this.className='playlistSong';};

			var songLink = subDivs[i].getElementsByTagName('a')[0];
			var songId = parseInt(songLink.id.replace('songlink', ''));

			// Add to playlist
			playlist.push(songId);

			// Set title
			songLink.title = this.titlePlay;

			// Setup actions
			songLink.onclick = function(){return Mp3Player.playMp3(parseInt(this.id.replace('songlink', '')), playlistIndex);};
			//songLink.onclick = function(){return Mp3Player.playMp3(songId, playlistIndex);};
			songLink.onfocus = function(){this.blur()};
		}
	}
	// Add playlist to player
	this.playlists.push(playlist);
}
//=================================================================================================
Mp3Player.playMp3 = function (songId, playlistId){
	// Stop anything that is already playing
	this.stopCurrentMp3();

	// Set playlist
	this.currentPlaylist = playlistId;
	this.currentSongIndex = this.getSongIndex(songId);

	// Set new songId
	this.currentSongId = songId;

	// Update display
	this.setDisplay(songId, this.LOADING);
	// Attach swf
	var url = document.getElementById('songlink'+songId).href;
	flashVars = ["songUrl="+url];
	var HTML = [
		'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"',
		'		codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
		'		WIDTH="0"',
		'		HEIGHT="0"',
		'       id="Mp3PlayersSwf">',
		'   <PARAM NAME="allowScriptAccess" value="always">',
		'	<PARAM NAME="movie" value="'+this.pathToRoot+this.swfFile+'">',
		'	<PARAM NAME="swLiveConnect" value="true" >',
		'	<PARAM NAME=quality VALUE=high>',
		'	<PARAM NAME=menu VALUE=false>',
		' <PARAM NAME="FlashVars" VALUE="' + flashVars + '">',
		'<EMBED src="'+this.pathToRoot+this.swfFile+'"',
		'       quality=high',
		'       WIDTH="0"',
		'       HEIGHT="0"',
		'		allowScriptAccess="always"',
		'		swLiveConnect="true"',
		'       MENU="false"',
		'       NAME="Mp3PlayersSwf" ALIGN="" TYPE="application/x-shockwave-flash"',
		'       PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"',
		'       FlashVars="' + flashVars + '">',
		'</EMBED>',
		'</OBJECT>'
	].join("");
	this.getAudioDiv().innerHTML = HTML;
	return false;
}
//=================================================================================================
Mp3Player.stopCurrentMp3 = function (){
	if (this.currentSongId != null){
		this.stopMp3(Mp3Player.currentSongId);
	}
	return false;
}
//=================================================================================================
Mp3Player.stopMp3 = function(songId){
	// Stop audio
	this.getAudioDiv().innerHTML = "";
	// Update display
	this.setDisplay(songId, this.STOPPED);
}
//=================================================================================================
Mp3Player.playNextSong = function(){
	if(this.playlists[this.currentPlaylist].length > 0){
		var nextIndex = this.currentSongIndex+1;
		var songId = this.playlists[this.currentPlaylist][nextIndex];
		if (songId){
			this.currentSongIndex = nextIndex;
			this.playMp3(songId, this.currentPlaylist);
		}else{
			this.currentSongIndex = -1;
			this.stopCurrentMp3();
		}
	}else{
		this.stopCurrentMp3();
	}
}
//=================================================================================================
Mp3Player.onSoundLoaded = function(succes){
	if (succes){
		this.setDisplay(this.currentSongId, this.PLAYING);
	}else{
		this.setDisplay(this.currentSongId, this.ERROR);
	}
}
//=================================================================================================
Mp3Player.getAudioDiv = function (){
	var div = document.getElementById('audioDiv');
	if(div == null){
		div = document.createElement('DIV');
		div.id = "audioDiv";
		document.documentElement.appendChild(div);
	}
	return div;
}
//=================================================================================================
Mp3Player.setDisplay = function(songId, state){
	Mp3Player.currentState = state;
	var linkDiv = document.getElementById('songlink'+songId);

	switch (state){
		case this.LOADING:
			// Show stop button and change onclick event
			linkDiv.className = 'loadingbutton';
			linkDiv.onclick = function(){return Mp3Player.stopCurrentMp3()};

			// Set title
			linkDiv.title = this.titleLoading;

			// Mark the song that is playing and disable onmouse over and onmouseout
			var playlistSongDiv = document.getElementById('playlistSong'+songId);
			playlistSongDiv.className = 'playlistSong-hover';
			playlistSongDiv.onmouseover = '';
			playlistSongDiv.onmouseout = '';
		break;
		case this.PLAYING:
			linkDiv.className = 'stopbutton';
			linkDiv.onclick = function(){return Mp3Player.stopCurrentMp3()};
			// Set title
			linkDiv.title = this.titleStop;

			// Mark the song that is playing and disable onmouse over and onmouseout
			var playlistSongDiv = document.getElementById('playlistSong'+songId);
			playlistSongDiv.className = 'playlistSong-hover';
			playlistSongDiv.onmouseover = '';
			playlistSongDiv.onmouseout = '';
		break;
		case this.ERROR:
			linkDiv.className = 'errorbutton';
			// Set title
			linkDiv.title = this.titleError;
		break;
		case this.STOPPED:
			// Restore playbutton and behaviour
			linkDiv.className = 'playbutton';
			linkDiv.onclick = function(){return Mp3Player.playMp3(songId, Mp3Player.getPlaylistIndex(songId));};

			// Set title
			linkDiv.title = this.titlePlay;

			// Restore hover behaviour on playlist item
			var playlistSongDiv = document.getElementById('playlistSong'+songId);
			playlistSongDiv.className = 'playlistSong';
			playlistSongDiv.onmouseover = function(){playlistSongDiv.className='playlistSong-hover'};
			playlistSongDiv.onmouseout = function(){playlistSongDiv.className='playlistSong'};
		break;
	}
}
//=================================================================================================
// Returns the playlist a song is in or flase on failure
Mp3Player.getPlaylistIndex = function(songId){
	var playlistId = false;
	for (var i=0; i < this.playlists.length; i++){
		for (var j=0; j < this.playlists[i].length; j++){
			if (this.playlists[i][j] == songId){
				playlistId = i;
				break;
			}
		}
	}
	return playlistId;
}
//=================================================================================================
// Returns the index of a song within the (current) playlist or flase on failure
Mp3Player.getSongIndex = function(songId){
	var index = false;
	for (var i=0; i < this.playlists[this.currentPlaylist].length; i++){
		if (this.playlists[this.currentPlaylist][i] == songId){
			index = i;
			break;
		}
	}
	return index;
}
