/* Gallery AJAX functions */
/* Requires Prototype */

/* Constructor function */
function Gallery(file,container,menu) {
	this.file = file;
	this.url = file;
	this.container = container;
	this.menu = menu;
	this.data = '';
	this.query = { fetch: 1 };
	this.addHandlers();
};

/* Load content (via AJAX) */
Gallery.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;
			this.addHandlers();
			shutterOnload();
		}.bind(this)
	});
	
	this.query = { fetch: 1 };
}

/* add event handlers */

Gallery.prototype.addHandlers = function()
{

	/* category select menu */
	if($(this.menu)) {
		$(this.menu).stopObserving('change');
		$(this.menu).observe('change',function(event){
			var element = event.element();
			var cid = $F(element);
			this.url = this.file;
			this.load({cat: cid});
			
		}.bind(this));
	}
	$(this.container).select('a').invoke('stopObserving','click');
	$(this.container).select('a').invoke('observe','click',function(event){
		var link = Event.findElement(event,'A');
		this.url = link.href;
		if(!this.url.match(/\.\w{2,5}$/))
		{
			this.load();
		}
		else
		{
			//window.open(this.url);
		}
		Event.stop(event);
	}.bindAsEventListener(this));
	
}

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

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


