/*	BUTTON VALUE REMEDY $Revision: 1.8 $
	Correct Internet Explorer's <button> handling
	reference: http://webdev.macromedia.com/wiki/index.php/ButtonValueRemedy
*/

ButtonValueRemedy = (function() {
	var _isNotNeeded = (function(){ 
		/*--- I'm only testing the value problem here. ---*/
		var test = adobe.Element.create("button", {value:"ok"});
		test.innerHTML = "not";
		return test.value =="ok"
	})();
	
	_Remedy = function() {
		if(_isNotNeeded) return;
		
		this.activeButton;
		this.activeName;
		this.buttons = $A(document.getElementsByTagName("BUTTON")).findAll(function(button) {
			return button.getAttributeNode("value");
		});
		this.submitObserver = this.doSubmitFix.bindAsEventListener(this);
		this.buttonObserver = this.setActiveButton.bindAsEventListener(this);
		this.buttons.each(function(button) {
			Event.observe(button, "focus", this.buttonObserver);
			Event.observe(button.form, "submit", this.submitObserver);
		}.bind(this));
	}
	
	_Remedy.prototype = {
		setActiveButton: function(event) {
			var el = event.element();
			this.activeName = el.getAttribute("name");
			return this.activeButton = el;
		},
		doSubmitFix:function(event) {
			var activeForm = event.element();
			this.buttons.each(function(button) {
				if(button.form == activeForm) {
					var isActive = (button == this.activeButton);
					button.disabled=(!isActive);
					if(isActive) {
						var w = button.offsetWidth+"px";
						button.innerHTML = button.getAttributeNode("value").value;
						adobe.Element.setAttributes(button, {
							"value":"", 
							"style":"width:"+w+";overflow:hidden;line-height:-999px;"});
					} else {
						button.setAttribute("name", button.name+"Remove");
					}
				}
				Event.stopObserving(button, "focus", this.buttonObserver);
				Event.stopObserving(button.form, "submit", this.submitObserver);
			}.bind(this));
		}
	}
	return {
		init: function() {
			return new _Remedy();
		}
	}
})();

registerOnReady(ButtonValueRemedy.init);
