/**
 * @name : Textarea Expander With Animation
 * @author : Swapnil Sarwe
 * @version : 0.2 [BETA]
 * @description : Based on and inspired by James Padolsey's jQuery autoResize
 *              The basic idea is to have textarea expand as the user types in
 *              the content, so that the user never has the scroll bar appear
 *              for the textarea.
 * @see : http://james.padolsey.com/javascript/jquery-plugin-autoresize/
 */

/**
 * @desription: addListener function is taken from the Jonathon Snook's book -
 *              "Accelerated DOM Scripting with Ajax, APIs, and Libraries"
 * @see : http://snook.ca/
 */
function addListener(element, event, listener) {
	if (element.addEventListener)
		element.addEventListener(event, listener, false);
	else if (element.attachEvent) {
		element.attachEvent('on' + event, function() {
					listener.call(element)
				});
	}
}

function Xpander(options) {
	makeDuplicate = function(el) {
		var newEl = el.cloneNode(true);
		newEl.removeAttribute('id');
		newEl.removeAttribute('name');
		newEl.style['marginLeft'] = "-9999px";
		return newEl;
	};
	insertAfter = function(target, bullet) {
		target.nextSibling ? target.parentNode.insertBefore(bullet,
				target.nextSibling) : target.parentNode.appendChild(bullet);
	};
	var lastHeight = null;
	activateResize = function(evt) {
		evt = evt || window.event;
		var cloned = this.clone;
		cloned.value = this.value;
		if (!lastHeight) {
			lastHeight = this.clientHeight;
		} else {
			lastHeight = cloned.scrollHeight;
		}
		var newHeight = cloned.scrollHeight + options.xtraSpace;
		if (newHeight == this.clientHeight)
			return;
		this.style['height'] = newHeight + 'px';
		if (evt.preventDefault) {
			evt.preventDefault();
		} else {
			evt.returnValue = false;
		}
	};
	var tAs = document.getElementsByTagName('message_box');
	var tACount = tAs.length;
	while (tACount) {
		tACount--;
		tAs[tACount].style['overflowY'] = 'hidden';
		var dupA = makeDuplicate(tAs[tACount]);
		tAs[tACount].clone = dupA;
		insertAfter(tAs[tACount], dupA);
		addListener(tAs[tACount], 'keyup', activateResize);
	}
}

