  
var playerHandlerGenerator = function(idPlayer, myPlayList){
  
  
  var $player  = undefined;
  var playList = myPlayList;
  var playItem = 0;
  var volume   = 50;
  initPlayer();
  
  /*****************************************************************************
   *                       FUNCTIONS DEFINITION
   ****************************************************************************/
  
  function initPlayer(){
    
    // Init HTML of the player
    $player = $(
      '<div class="player">'+
        '<div id="'+idPlayer+'"></div>'+
        '<div class="jp-playlist-player">'+
          '<div class="jp-interface">'+
            '<ul class="jp-controls">'+
              '<li><button href="#" class="jp-button jp-previous"    ></button></li>'+
              '<li><button href="#" class="jp-button jp-pause"       ></button></li>'+
              '<li><button href="#" class="jp-button jp-play"        ></button></li>'+
              '<li><button href="#" class="jp-button jp-stop"        ></button></li>'+
              '<li><button href="#" class="jp-button jp-next"        ></button></li>'+
              '<li><button href="#" class="jp-button jp-volume-min"  ></button></li>'+
              '<li><button href="#" class="jp-button jp-volume-max"  ></button></li>'+
            '</ul>'+
            '<div class="clearline"></div>'+
            '<div class="jp-progress">'+
              '<div class="jp-load-bar">'+
                '<div class="jp-play-bar"></div>'+
              '</div>'+
            '</div>'+
            '<div class="jp-time-controls">'+
              '<div class="jp-play-time"></div>'+
              '<div class="jp-total-time"></div>'+
            '</div>'+
            /*
            '<div class="jp-volume-controls">'+
              '<div class="clearline"></div>'+
              '<button href="#" class="jp-button jp-volume-min"></button>'+
              '<div class="jp-volume-bar">'+
                '<div class="jp-volume-bar-value"></div>'+
              '</div>'+
              '<button href="#" class="jp-button jp-volume-max"></button>'+
              '<div class="clearline"></div>'+
            '</div>'+
            */
          '</div>'+
          '<div class="jp-playlist"></div>'+
        '</div>'+
      '</div>'
    );
    
  
    $player.find("#"+idPlayer).jPlayer({
    
      oggSupport    : false,
      position      : "static",
      volume        : volume,
      //errorAlerts   : true,
      //warningAlerts : true,
      nativeSupport : true,
      customCssIds  : true,
      ready         : function() {
        playListInit(false); // Parameter is a boolean for autoplay.
      }
      
    });
    
    initPlayList();
    
    // Init of element for update display
    var $jpPlayTime       = $player.find(".jp-play-time");
    var $jpTotalTime      = $player.find(".jp-total-time");
    var $jpPlayBar        = $player.find(".jp-play-bar");
    var $jpProgress       = $player.find(".jp-progress");
        
    $player.find("#"+idPlayer).jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
      $jpPlayBar.css("width", (playedPercentRelative*$jpProgress.width()/100)+"px");
      $jpPlayTime.text($.jPlayer.convertTime(playedTime));
      $jpTotalTime.text($.jPlayer.convertTime(totalTime));
    });
    
    $player.find("#"+idPlayer).jPlayer("onSoundComplete", function() {
      playListNext();
    });
    
    //Hide pause button
    $player.find(".jp-pause").hide();
    
    // Bind all controls
    bindControls();
  
  }
  
   
  function setVolumeBarValue(){
    var $jpVolumeBar      = $player.find(".jp-volume-bar");
    var $jpVolumeBarValue = $player.find(".jp-volume-bar-value");
    $jpVolumeBarValue.width($jpVolumeBar.width()*volume/100);
  }
  
  function bindControls(){
    
    // Bind play button
    $player.find(".jp-play").click( function() {
      $player.find("#"+idPlayer).jPlayer("play");
      
      $(this).hide();
      $player.find(".jp-pause").show();
      
      $(this).blur();
      return false;
    });
    
    // Bind pause button
    $player.find(".jp-pause").click( function() {     
      $player.find("#"+idPlayer).jPlayer("pause");
      
      $(this).hide();
      $player.find(".jp-play").show();
      
      $(this).blur();
      return false;
    });
    
    // Bind stop button
    $player.find(".jp-stop").click( function() {
      $player.find("#"+idPlayer).jPlayer("stop");
      $(this).blur();
      return false;
    });
    
    // Bind volume min button
    $player.find(".jp-volume-min").click( function() {
      if(volume > 0){
        volume -= 10;
      }
      else{
        volume = 0;
      }
      $player.find("#"+idPlayer).jPlayer("volume", volume);
      setVolumeBarValue();
      $(this).blur();
      return false;
    });
    
    // Bind volume max button
    $player.find(".jp-volume-max").click( function() {
      if(volume < 100){
        volume += 10;
      }
      else{
        volume = 100
      }
      setVolumeBarValue();
      $player.find("#"+idPlayer).jPlayer("volume", volume);
      $(this).blur();
      return false;
    });
    
    
    // Bind previous button
    $player.find(".jp-previous").click( function() {
      playListPrev();
      $(this).blur();
      return false;
    });
  
    // Bind next button
    $player.find(".jp-next").click( function() {
      playListNext();
      $(this).blur();
      return false;
    });
  }
  
  function initPlayList() {
    $player.find(".jp-playlist").empty();
    $player.find(".jp-playlist").append("<ul></ul>");

    for (i=0 ; i < playList.length ; i++) {
      
      // Set the file
      $player.find("#"+idPlayer).jPlayer( "setFile",  playList[i].mp3,  playList[i].ogg);
      
      
      // Set Html
      var $listItem = $("<li><a href='#'>"+ playList[i].name +"</a></li>");
      $listItem.find("a").addClass('jp-playlist-item_'+i);
      if(i == playList.length-1){
        $listItem.addClass('jp-playlist-item-last');
      }
      $player.find(".jp-playlist ul").append($listItem);
      
      // Set action
      $listItem.data( "index", i ).click( function() {
        var index = $(this).data("index");
        if (playItem != index) {
          playListChange( index );
        } else {
          $player.find("#"+idPlayer).jPlayer("play");
        }
        $(this).blur();
        return false;
      });
    }
    
  }
  
  
  function playListInit(autoplay) {
    if(autoplay) {
      playListChange( playItem );
    } else {
      playListConfig( playItem );
    }
  }
  
  function playListConfig( index ) {
    $player.find(".jp-playlist-item_"+playItem).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
    $player.find(".jp-playlist-item_"+index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
    playItem = index;
    $player.find("#"+idPlayer).jPlayer("setFile", playList[playItem].mp3, playList[playItem].ogg);
  }
  
  function playListChange( index ) {
    playListConfig( index );
    $player.find("#"+idPlayer).jPlayer("play");
  }
  
  function playListNext() {
    var index = (playItem+1 < playList.length) ? playItem+1 : 0;
    playListChange( index );
  }
  
  function playListPrev() {
    var index = (playItem-1 >= 0) ? playItem-1 : playList.length-1;
    playListChange( index );
  }
  
  
  return {
    $player           : $player,
    setVolumeBarValue : setVolumeBarValue
  };
  
}
