RumbleUI.Form = Class.create();

RumbleUI.Form.MasterCheckbox = Class.create();
RumbleUI.Form.MasterCheckbox.prototype = {

	initialize: function(master, name) {
		this.master = master;
		this.name = name;		
		master.observe("click", (function() {
			this.check(master, name);
		}).bind(this));
	}
}

RumbleUI.Form.MasterCheckbox.check = function(master, name) {
	if (!master.form) {
		return false;
	} 
	for (i = 0; i < master.form.elements.length; i++) {
		var formControl = master.form.elements[i];
		if ((formControl.type == "checkbox") && ((!name) || (formControl.name == name))) {
			formControl.checked = master.checked;
		}
	}	
}

RumbleUI.Form.Textarea = Class.create();

RumbleUI.Form.Textarea.isOverMaxLength = function(textareaElement, maxLength) {
	return $F(textareaElement).length <= maxLength;
}

RumbleUI.Form.Textarea.restrictLength = function(textareaElement, maxLength) {
	if (!RumbleUI.Form.Textarea.isOverMaxLength(textareaElement, maxLength)) {	
		textareaElement.value = $F(textareaElement).substr(0, maxLength);
		return false;
	}
	return true;
}

RumbleUI.Form.Textarea.LengthRestricter = Class.create();
RumbleUI.Form.Textarea.LengthRestricter.prototype = {
	initialize: function(textareaElement, maxLength) {
		textareaElement.onkeyup = RumbleUI.Form.Textarea.restrictLength.bind(this, textareaElement, maxLength);
	}
}

RumbleUI.Form.Textarea.wrapSelection = function(textareaElement, prefix, postfix) {
	var ie4 = document.all;
	var ns4 = document.layers;
	var ns6 = document.getElementById && !document.all;
	textareaElement = $(textareaElement);
	if (textareaElement) {
		textareaElement.focus();		
		if (ns6) {
			var selectionStart = textareaElement.selectionStart;
			var selectionEnd = textareaElement.selectionEnd;
			selectedText = textareaElement.value.substring(selectionStart,selectionEnd);
			textareaElement.value = textareaElement.value.substring(0, selectionStart) + prefix + selectedText +postfix + textareaElement.value.substring(selectionEnd);
			textareaElement.setSelectionRange(selectionStart+prefix.length,selectionStart+prefix.length+selectedText.length);
		} else if (ns4) {
			textareaElement.value += prefix + "<Type Here>" + postfix;
			textareaElement.setSelectionRange(textareaElement.value.length-postfix.length-11,textareaElement.value.length-postfix.length, textareaElement.value.length-postfix.length);
		} else if (ie4) {
			if (document.selection != null) {
				tr = document.selection.createRange();
				tr.text = prefix+tr.text+postfix;
				tr.select();
			} else {
				textareaElement.value += prefix + postfix;
			}
		}
		textareaElement.focus();
	}
}


RumbleUI.Form.AutoClear = Class.create();
RumbleUI.Form.AutoClear.prototype = {
	initialize: function(element, labels, options) {
	
		this.options = {
      beforeBlur: function() {},
      beforeFocus: function() {},
      afterBlur: function() {},
      afterFocus: function() {}      
    };
    
    Object.extend(this.options, options || { });
	
		this.labels = labels;
		if (this.labels && this.labels.constructor !== Array) {
			this.labels = [this.labels];
		}
		
		this.element = $(element);		
	
		if (this.element.type == "password") {
			this.password = true;
		} else if (this.element.type != "text" && this.element.type != "textarea") {
			return false;
		} 
		this.lastIndex = 0;
	  this.element.observe("focus", this.clearOnMismatch.bind(this));
		
		this.element.observe("blur", (function() {
			this.options.beforeBlur();
			if (this.element.value == "") {
				if (this.password) {
					if (!Prototype.Browser.IE) {
						this.element.type = "text";
					}
				}
				this.element.value = this.labels[0];
			}
			this.options.afterBlur();
		}).bind(this));
		
		if (this.element.value == "") {	
			if (this.password) {
				if (!Prototype.Browser.IE) {
					this.element.type = "text";
				}
			}		
			this.element.value = this.labels[0];
		}
		
		
		if ((this.lastIndex = this.labels.indexOf(this.element.value)) != -1) {
			if (this.password) {
				if (Prototype.Browser.IE) {
					
				} else {
					this.element.type = "text";
				}
			}
		}
	},
	
	clearOnMismatch: function() {
		this.options.beforeFocus();
		if ((this.lastIndex = this.labels.indexOf(this.element.value)) != -1) {
			this.element.clear();
			if (this.password) {
				this.element.type = "password";
			}
		}
		this.options.afterFocus();
	}
}

RumbleUI.Form.TextareaExpander = Class.create();
RumbleUI.Form.TextareaExpander.prototype = {
	initialize: function(element, options) {
		this.element = $(element);
	
		this.options = {
      maxHeight: null
    };
    
    Object.extend(this.options, options || { });
		
		this.shaddow = $("rumbleui-textshaddow");
	
		if (!this.shaddow) {
			this.shaddow = document.createElement("div");
			Element.extend(this.shaddow);
			this.shaddow.id = "rumbleui-textshaddow";
			this.shaddow.hide();
			document.body.appendChild(this.shaddow);
		}
		
		this.element.observe("keyup", this.update.bind(this));
		this.update();
	},
	
	update: function() {
		var width = this.element.getWidth();
		this.shaddow.setStyle( { width: width + "px" });
		this.shaddow.innerHTML = this.element.value.replace(/\n/g, "<br />");
		
		if (this.element.originalHeight == null) {
			this.element.originalHeight = this.element.getHeight();
		}
		
		var shaddowHeight = this.shaddow.getHeight()
		var height = this.element.getHeight();
		
		if ((shaddowHeight + 50) > height) {
			if ((this.options.maxHeight == null) || (height < this.options.maxHeight)) {
				var newHeight = Math.min(shaddowHeight + 50, this.options.maxHeight); 
				this.element.setStyle( { height: newHeight + "px" });
			}
		}	else if (shaddowHeight < this.element.originalHeight - 50) {
			this.element.setStyle( { height: this.element.originalHeight + "px" });
		}
	}
}
