/*
jQuery.fn.extend({
	soundrecorder: function(){
		return true;
	}
});
*/

jQuery.soundrecorder = jQuery.prototype = {
   initialize: function(obj_name, recorder_name) {
      this.obj_name      = obj_name;
      this.recorder_name = recorder_name;
      this.playlist      = new Array();
      this.playing       = false;
      this.playAll       = false;
      this.id            = 0;
      this.sound_uri     = "";
      this.id_for_url    = new Object();
      this.url_for_id    = new Object();
      
   },

   addToPlaylist: function(sound_uri) {
      var new_id = this.playlist.length;
      this.playlist.push(sound_uri);
      this.id_for_url[sound_uri] = new_id;
      this.id_for_url[new_id]    = new_id;
      this.url_for_id[new_id]    = sound_uri;
      return new_id;
   },

   clearPlaylist: function() {
      this.playlist = new Array();
   },

   pause: function() {
      if (this.playing) {
         document[this.recorder_name].pausePlayback();
         this.playing = false;
         this.onPause(this.sound_uri || this.url_for_id[this.id]);
         this.onStop(this.sound_uri || this.url_for_id[this.id]);
         this.sound_uri = "";
      }
   },

   play: function(sound_id) {	  
      if (!this.playing) {
         if (sound_id) {
            if (this.id_for_url[sound_id]+1) {   // "+1" solves a Bug where the id is 0. 0 means "false" in JS.
               this.sound_uri = "";
               sound_id       = this.id_for_url[sound_id];
               this.id        = sound_id;
            }
            else {
               this.sound_uri = sound_id;
            }
         }
         this.playing = false;
         this.playAll = false;
         window.setTimeout(this.obj_name+"._play()", 120);
      }
   },

   playall: function() {
      if (!this.playing) {
         this.sound_uri = ""; 
         if (this.playlist.length == 0) return;
         var resume = true;
         this.playing = false;
         if (!this.playAll) {
            this.id = 0;
            resume  = false;
         }
         this.playAll = true;
         this.onPlayAll();
         window.setTimeout(this.obj_name+"._play("+resume+")", 120);
      }
   },

   stop: function() {
      if (this.playing) {
         document[this.recorder_name].stopPlayback();
         this.onStop(this.sound_uri || this.url_for_id[this.id]);
         this.sound_uri = "";
      }
         if (this.playAll) {
            this.onStopAll();
         }
         this.playing = false;
         this.playAll = false;
         this.id      = 0;
   },

   stopped: function() {
      this.playing = false;
      this.onStop(this.sound_uri || this.url_for_id[this.id]);
      if (this.playAll && this.id < this.playlist.length-1) {
         this.id++;
         window.setTimeout(this.obj_name+"._play()", 120);
      }
      else {
         this.playAll = false;
         this.onStopAll();
      }
   },

   _play: function(resume) {
		currentid = this.id;
      if (resume) { document[this.recorder_name].play(); }
      else if (this.sound_uri) { document[this.recorder_name].play("http://" + this.urlPrefix + this.sound_uri); }
      else { 
    	  document[this.recorder_name].play("http://" + this.urlPrefix + this.playlist[this.id]);
		}
      this.playing = true;
      this.onPlay(this.sound_uri || this.url_for_id[this.id]);
   },
	
	urlPrefix : '',
   onPlay    : function(id) {
		soundrecorderOnPlay(id);
   },
   onStop    : function(id) {
		if (typeof window.soundrecorderOnStop == 'function') {
			soundrecorderOnStop(id);
		}
   },
   onPause   : function(id) {
		if (typeof window.soundrecorderOnPause == 'function') {
	  		soundrecorderOnPause(id);
	  	}
   },
   onPlayAll : function() {
      if (typeof window.soundrecorderOnPlayAll == 'function') {
	  		soundrecorderOnPlayAll();
	  	}
   },
   onStopAll : function() {
      if (typeof window.soundrecorderOnStopAll == 'function') {
			soundrecorderOnStopAll();
		}
   }
};

player = jQuery.soundrecorder;
