audioPlayer = function(player_id)
{
    this.tracks = new Array();
    this.currentTrack = 1;
    this.firstTrack = 1;
    this.init = false;
    this.object = false;
    this.player_div = document.getElementById(player_id);
    this.object = document.getElementById("obj_"+player_id);

    /**
     * Initialisation
     */
    this.onInit = function()
    {
        this.position = 0;
        this.autoNext = false;
        this.object.width = 1;
        this.object.height = 1;
    };

    this.getElementsByClassName = function (oElm, strClassName)
    {
        if (document.getElementsByClassName)
        {
            return oElm.getElementsByClassName(strClassName);
        }
    
      var arrElements = oElm.all;
      var arrReturnElements = new Array();
      strClassName = strClassName.replace(/\-/g, "\\-");
      var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
      var oElement;
      for(var i=0; i<arrElements.length; i++){
          oElement = arrElements[i];     
          if(oRegExp.test(oElement.className)){
              arrReturnElements.push(oElement);
          }   
      }
      return (arrReturnElements);
    };

    this.onUpdate = function()
    {
        var isPlaying = (this.isPlaying == "true");
        this.player_div.className = this.player_div.className.replace(/[ ]*(paused|playing|waiting|$)/, isPlaying ? " playing" : " paused");

        this.getElementsByClassName(this.player_div, "progress_play")[0].style.width = Math.round((this.position / this.duration) * 100) + "%";
        this.getElementsByClassName(this.player_div, "progress_load")[0].style.width = this.bytesPercent + "%";

        var max_duration = parseInt(this.duration) - 2000;

        if (this.bytesTotal == this.bytesLoaded && parseInt(this.position) >= max_duration && !this.autoNext)
        {
            this.autoNext = true;
            window.setTimeout(function (thisObj) { thisObj.next(true); }, 2000, this);
        }
    };

    this.playPause = function(Id)
    {
        if (!this.object)
        {
            alert("Vous devez installer Flash pour utiliser ce lecteur. " );
            return false;
        }

        if (Id == false && this.init == false)
        {
            Id = this.firstTrack;
        }

        if (Id == false)
        {
            if (this.isPlaying == "true")
            {
              this.isPlaying = "false";
                this.object.SetVariable("method:pause", "");
            }
            else
            {
            this.isPlaying = "true";
                this.object.SetVariable("method:play", "");
            }
        }
        else
        {
            var currentRow = this.getElementsByClassName(this.player_div, "track_"+this.currentTrack)[0];
            currentRow.className = currentRow.className.replace(/[ ]*(current)[ ]*/, '');
            this.object.SetVariable("method:setUrl", this.tracks[Id][0]);
            this.object.SetVariable("method:play", "");
            this.isPlaying = "true";
            this.currentTrack = Id;
            this.getElementsByClassName(this.player_div, "track_"+this.currentTrack)[0].className += " current";

            if (this.init == false)
            {
                this.object.SetVariable("enabled", "true");
                this.init = true;
            }
        }
    };

    this.next = function(autoNext)
    {
        var next = false;
        var last_id = false;

        for (id in this.tracks)
        {
            if (last_id == this.currentTrack)
                next = id;

            last_id = id;
        }

        if (!next)
            next = this.firstTrack;

        this.playPause(next);

        if (autoNext)
        {
            this.autoNext = false;
        }
    };

    this.previous = function()
    {
        var prev = false;
        var last_id = false;

        for (id in this.tracks)
        {
            if (id == this.currentTrack)
                prev = last_id;

            last_id = id;
        }

        if (!prev)
            prev = last_id;

        this.playPause(prev);
    };

    this.setPosition = function(e, o)
    {
        var curleft = 0;
        var curwidth = o.offsetWidth;

        if (o.offsetParent) {
            while (o.offsetParent) {
                curleft += o.offsetLeft;
                o = o.offsetParent;
            }
        }
        else if (o.x) curleft += o.x;

        var percent = ((e.clientX - curleft) / curwidth);
        var pos = Math.round(this.duration * percent);
        this.object.SetVariable("method:setPosition", pos);
    };
};