function Playlist(JSONObject) {
	this.url = "/webservice/playlistmgr";
	this.list;
	this.name;
	this.edition;
	this.guid;
	this.createDate;
	this.modDate;
	this.id;
	this.trunc = 10000;
	this.sort = 0;
	this.size;
	this.modDateRanking;
	/** Sort values are
	NATURAL = 0;
	REVERSE_NATURAL_SORT = 1;
	TRACK_NAME_SORT = 6;
	REVERSE_TRACK_NAME_SORT = 7;
	ARTIST_NAME_SORT = 8;
	REVERSE_ARTIST_NAME_SORT = 9;
	ALBUM_NAME_SORT = 10;
	REVERSE_ALBUM_NAME_SORT = 11;
	**/
	this.mwidth = 10000;
	this.trackmwidth = 10000;
	this.albummwidth = 10000;
	this.artistmwidth = 10000;
	if (JSONObject){
		if (JSONObject.list) {
			this.list = JSONObject.list;
		}
		this.size = JSONObject.size;
		this.name = JSONObject.name;
		this.trunc = JSONObject.trunc;
		this.edition = JSONObject.edition;
		this.guid = JSONObject.guid;
		this.id = JSONObject.id;
		this.createDate = JSONObject.createdate;
		this.modDate = JSONObject.moddate;
		this.modDateRanking = JSONObject.modDateRanking;
	}
	
	this.onSet = function(data){Ajax.Responders.dispatch('PlaylistOnSet', data);  };
	this.onMove = function(data){Ajax.Responders.dispatch('PlaylistOnMove', data);  };
	this.onClear = function(data){Ajax.Responders.dispatch('PlaylistOnClear', data);  };
	this.onRemoveTrackById = function(data){Ajax.Responders.dispatch('PlaylistOnRemoveTrackById', data);  };
	this.onRemoveTrack = function(data){Ajax.Responders.dispatch('PlaylistOnRemoveTrack', data);  };
	this.onAddTrack = function(data){Ajax.Responders.dispatch('PlaylistOnAddTrack', data);  };
	this.onError = function(data){Ajax.Responders.dispatch('PlaylistOnError', data);  };
	this.onContacting = function(){Ajax.Responders.dispatch('PlaylistOnContacting', "CONTACTING");};
	
	
	this.sendMessage = function(url, content, callback){
						  this.onContacting();
						  var wrappedCallback = function (requestObj, emptyData) {
						  							if(requestObj.status == 200){
						  								var text = requestObj.responseText;
						  								if (text.substring(0,2) == "[{"){
						  									var data = eval(text);
						  								} else {
						  									var data = eval('('+text+')');
						  								}
						  								//alert(data);
						  								if (data.hasError){
						  									//alert("Whoops - " + data.exception.message + ' ' + data.exception.code);
															// all onError calls aren't working for some strange reason
						  									new Playlist().onError("Whoops - " + data.exception.message + ' ' + data.exception.code);
														}
						  								callback(data);
						  							} else {
						  								var data;
						  								data.exception.message="Bad response from ajax request. "
						  								data.exception.code=requestObj.status
						  								new Playlist().onTransportError(data);
						  							}
						  						}
						  new Ajax.Request(url,{
							    onSuccess: wrappedCallback,
							    method: 'post',
							    parameters: content.toQueryString()+"&sort"+this.sort
								});
						}
						

}



Playlist.prototype.setList = function (aList) {
	this.list = aList;
}

Playlist.prototype.getList = function () {
	return this.list;
}

Playlist.prototype.getLength = function () {
	return this.list.length;
}

Playlist.prototype.get = function (idx) {
	return this.list[idx];
}

Playlist.prototype.setName = function(aName){
	this.name = aName;
}

Playlist.prototype.getName = function(){
	return this.name;
}

Playlist.prototype.getTrunc = function(){
	return this.trunc;
}

Playlist.prototype.setId = function(anId){
	this.id = anId;
}

Playlist.prototype.getId = function(){
	return this.id;
}

Playlist.prototype.setGuid = function(aValue){
	this.guid = aValue;
}

Playlist.prototype.getGuid = function(){
	return this.guid;
}

