/* Artist AJAX functions */
/* Requires Prototype */

/* Constructor function, creates ajax call to XML file */
function Artist(file,container,nav,menu) {
	this.file = file;
	this.url = file;
	this.container = container;
	this.menu = menu;
	this.nav = nav;
	this.data = '';
	this.query = { fetch: 1 };
	this.addHandlers();
};

/* Load content (via AJAX) */
Artist.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.container).innerHTML = this.data;
			if(this.content_tab == 'albums') $$('#IntSidebar h4, #IntSidebar ul').invoke('hide');
			else $$('#IntSidebar h4, #IntSidebar ul').invoke('show');
			this.updateNav();
			if(staging_upload_path) staging_upload_path();
			if(shutterOnload) shutterOnload();
		}.bind(this)
	});
	
	this.query = { fetch: 1 };
}

/* add event handlers */

Artist.prototype.addHandlers = function()
{

	/* add load handler to artist select menu */
	if($(this.menu)) {
		$(this.menu).stopObserving('change');
		$(this.menu).observe('change',function(event){
			var element = event.element();
			var cid = $F(element);
			element.form.submit();	
		}.bind(this));
	}
	
	if($(this.nav)) {
		$(this.nav).select('a').invoke('observe','click',function(event){
			var link = Event.findElement(event,'A');
			this.active = link;
			this.url = link.href;
			var params = this.url.toQueryParams();
			this.content_tab = params.content_tab;
			if(!this.url.match(/\.\w{2,5}$/))
			{
				this.load();
			}
			else
			{
				window.open(this.url);
			}
			Event.stop(event);
		}.bindAsEventListener(this));
	}
}

/* update active tab on AJAX load */

Artist.prototype.updateNav = function() {
	if($(this.nav)) {
		$(this.nav).select('a').invoke('removeClassName','active');
		$(this.active).addClassName('active');
	}
}

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

Artist.prototype.setQuery = function(q)
{
	if(q.artist_id) this.query.artist_id = q.artist_id;
	if(q.content_tab) this.query.content_tab = q.content_tab;
}

