// Copyright (c) 2009 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', $.proxy(this.onKeyPress, this));
		document.observe('keyup', $.proxy(this.onKeyPress, this));
		$$('input,textarea,select').invoke('observe', 'focus', $.proxy(this.onFocus, this));
		$$('input,textarea,select').invoke('observe', 'blur', $.proxy(this.onBlur, this));

	},

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

	onKeyPress: function(event) {
		if (this.inputting) { 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;
		}
	},

	onFocus: function(event) {
		this.inputting = true;
//		alert('focus: ' + this.inputting)
	},

	onBlur: function(event) {
		this.inputting = false;
//		alert('blur: ' + this.inputting)
	}
});

