/* Playlist AJAX functions */
/* Requires Prototype */

/* Constructor function */
function Playlist(file,playlist_container,archive_container) {
	this.file = file;
	this.url = file;
	this.playlist_container = playlist_container;
	this.archive_container = archive_container;
	this.data = '';
	this.query = { fetch: 1 };
	this.addHandlers();
};

/* Load content (via AJAX) */
Playlist.prototype.load = function()
{
	if(arguments[0]) this.setQuery(arguments[0]);
	var query = $H(this.query).toQueryString();
	new Ajax.Request(this.url, {
		method: 'get',
		parameters: query,
		onSuccess: function(transport) {
			this.data = transport.responseText;
			$(this.target_container).innerHTML = this.data;
			this.addHandlers();
		}.bind(this)
	});
	
	this.query = { fetch: 1 };
}

/* add event handlers */

Playlist.prototype.addHandlers = function()
{

	/* playlist container */
	if($(this.playlist_container)) {
		$(this.playlist_container).select('.navigation a').invoke('observe','click',function(event){
			var link = Event.findElement(event,'A');
			this.url = link.href;
			this.target_container = this.playlist_container;
			this.load();
			Event.stop(event);
		}.bindAsEventListener(this));
	}
	
	/* archive container */
	if($(this.archive_container)) {
		$(this.archive_container).select('.navigation a').invoke('observe','click',function(event){
			var link = Event.findElement(event,'A');
			this.url = link.href;
			this.target_container = this.archive_container;
			this.load();
			Event.stop(event);
		}.bindAsEventListener(this));
	}
	
}

Playlist.prototype.debug = function(q)
{
	alert("page: "+q.page+"\ncategory: "+q.category);
}

Playlist.prototype.setQuery = function(q)
{
	if(q.cat) this.query.cat = q.cat;
	if(q.gallery_id) this.query.gallery_id = q.gallery_id;
}


