function Player () {
	this.autoPlay = false;
	this.paused = false;
	this.stopped = true;
	this.sound = new Sound();
	this.currentTrack = -1;
	this.currentTrackLength = '';
	this.currPage = 0;
	this.tracksPerPage = 16;
	this.position = 0;
	this.frequency = 1000;
	this.isLoaded = false;
	this.duration = 0;
	this.bytesTotal = 0;
	this.playerWidth = 328;
	this.registerCallback();
}

Player.prototype.onTimerEvent = function() {
	var isDurationOk = false;
	if(!this.paused) {
		var position = this.sound.getPosition();
		if(!position) position = 0;
		if(position != this.position && position != 0) {
			this.onPlaying();
			this.setTimer();
		}
		else {
			this.onBuffering();
		}
		this.position = position;
		
		var duration = 0;
		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 && (this.sound.getBytesLoaded() < 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;
	$('t'+this.currentTrack+'-progress').style.width = progress * this.playerWidth + 'px';
}

Player.prototype.setLoadedBar = function(loaded) {
	if(!loaded) loaded = 0;
	//$('t'+this.currentTrack+'-clock').innerHTML = Math.round(loaded * 100) + "%";
	$('t'+this.currentTrack+'-loaded').style.width = loaded * this.playerWidth + 'px';
	
}

Player.prototype.setTimer = function() {
	seconds = Math.floor(this.position / 1000);
	minutes = Math.floor(seconds / 60);
	seconds %= 60;
	seconds = ((seconds < 10) ? "0" + seconds : seconds);
	time_holder = minutes + ":" + seconds;
	$('t'+this.currentTrack+'-clock').innerHTML = time_holder;
}

Player.prototype.resetTimer = function() {
	this.position = 0;
	this.duration = 0;
	if(this.currentTrack > -1) {
		$('t'+this.currentTrack+'-clock').innerHTML = this.currentTrackDuration;
		this.setProgressBar(0);
		this.setLoadedBar(0);	
	}
}

Player.prototype.resetAction = function() {
	if(this.currentTrack > -1) {
		$('t'+this.currentTrack).removeClassName('playing');
		$('t'+this.currentTrack+'-action').innerHTML = "play";
		$('t'+this.currentTrack+'-action').removeClassName('pause');
		$('t'+this.currentTrack+'-action').addClassName('play');
	}
}

Player.prototype.onPlaying = function() {
	//$('display').innerHTML = "playing...";
	$('t'+this.currentTrack).addClassName('playing');
	$('t'+this.currentTrack+'-action').innerHTML = "pause";
	$('t'+this.currentTrack+'-action').removeClassName('play');
	$('t'+this.currentTrack+'-action').addClassName('pause');
}

Player.prototype.onPause = function() {
	//$('display').innerHTML = "paused"; 
	$('t'+this.currentTrack+'-action').innerHTML = "play";
	$('t'+this.currentTrack+'-action').removeClassName('pause');
	$('t'+this.currentTrack+'-action').addClassName('play');
}   

Player.prototype.onBuffering = function() {
	//$('display').innerHTML = "buffering...";  
}

Player.prototype.registerCallback = function() {
	setInterval(this.onTimerEvent.bind(this), this.frequency);
}

Player.prototype.onPlayButtonClick = function() {
	if(this.paused) {
		this.paused = false;
		if(this.stopped) {
			this.sound.loadSound('/music/' + this.playlist[this.currentTrack].filename + '.mp3', true);
		}
		this.sound.start(this.position/1000, 1);
		this.stopped = false;
		}
	else {
		this.position = this.sound.getPosition();
		this.sound.stop();         
		this.paused = true;
		this.onPause();
	}
}

Player.prototype.onForwardButtonClick = function() {
	this.sound.start(this.duration/1000, 1);
	this.sound.stop();
	this.resetAction();
	this.resetTimer();
	this.stopped = true;
	nextTrack = this.currentTrack + 1;
	if(nextTrack >= this.playlist.length) nextTrack = 0;
	if(!this.paused) {
		this.paused = true;
		this.playTrack(nextTrack);
	}
}   

Player.prototype.onBackButtonClick = function() {
	this.sound.start(this.duration/1000, 1);
	this.sound.stop();
	this.resetAction();
	this.resetTimer();
	this.stopped = true;
	prevTrack = this.currentTrack - 1;
	if(prevTrack < 0) prevTrackTrack = this.playlist.length - 1;
	if(!this.paused) {
		this.paused = true;
		this.playTrack(prevTrack);
	}
}      

Player.prototype.onStopButtonClick = function() {
	this.sound.start(this.duration/1000, 1);
	this.sound.stop();
	this.resetAction();
	this.resetTimer();
	this.stopped = true;
	this.paused = true;
}

Player.prototype.loadPlaylist = function(playlist) {
	this.playlist = playlist;
	if(this.autoPlay) {
		function autoPlayTrack() {
			if($('__sound_flash__').visible()) {
				pe.stop();
				var num;
				if(player.tracksPerPage < playlist.length) num = player.tracksPerPage;
				else num = playlist.length;
				randomTrack = Math.floor(Math.random()*num);
				player.playTrack(randomTrack);
			}
		}
		var pe = new PeriodicalExecuter(autoPlayTrack, 1);
	}
}

Player.prototype.onSoundComplete = function() {
	if(!this.paused) {
		this.onForwardButtonClick();
	}
}

Player.prototype.loadTrack = function(track) {
	if(this.currentTrack != track) {
		this.sound.start(this.duration/1000, 1);
		this.sound.stop();
		this.resetAction();
		this.resetTimer();
		this.stopped = true;
		this.paused = true;
		this.playTrack(track);
	}
	else if(this.paused) {
		this.paused = false;
		if(this.stopped) {
			this.sound.loadSound('/music/' + this.playlist[track].filename + '.mp3', true);
		}
		this.sound.start(this.position/1000, 1);
		this.stopped = false;
	}
	else {
		this.position = this.sound.getPosition();
		this.sound.stop();         
		this.paused = true;
		this.onPause();
	}
}

Player.prototype.changePosition = function(event) {
	mouseX = Event.pointerX(event);
			mouseY = Event.pointerY(event);
			mouseC = "Mouse:  X: " + mouseX + "px Y: " + mouseY + "px" + "\n";
			loadedXY = $('t'+player.currentTrack+'-loaded').cumulativeOffset();
			loadedC = "Loaded Bar:  X: " + loadedXY[0] + "px Y: " + loadedXY[1] + "px" + "\n";
			trackPosChange = mouseX - loadedXY[0];
			trackPosAmount = trackPosChange / $('t'+player.currentTrack+'-loaded').getWidth();
			newTrackPos = ((player.duration / 1000) * trackPosAmount).floor();
			player.sound.start(newTrackPos, 1);
			//alert(mouseC + loadedC + "Raw Track Position Change: " + trackPosChange + "\nTrack Duration: " + player.duration/1000 + "\nNew Track Position: " + newTrackPos + "\nNew Track Position (Rounded): " + (newTrackPos).floor());
}

Player.prototype.playTrack = function(track) {
		//this.sound.setVolume(0);
		this.sound.start(this.duration/1000, 1);
		this.sound.stop();
		this.resetAction();
		this.resetTimer();
		if(this.currentTrack > -1) Event.stopObserving('t'+this.currentTrack+'-bar', 'click', this.changePosition);
		this.stopped = true;
		this.sound.loadSound('/music/' + this.playlist[track].filename + '.mp3', true);
		this.sound.start(this.position/1000, 1);
		this.currentTrack = track;
		this.currentTrackDuration = $('t'+this.currentTrack+'-clock').innerHTML;
		Event.observe('t'+this.currentTrack+'-bar', 'click', this.changePosition);
		this.paused = false;
		this.stopped = false;
}
