

/**
 *
 */
function TrackStack() {
  var self = this;
  this.items = [];
   
  /**
   * @param  string	pos			begin, end
   */
  this.addTrack = function(trackObj,pos) {
  	if(typeof(pos)=='undefined') {
  		pos = 'end' ;
  	}
  	// wenn items noch leer sind
    if(this.items.length==0){
    	this.items.push(trackObj);
    } else {
	  	if(pos=='end'){
		  	if(trackObj.id != this.items[(this.items.length-1)].id)
		    	this.items.push(trackObj);
	  	} else 
	  	if(pos=='begin'){
	  		if(trackObj.id != this.items[0].id)
	  			this.items.unshift(trackObj);
	  	}
    }
  }
	this.deleteTrack = function(track_id){
		var newitems=new Array();
		for(i=0;i<this.items.length;i++){
			//console.log(this.items[i].title);
			if(this.items[i].id!=track_id){
				newitems.unshift(this.items[i]);
			}
		}
		this.items=newitems;
	}
  this.getTrack = function (track_id){
		for(i=0;i<this.items.length;i++){
			//console.log(this.items[i].title);
			if(this.items[i].id==track_id){
				return this.items[i];
			}
		}
	}
	 
	this.getTracks = function(){
		return this.items ;
	}
	
	// holt das nächste Trackobject, anhand einer Track
	this.getNextTrackById = function(track_id){
		var index=0;
		for(i=0;i<this.items.length;i++){
			if(this.items[i].id==track_id){
				index = i;
				continue;
			}
		}
		console.log('aktueller index = '+index);
		if (index+1>=this.items.length) {
			index = 0 ;
		} else { 
			index++ ;
		}
		console.log('next index = '+index);
		
		return this.items[index] ;
	}
	this.setPlaystate = function(track_id){
		// alle auf 0setzen
		for(i=0;i<this.items.length;i++){
			this.items[i].playstate='0';
		}
		// playerstate beim track setzen
		for(i=0;i<this.items.length;i++){
			if(this.items[i].id==track_id){
				this.items[i].playstate='1';
				continue;
			}
		}
	}
  this.init = function() {
    //alert('TrackStack.init();');
    //console.log(this.items.length+' Tracks im Stack.');
  }
	
  this.init();

}
