// Copyright (c) 2009-2010 Anders Toxboe & Benjamin Media - For White Album Gallery

document.observe('dom:loaded', function() {
	new BrightGallery();
});

BrightGallery = Class.create({
	next_url: null,
	previous_url: null,
	inputting: false,

	initialize: function(container_id) {
		this.retrieveNavigationURLs();
		document.observe('keydown', this.onKeyDown.bind(this));
		document.observe('keyup', this.onKeyUp.bind(this));
		$$('input,textarea,select').invoke('observe', 'focus', this.onFocus.bind(this));
		$$('input,textarea,select').invoke('observe', 'blur', this.onBlur.bind(this));

	},

	retrieveNavigationURLs: function() {
		this.next_url = $('next') ? $('next').getAttribute('href') : null;
		this.previous_url = $('previous') ? $('previous').getAttribute('href') : null;
	},

	onKeyDown: function(event) {
		if (this.inputting || event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) { return false; }
		if (this.previous_url && [Event.KEY_LEFT, 74, 106].include(event.keyCode)) {
			location.href = this.previous_url;
		}
		if (this.next_url && [Event.KEY_RIGHT, 75, 107].include(event.keyCode)) {
			location.href = this.next_url;
		}
	},

	onKeyUp: function(event) {
		this.onKeyDown(event);
	},
	
	onFocus: function(event) {
		this.inputting = true;
	},

	onBlur: function(event) {
		this.inputting = false;
	}
});
