// Programmed by joep@joep-i.nl
// Feel free to use and modify this.

	function Player() {
		this.playing = false;
		this.loaded = false;
		this.frequency = 100;
		this.song = '';
		this.slide = 0;
		this.hidden = 1;
		this.id = '';
		this.timeleft = 5;
		this.position = 0;
      	this.registerCallback();      
	}

	Player.prototype.sliding = function(position) {
		this.slide=1;
	    var duration =  sound.getDuration();

		var timeleft = (duration - (duration * position));
		var rest = (timeleft % 60000);
		var seconds = Math.round(rest/1000);
		var minutes = Math.round((timeleft - rest) / 60000);
		if (!(isNaN(minutes)) && !(isNaN(seconds))) {
			document.getElementById('handle1').innerHTML = sprintf("<span class='time'>%d:%02d</span>",minutes,seconds); 
		}
	}

	Player.prototype.change = function(position) {
		if (this.slide > 0) {
			sound.stop();
			this.slide = 0;
			this.setPosition(position);
		}
	}

    Player.prototype.registerCallback = function() {
   	   setInterval(this.onTimerEvent.bind(this), this.frequency);
    }

	Player.prototype.play = function(song,id) {
		if (this.playing) {
			if (this.song == song) {
				hideslider();
            	this.onSoundComplete();
			} else {
				hideslider();
				document.getElementById(this.id).className = 'silent'; 
				sound.start(0,1);
				sound.stop();
				sound.loadSound(song, true);
				this.id = id;
				this.song = song;
		  		document.getElementById(this.id).className = 'playing'; 
				this.playing = true;
			}
		} else {
			sound.loadSound(song, true);
			this.id = id;
			this.song = song;
			document.getElementById(this.id).className = 'playing'; 
			this.playing = true;
		}
		return false;
	}


   Player.prototype.setPosition = function(pos) {
   	var newpos = (this.duration*pos);
	sound.start(Math.round(newpos/1000),1);
   // var progress = newpos/this.duration;
   // slider1.setValue(progress);
   }

   Player.prototype.onTimerEvent = function() {
      if(this.playing) {
          var position = sound.getPosition();
          this.position = position;
          
          var duration =  sound.getDuration();
          this.duration = duration;

          var progress = position/duration;
/*		  if (sound.getBytesLoaded() == sound.getBytesTotal()) {
		  	showslider(this.id);
			this.hidden = 0; 
		  }
		  */
		  showslider(this.id);
		  this.hidden = 0; 
		  this.timeleft = duration - position;
          
		  var rest = (this.timeleft % 60000);
		  var seconds = Math.round(rest/1000);
		  var minutes = Math.round((this.timeleft - rest) / 60000);
		  if (seconds == 60) {
		    seconds = 0;
		  	minutes = minutes + 1;
		  }
		   if (!(isNaN(minutes)) && !(isNaN(seconds))) {
		      if (this.slide == 0) {
				  document.getElementById('handle1').innerHTML = sprintf("<span class='time'>%d:%02d</span>",minutes,seconds); 
			  }
			  if (!(this.timeleft > 0) && (this.duration > 0)) {
				this.onSoundComplete();
			  }
		  }
		  if (this.slide == 0) {
		  	if (isNaN(progress)) {
				progress = 0
			}
		  	slider1.setValue(progress);
		  }
      }
   }

   Player.prototype.onSoundComplete = function() {
			sound.start(0,1);
			sound.stop();
			hideslider();
			this.hidden = 1;
			document.getElementById(this.id).className = 'silent'; 
			this.song = '';
			this.playing = false;
			this.id = '';
   }

   function showslider(id) {
		listitem = document.getElementById(id);
		slideobj = document.getElementById('slider');
   		if (slideobj.style.visibility != 'visible') {
			slideobj.style.display = 'none';
			slideobj.style.visibility = 'visible';
		}
		slideobj.style.left = (findPosX(listitem)) + "px";
		slideobj.style.top = (findPosY(listitem) + 14) + "px";
		if (slideobj.style.display == 'none') {
			Effect.Appear('slider');
		}

   }

   function hideslider() {
   		slideobj = document.getElementById('slider')
		slideobj.style.visibility = 'hidden';
		slideobj.style.left = "0px";
		slideobj.style.top = "0px";
   }

	function findPosX(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
		curleft += obj.x;
		return curleft;
	}

	function findPosY(obj)
	{
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
		curtop += obj.y;
		return curtop;
	}




