function Player() {
	this.obj = document.getElementById(PLAYER_ID);
	this.loop = LOOP_DEFAULT;
	this.play = function () {
		this.obj.TCallLabel('/','play');
	}
	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	}
	this.pause = function () {
		this.obj.TCallLabel('/','pause');
	}
	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	}
	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	}
	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	}
	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	}
	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');
		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;
		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;
		return ps;
	}
	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		return this.obj.GetVariable('playingState');
	}
	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	}
	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');
		this.obj.SetVariable(eventName, action);
	}	
	this.next_tune = function (flash) {
		if (this.loop == 2 && flash) {
			this.stop();
			this.play();
		} else {
			var old_selected_tune = playlist.selected_tune;
			var old_selected_pl_mix = playlist.get_mix(playlist.selected_tune.id_mix);
			if (old_selected_pl_mix["tunes"].length > playlist.selected_tune.position+1) {
				var next_mix = old_selected_pl_mix["mix"];
				var next_position = playlist.selected_tune.position+1;
				var next_tune = old_selected_pl_mix["tunes"][next_position];
			} else {
				for (i=0; i<playlist.mixes.length; i++) {
					if (playlist.mixes[i] == old_selected_pl_mix) {
						old_selected_pl_mix_position = i;
						break;
					}
				}
				if (old_selected_pl_mix_position < (playlist.mixes.length-1)) {
					var new_position = old_selected_pl_mix_position+1;
					var next_mix = playlist.mixes[new_position]["mix"];
					var next_position = 0;
					var next_tune = playlist.mixes[new_position]["tunes"][0];
				} else if (this.loop == 1 || flash != 1) {
					var next_position = 0;
					var next_mix = playlist.mixes[0]["mix"];
					var next_tune = playlist.mixes[0]["tunes"][0];
				} else return;
			}
			playlist.select(next_mix, next_position);
			playlist.selected_tune = next_tune;
		}
	}
	this.prev_tune = function () {
		var old_selected_tune = playlist.selected_tune;
		var old_selected_pl_mix = playlist.get_mix(playlist.selected_tune.id_mix);
		if (playlist.selected_tune.position > 0) {
			var prev_mix = old_selected_pl_mix["mix"];
			var prev_position = playlist.selected_tune.position-1;
			var prev_tune = old_selected_pl_mix["tunes"][prev_position];
		} else {
			for (i=0; i<playlist.mixes.length; i++) {
				if (playlist.mixes[i] == old_selected_pl_mix) {
					old_selected_pl_mix_position = i;
					break;
				}
			}
			if (old_selected_pl_mix_position > 0) {
				var new_position = old_selected_pl_mix_position-1;
				var prev_mix = playlist.mixes[new_position]["mix"];
				var prev_position = playlist.mixes[new_position]["tunes"].length-1;
				var prev_tune = playlist.mixes[new_position]["tunes"][prev_position];
			} else {
				var lastMixNumber = playlist.mixes.length-1;
				var prev_mix = playlist.mixes[lastMixNumber]["mix"];
				var prev_position = prev_mix.tracks.length-1
				var prev_tune = playlist.mixes[lastMixNumber]["tunes"][prev_position];
			}
		}
		playlist.select(prev_mix, prev_position);
		playlist.selected_tune = prev_tune;
	}
	
	this.loop_change =function(){
		this.loop=(this.loop+1) % 3;
		switch(this.loop){
			case 0 : new_loop_text="None";break;
			case 1 : new_loop_text="All";break;
			case 2 : new_loop_text="Mix";break;
		}
		document.getElementById("loop_text").innerHTML=new_loop_text;
	}
	
}

