function Player ()
{
	this.paused = true;
	this.stopped = true;
	this.sound = new Sound();
	this.position = 0;
	this.frequency = 1000;
	this.isLoaded = false;
	this.duration = 0;
	this.bytesTotal = 0;
	this.playerWidth = 93;
	this.registerCallback();
}

Player.prototype.onTimerEvent = function()
{
	var isDurationOk = false;
	if(!this.paused)
	{
		var position = this.sound.getPosition();
		if(!position)
			position = 0;

		this.position = position;
		
		var duration = 0;
		
		// Let's trust the duration given in the playlist
		duration = this.sound.getDuration();
		if(!duration)
			duration = 0;
		if(duration == this.duration && duration != 0)
			isDurationOk = true;

		this.duration = duration;
		var progress = position/duration;
		if(isDurationOk)
			this.setProgressBar(progress);

		var isBytesTotalOk = false;
		var bytesTotal = this.sound.getBytesTotal();
		if(bytesTotal == this.bytesTotal)
			isBytesTotalOk = true;

		this.bytesTotal = bytesTotal;
		if(isBytesTotalOk)
		{
			var loaded = this.sound.getBytesLoaded()/bytesTotal;
			this.setLoadedBar(loaded);
		}
		if (progress >= 1 && duration != 0 && position != 0)
			this.onSoundComplete();
	}
}

Player.prototype.setProgressBar = function(progress)
{
	if(!progress)
		progress = 0;
	$('click_sound_panel_played').style.width = progress * this.playerWidth + 'px';
}

Player.prototype.setLoadedBar = function(loaded)
{
	if(!loaded)
		loaded = 0;
	$('click_sound_panel_loaded').style.width = loaded * this.playerWidth + 'px';
}

Player.prototype.registerCallback = function()
{
	setInterval(this.onTimerEvent.bind(this), this.frequency);
}

Player.prototype.onPlayButtonClick = function()
{
	if(this.paused)
	{
		$('click_sound_panel_play').style.display ='none';
		$('click_sound_panel_pause').style.display ='';
		this.paused = false;
		if(this.stopped)
		{
			this.position=0;
		}
		this.sound.start(this.position/1000, 1);
		this.stopped = false;
	}
	else
	{
		$('click_sound_panel_play').style.display ='';
		$('click_sound_panel_pause').style.display ='none';
		this.position = this.sound.getPosition();
		this.sound.stop();
		this.paused = true;
	}
}

Player.prototype.onStopButtonClick = function()
{
	this.paused = true;
	this.stopped = true;
	this.position = 0;
	this.duration = 0;
	this.sound.start(this.duration/1000, 1);
	this.sound.stop();
	this.setProgressBar(0);
	$('click_sound_panel_play').style.display ='';
	$('click_sound_panel_pause').style.display ='none';
}

Player.prototype.onSoundComplete = function()
{
	if(!this.paused)
	{
		this.onStopButtonClick();
	}
}