// @name: HtmlInput
// @type: Behaviour
// @author: Rocco Howard
// @version: 20070712

Bluora.INPUTactiviate = function(el) {
	el = $(el);
	
	if (el.up('td')) {
		if (el.up('td').hasClassName('highlightable')) {
			el.up('td').addClassName('field_highlight');
		}
	}
	if (!el.hasClassName('popupdatefield')) {
		Bluora.INPUTfieldExplaination(el, 'activiate');
	}
};
	
Bluora.INPUTdeactiviate = function(el) {
	el = $(el);
	if (el.up('td')) {
		if (el.up('td').hasClassName('highlightable')) {
			el.up('td').removeClassName('field_highlight');
		}
	}
	if (!el.hasClassName('popupdatefield')) {
		Bluora.INPUTfieldExplaination(el, 'deactiviate');
	}
};

Bluora.INPUTfieldExplaination = function (el, type) {
	el = $(el);
	if (el.up('td')) {
		if (!el.hasClassName('popupdatefield')) {
			if (el.alt != '' && typeof el.alt != 'undefined') {
				if ($F(el) == '' && type == 'deactiviate'
					&& !el.up('td').hasClassName('field_highlight')) {
					el.up('td').addClassName('field_explaination');
					el.value = el.alt;
				} else if ($F(el) != '' && $F(el) != el.alt && type == 'deactiviate') {
					el.up('td').removeClassName('field_explaination');
				} else if ($F(el) == el.alt && type == 'activiate') {
					el.up('td').removeClassName('field_explaination');
					el.value = '';
				}
			}
		}
	}
};

Bluora.TDonclick = function(el) {
	inputobj = el.getElementsByTagName('input');
	if (inputobj.length != 0) {
		if (Element.hasClassName(inputobj[0],'notselectable')) {
			inputobj = [];
		}
	}
	if (inputobj.length == 0) {
		inputobj = el.getElementsByTagName('textarea');
	}
	if (inputobj[0]) {
		if (!inputobj[0].disabled) {
			if (inputobj[0].focus) {
				inputobj[0].focus();
			}
			if (inputobj[0].select != null
				&& Element.hasClassName(inputobj[0], 'selectable')) {
				inputobj[0].select();
			}
			Bluora.INPUTactiviate(inputobj[0]);
		}
	}
};

var Extender = {
	get: function (el) {
		if (typeof el.alt != 'undefined') {
			field_alt = el.alt;
			if (field_alt == $F(el)) {
				el.value = '';
			}
		}
		return $F(el);
	},
	setDefault: function(element) {
		element.value = element.defaultValue;
		if (typeof Bluora.INPUTfieldExplaination != 'undefined') {
			Bluora.INPUTfieldExplaination(element, 'deactiviate');
		}
	}
}
Element.addMethods(['input','textarea'],Extender);

var Extender = {
	fieldExplainOff: function (element) {
		for (var i = 0; i < $(element).elements.length; i++) {
			var tag = $(element).elements[i].tagName.toLowerCase();
			if (tag == 'input') {
				if ($($(element).elements[i]).alt == $F($(element).elements[i])) {
					$($(element).elements[i]).value = '';
				}
			}
		}
	},
	fieldExplainOn: function (element) {
		for (var i = 0; i < $(element).elements.length; i++) {
			var tag = $(element).elements[i].tagName.toLowerCase();
			if (tag == 'input') {
				Bluora.INPUTfieldExplaination($($(element).elements[i]),'deactiviate');	
			}
		}
	}
}
Element.addMethods(['form'],Extender);

$$F = function (element) {
	field = $(element);
	if (field != null) {
		if (typeof field.alt != 'undefined') {
			if (field.alt == $F(element)) {
				field.value = '';
			}
		}
		switch(element.type) {
			case 'radio':
				if (element.checked) {
					field_value = $F(element);
				} else {
					field_value = false;
				}
				break;
			default:
				field_value = $F(element);
		}
		Bluora.INPUTfieldExplaination(field,'deactiviate');	
		
		return field_value;
	} else {
		Bluora.log(element);	
	}
};

Bluora.Behaviour.HtmlInput = {
	'input': function (el) {
		if (!el.hasClassName('BluoraAutoCompleteOn')) {
			el.setAttribute('autocomplete', 'off');
		}
		if (!el.hasClassName('popupdatefield')) {
			Bluora.INPUTfieldExplaination(el, 'deactiviate');
		}
	},
	'input.highlight': function(el) {
		el.onfocus = function() {
			Bluora.INPUTactiviate(this);
			if (el.actiononfocus) {
				el.actiononfocus();
			}
		};
		el.onblur = function() {
			Bluora.INPUTdeactiviate(this);
			if (el.actiononblur) {
				return el.actiononblur();
			}
		};
	},
	'SELECT': function (el) {
		if (!el.hasClassName('BluoraAutoCompleteOn')) {
			el.setAttribute('autocomplete', 'off');
		}
	},
	
	'select.highlight': function(el) {
		el.onmouseover = function() {
			if (typeof Bluora != 'undefined') {
				Bluora.INPUTactiviate(this);
			}
		};
		el.onmouseout = function() {
			if (typeof Bluora != 'undefined') {
				Bluora.INPUTdeactiviate(this);
			}
		};
	},
	
	'td.highlightable': function (el) {
		el.onmouseover = function () {
			el.style.cursor = 'text';
		};
		el.onclick = function () {
			Bluora.TDonclick(el);
		};
	},
	'textarea.BluoraAutoGrow': function(el) {
		if(!$('autogrow_'+el.id)) {
			el.previous_line_count = 0;
			el.style.overflow = 'hidden';
			autogrow_div= document.createElement('div');
			autogrow_div.id='autogrow_'+el.id;
			autogrow_div.style.position="absolute";
			autogrow_div.style.left="-10000px";
			autogrow_div.style.top="-10000px";
			autogrow_div.fontSize=el.style.fontSize+'px';
			autogrow_div.style.width=parseInt(el.clientWidth-8)+'px';
			el.setAttribute('startHeight',el.clientHeight);
			el.parentNode.appendChild(autogrow_div);
		}
		
		el.onkeyup = function() {
			line_count = el.value.split("\n").length;
			
			if (line_count != el.previous_line_count) {
				el.previous_line_count = line_count;
				autogrow_div = $('autogrow_'+el.id);
				var clientHeight = el.clientHeight;
				autogrow_div.innerHTML=el.value.replace(/[\n]/g,'<br />&nbsp;');
				var autogrowHeight = autogrow_div.clientHeight;
				var to_height;
				var startHeight=el.getAttribute('startHeight');
				if(autogrowHeight<startHeight){
					to_height=startHeight;
				} else {
					to_height=autogrowHeight+0;
				}
				if(to_height && to_height!=clientHeight){
					el.style.height=to_height+'px';
				}
			}
		}
		el.onkeyup();
	},
	'textarea.highlight': function(el) {
		
		el.onfocus = function() {
			Bluora.INPUTactiviate(this);
			if (el.actiononfocus) {
				el.actiononfocus();
			}
		};
		el.onblur = function() {
			Bluora.INPUTdeactiviate(this);
			if (el.actiononblur) {
				el.actiononblur();
			}
		};
	}
};

Bluora.Behaviour.register(Bluora.Behaviour.HtmlInput);
Bluora.Behaviour.apply();