function Playlist(){
  this.tracklist = [];
  this.song = 0;
  this.last = -1;
  this.index = -1;

  this.current = function(){
    return this.song;
  };

  this.next = function(){
    this.last = this.song;
    if(this.tracklist.length > 0){
      if(this.tracklist[this.index+1] !=  null){
       this.song = this.tracklist[this.index+1];
       this.index++;
      }else{
       this.song = this.tracklist[0];
       this.index=0;
      }
    }else{
      this.song = -1;
    }
  };

  this.previous = function(){
    this.last = this.song;
    if(this.tracklist.length > 0){
      if((this.index-1) >= 0){
        this.song = this.tracklist[this.index-1];
        this.index--;
      }else{
        this.song = this.tracklist[this.tracklist.length - 1];
        this.index = this.tracklist.length - 1;
      }
    }else{
      this.song = -1;
    }
  };

  this.choose = function(id){
	this.last = this.song;
    for(var i=0;i<this.tracklist.length;i++){
      if(id == this.tracklist[i]){
        this.song = id;
        this.index = i;
        break;
      }
    }
  };

  this.addTrackHtml = function(id, artist, title) {
      track = artist + " - " + title;
      jQuery(".ui-sortable").append('<li id="song_' + id + '" value="' + id + '" title="' + track + '" class="song"></li>');
      jQuery("#song_" + id).append('<img src="images/playerListCheckboxChecked.gif" alt="Checkbox checked" />');
      jQuery("#song_" + id).append('<a onDblclick="javascript:player.choose(' + id + ');">' + track + '</a>');
  }

  this.addTrackId = function(id) {
      if(this.tracklist == null){
          this.tracklist = new Array();
        }
      this.tracklist[this.tracklist.length] = id;
      this.elipse(id);
  }

  this.addTrackList = function(playfirst) {
    jQuery.getJSON(baseUrl + "gettracks?sidx=nr&sord=asc", function(json){
      var rows = json.rows;
      for (row in rows) {
        if (rows[row].Artist) {
          player.playlist.addTrackHtml(
              rows[row].id,
              rows[row].Artist.name,
              rows[row].title);
          player.playlist.addTrackId(rows[row].id);
        }
      }
      jQuery('#playlistScroll').jScrollPane({showArrows:true});

      if (playfirst) {
        player.choose(rows[0].id);
        player.play();
      }
    })
  }


  this.add = function(id, auto){
	if(auto === undefined){
		auto = true;
	}
	if(!this.exist(id)){
      jQuery.getJSON(baseUrl + "getinfo/" + id + "/" + this.tracklist.length, function(json){
    	player.playlist.addTrackHtml(id, json.artist, json.title);
    	player.playlist.addTrackId(id);
    	if(auto){
        	player.choose(id);
    	}
        jQuery('#playlistScroll').jScrollPane({showArrows:true});
      });
    }else{
    	if(auto){
            player.choose(id);
    	}
    }
  };

  this.sort = function(){
      var sort = jQuery("#playlistScroll ul").sortable("toArray");
      this.tracklist = new Array();
      for(i=0;i<sort.length;i++){
        songId = sort[i].split("_");
        if(songId[1] == this.song) this.index = i;
        this.tracklist[i] = songId[1];
      }
      actions.call("track/sort", {"tracklist": this.tracklist});
  };


  this.elipse = function(id){
    var maxWidth = jQuery(".song").width() - 12;
    maxWidth -= jQuery(".song img").width();
    text = jQuery("#song_" + id + " a").text();
    var i=text.length;
    while(jQuery("#song_" + id + " a").width() > maxWidth && i > 0)
    {
      jQuery("#song_" + id + " a").html(text.substr(0,i) + '&hellip;');
      i--;
    }
  };

  this.togglePlaylist = function(){
  var duration = 500;

    if(jQuery("#playlistList").height() == "37"){
      jQuery("#playlist").css("background","url('" + baseUrl_root  + "images/playerListLongBackground.png') transparent no-repeat");
      jQuery('#playlistList .jspContainer').height(169);
      jQuery('#playlistHeaderArrow').attr("src",baseUrl_root  +"images/playerListArrowOpen.gif");
      jQuery('#playlistScroll').jScrollPane({showArrows:true});
      jQuery("#playlistList").animate({height: 169}, duration);
    }else{
      jQuery('#playlistList .jspContainer').animate({height: 37}, duration);
      jQuery("#playlistList").animate({height: 37}, duration, function(){
      jQuery('#playlistScroll').jScrollPane({showArrows:true});
      jQuery("#playlist").css("background","url('" + baseUrl_root  + "images/playerListShortBackground.png') transparent no-repeat");
        jQuery('#playlistHeaderArrow').attr("src",baseUrl_root  + "images/playerListArrowClosed.gif");
      });

    }
  };

  this.elipseAll = function(){
    for(var i=0;i<this.tracklist.length;i++){
      this.elipse(this.tracklist[i]);
    }
  };



  this.isEmpty = function(){
     return !(this.tracklist.length > 0);
  };

  this.exist = function(id){
    var i = -1;
    exist = false;
    while(!exist && i < this.tracklist.length){
      if(this.tracklist[i++] == id){
        exist = true;
      }
    }

    return exist;
  };

}

