/* Js Player v1.0 (C) www.kalelmedia.com */

var player = {
	
	Songs: {},			//Array to hold all songs
	_inSong: 0,			// Var so see if we are playing a song
	_current: 0,		// Var to identify current song
	_mute: 0,			// Are we muted yes or no?
	_letsPlay: 0,		// If everything is cool this will eq 1
	_trackTextbox: $('songName'),			// Text box to hold name of track
	
	
	_makeCurrent: function(name){
		var id = player.Songs.indexOf(name);
		player._current = id;
	},
	
	internalPlay: function(name){
		if(player.Songs.length < 1){
			player._letsPlay = 0;
			throw('There are no songs to play, you must use the player.addTrack() function before calling player.play();');
		} else {
			if(player._mute){
				if(confirm("You are in mute mode but you clicked the play button, would you like to unmute and play the song?")){
						player.unmute();
				} else { 
					alert('Song could not be played.');
					exit;
				}
			}
			
			Sound.play(player.Songs[player._current]);
			
			//debug
				alert(player.Songs.toSource());
				
			player._inSong = 1;
		}
	},
	
	play: function(){
		if(!player._inSong){
			player.internalPlay(player.Songs.first());
		} else {
			player.next();
		}
	},
	
	pause: function(){
		if(!player._inSong){
			alert('There is no song playing, so you cant pause it.');
		} else {
			Sound.disable();
		}
	},
	
	next: function(){
		if(!player._inSong){
			player.play();
		} else {
			if(!player._current + 1 > player.Songs.length){
				player.internalPlay(player.Songs[player._current + 1]);
			} else {
				alert('Last Track, you can only go backwards from here.');
			}
		}
	},
	
	previous: function(){
		if(!player._inSong){
			player.play();
		} else {
			if(!player._current - 1 < 0){
				player.internalPlay(player.Songs[player._current - 1]);
			} else {
				alert('First Track, you can only go forward from here');
			}
		}
	},
	
	mute: function(){
		if(player._mute){
			player._mute = 0;
			Sounds.enable();
		} else {
			player._mute = 1;
			Sounds.disable();
		}
	},
	
	addTrack: function(track){
		if(!player._letsPlay){
			throw "Sorry, you must load player before you can add tracks";
		} else {
			player.Songs.push(track);
		}
	},
	
	load: function(){
		if((typeof Prototype == "Undefined") || (typeof Scriptaculous == "Undefined")){
			throw "Sorry, You need both Prototype & Script.ac.ulous to use this player.";
		} else {
			player._letsPlay = 1;
			player.Songs = new Array(); 
		}
	}
}
