$(function(){
	var parent_selector = '.misc-files-gallery';

	var G = new Gallery(parent_selector);
	G.keyControls(document, new Array('left','right'));
	G.keyControls(G._context, new Array('up','down'));

	G.getThumbs().click(function(e){
		var jThis = $(this);
		G.setContext(jThis.closest(parent_selector));
		G.view(jThis);
		e.preventDefault();
	});

	G.getNavi('.next').click(function(e){
		var context = $(this).closest(parent_selector);

		G.setContext(context);
		G.viewNext();
		e.preventDefault();
	});

	G.getNavi('.prev').click(function(e){
		var context = $(this).closest(parent_selector);

		G.setContext(context);
		G.viewPrev();
		e.preventDefault();
	});


	$(parent_selector).find(G._viewport + ' a, ' + G._navi + ' .back').click(function(e){
		try{
			if ( !go_back_or_to_detail_view() ){
				e.preventDefault();
			}
		}catch(msg){}
	});


	G.setContext(G.getDefaultContext());
});


/*requires jQuery*/
function Gallery (context) {
	if (context) {
		this.setContext($(context));
	}
}

Gallery.prototype.jContext = null;
Gallery.prototype.norepeat = false;

Gallery.prototype._context = '.misc-files-gallery';
Gallery.prototype._thumbnails = '.thumbnails';
Gallery.prototype._viewport = '.viewport';
Gallery.prototype._navi = '.navi';
Gallery.prototype._fullview = '.fullview';
Gallery.prototype._thumbs = '.thumbnails .item';
Gallery.prototype._thumbimg = '.thumbnail';


Gallery.prototype.getThumbs = function(){
	return this.getContext().find(this._thumbs);
}

Gallery.prototype.selectThumb = function(jThumb){
	this.selectedThumb().removeClass('selected');
	jThumb.addClass('selected');
}

Gallery.prototype.selectedThumb = function(){
	return this.getThumbs().andSelf().find('.selected');
}

Gallery.prototype.view = function(jThumb){
	var img_src = jThumb.find(this._thumbimg).attr('data-ref');
	var target = this.getContext().find(this._fullview);

	target
		.css('height', target.height())
		.attr('src', '/images/s.gif')
	;


	var img_loader = new Image();
	
	img_loader.onload = function(){
		target
			.css('height', 'auto')
			.attr('src', img_src)
		;
	}
	img_loader.src = img_src; //must be after .onload, otherwise onload might not fire

	this.selectThumb(jThumb);
}

Gallery.prototype.viewNext = function(){
	var jSelected = this.selectedThumb();

	if (jSelected.index() == this.getThumbs().last().index()) {
		if (!this.norepeat) {
			jSelected = this.getThumbs().first();
		}
	}
	else {
		jSelected = jSelected.next();
	}

	this.view(jSelected);
}

Gallery.prototype.viewPrev = function(){
	var jSelected = this.selectedThumb();

	if (jSelected.index() == 0) {
		if (!this.norepeat) {
			jSelected = this.getThumbs().last();
		}
	}
	else {
		jSelected = jSelected.prev();
	}

	this.view(jSelected);
}

Gallery.prototype.keyControls = function(target, commands){
	var G = this;
	$(target).keydown(function(e){
		if (!e.altKey) {
			var key = e.keyCode;

			for(var i in commands) {
				var cmd = commands[i];

				if (cmd=='right' && key==39 || cmd=='down' && key==40) {
					G.viewNext();
					e.preventDefault();
				}
				else if (cmd=='left' && key==37 || cmd=='up' && key==38) {
					G.viewPrev();
					e.preventDefault();
				}
			}
		}
	});
}


Gallery.prototype.getNavi = function(selector){
	return this.getContext().find(this._navi + ' ' + selector);
}

Gallery.prototype.setContext = function(jEl){
	this.jContext = jEl;
}

Gallery.prototype.getContext = function(){
	return this.jContext != null ? this.jContext : this.getDefaultContext();
}

Gallery.prototype.getDefaultContext = function(){
	return $(this._context + ':first');
}