Playlist.prototype.setEdition = function(aValue){
	this.edition = aValue;
}

Playlist.prototype.getEdition = function(){
	return this.edition;
}

Playlist.prototype.setCreateDate = function(aValue){
	this.createDate = aValue;
}

Playlist.prototype.getCreateDate = function(){
	return this.createDate;
}

Playlist.prototype.setModDate = function(aValue){
	this.modDate = aValue;
}

Playlist.prototype.getModDate = function(){
	return this.modDate;
}

Playlist.prototype.getModDateRanking = function(){
	return this.modDateRanking;
}

Playlist.prototype.clear = function (handler) {
	if (handler == null){
		handler = this.onClear;
	}
	var action = "clear";
	var content = $H({name: this.name,
	    		  returntype: "object",
	    		  todo: action});
	var callback = function (data){
						if (data.hasError){
								this.onError(data);
						} else {
								data = new Playlist(data);
								handler(data);
						}
					}
	this.sendMessage(this.url, content, callback);
	
}


Playlist.prototype.move = function (from, to, handler) {
	if (handler == null){
		handler = this.onMove;
	}
	if (from > this.list.length || to > this.list.length) {
		this.onError("Can't do a move operation beyond the length of the playlist");
		return;
	}
	var action = "move";
	var content = $H({name: this.name,
	    		 
	    		  todo: action,
	    		  returntype: "object",
	    		  from: from,
	    		  to: to});
	var callback = function (data){
						if (data.hasError){
								this.onError(data);
						} else {
								data = new Playlist(data);
								handler(data);
						}
					}
	this.sendMessage(this.url, content, callback);
	
}





Playlist.prototype.removeTrack = function (idx, handler) {
	if (handler == null){
		handler = this.onRemoveTrack;
	}
	if (idx != null) {
		if (idx >= this.getLength()) {
			this.onError("Can't do a remove operation beyond the length of the playlist");
			return;
		}
	}
	var action = "remove";
	var content = $H({id: this.id,
	    		  todo: action,
	    		  returntype: "object",
	    		  index: idx});
	var callback = function (data){
						if (data.hasError){
								this.onError(data);
						} else {
								data = new Playlist(data);
								handler(data);
						}
					}
	this.sendMessage(this.url, content, callback);
	
}




Playlist.prototype.addTrack = function (id, idx, handler) {
	if (handler == null){
		handler = this.onAddTrack;
	}
	if (idx != null) {
		if (idx > this.getLength()) {
			idx = this.getLength()
		}
	}
	var action = "insert";
	var content = $H({name: this.name,
	    		 
	    		  todo: action,
	    		  returntype: "object",
	    		  id: id,
	    		  index: idx});
	var callback = function (data){
						if (data.hasError){
								this.onError(data);
						} else {
								data = new Playlist(data);
								handler(data);
						}
					}
	this.sendMessage(this.url, content, callback);
	
}

Playlist.prototype.set = function (ids, handler) {
	if (handler == null){
		handler = this.onSet;
	}

	var action = "set-existing";
	var content = $H({id: this.id,
	    		  todo: action,
	    		  returntype: "object",
	    		  ids: ids
	    		  });
	var callback = function (data){
						if (data.hasError){
								this.onError(data);
						} else {
								data = new Playlist(data);
								handler(data);
						}
					}
	this.sendMessage(this.url, content, callback);
	
}


Playlist.prototype.logError = function (message) {
	this.onError(message);
}

Playlist.prototype.setSort = function (sort) {
	this.sort = sort;
}

Playlist.prototype.setSize = function (size) {
	this.size = size;
}

Playlist.prototype.getSize = function () {
	return this.size;
}
Playlist.prototype.toString = function () {

	var output = 'playlist name: ' + this.name + ' edition: ' + this.edition + ' guid: ' + this.guid;
	for (var i = 0; i<this.list.length; i++){
		output = output + this.list[i].id + " " + this.list[i].rcid + " " + this.list[i].playlength + " " + this.list[i].name + " " + this.list[i].discindex + " " + this.list[i].remoteid + " " + this.list[i].artistdisplayname;
	}
	return output;
}


