if (!Prototype.Version) { 
	throw("prototype.js is required to use this library") 
} else {

var ButtonDisabler = {
  disable_button_on: function(event, options) {
		var options = options || {}
		if (!event) { throw(new this.ArgumentError("You must supply an event for this to be triggered on")) }
		if (!options.classes && !options.ids) { throw(new this.ArgumentError("You must supply at least one class or id to apply this JS to.")) }
		var classes = (options.classes instanceof Array) ? options.classes : [options.classes]
		var ids = (options.ids instanceof Array) ? options.ids : [options.ids]
		var elements = $A(this._find_from_ids(ids).concat(this._find_from_classes(classes)))
		var handler_options = {post_event: options.post_event}
		this._apply_event_handlers(event, elements, handler_options)
	},
	
// Private
	_find_from_classes: function(classes) { 
		var elements = $A(classes).collect(function (class_name){ return document.getElementsByClassName(class_name) })
		elements = $A(elements).flatten().compact()
		return elements
	},
	
	_find_from_ids: function(ids) { 
		var elements = $A(ids).collect(function (id_name){ return $(id_name) })
		elements = $A(elements).compact()
		return elements
	},
	
	_apply_event_handlers: function(event, elements, handler_options) {
		elements.each(function (element){ 
			this._check_type(element)
			if (!element.button_disabling_applied) { 
				Event.observe(element, event, this['_event_handler_for_on' + event.toLowerCase()].bind(element))
				if (handler_options.post_event != null) { Event.observe(element, event, handler_options.post_event.bind(element)) }
			}
			element.button_disabling_applied = true
		}.bind(this))
	},
	
	_check_type: function(element) {
		var element_type = element.getAttribute('type')
		if ((typeof(element_type) == 'undefined' || null == element_type) || ((element_type.toLowerCase() != 'button') && (element_type.toLowerCase() != 'submit'))) { 
			throw(new this.ElementTypeError("Element must be an input of type button or submit"))
		}
	},
	
// Default Event Handlers
	// In the event handler context, 'this' is the element it's bound to, which happens in
	// apply_event_handlers
	_event_handler_for_onclick: function(event){
		this.disabled = true
		// This is because once I set this to disabled, IE won't submit the form itself.
		if (this.type.toLowerCase() == 'submit') { this.form.submit() }
		return true
	},
	
// Errors
	ArgumentError: function(message) {
		var error = new Error(message)
		error.name = 'ArgumentError'
		return error
	},
	
	ElementTypeError: function(message) {
		var error = new Error(message)
		error.name = 'ElementTypeError'
		return error
	}
}

} // prototype.js check