/*
**  TextAreaResizer script by Jason Johnston (jj@lojjic.net)
**  Created August 2003.  Use freely, but give me credit.
**
**  This script adds a handle below textareas that the user
**  can drag with the mouse to resize the textarea.
*************************************************************
**  Modified by John Ha 2005, 2006 (ink@bur.st)
**
**  Now self contained, with slimmer cross-browser event
**  handling, and slightly simplified.
**  Updated: textareas with title "true" will be ignored
**  Eg. tinyMCE hidden textareas
**  Now includes maximise, fit-to, restore functionality.
**  Huge number of enhandements.
*/

function TextAreaResizer(elt) {
	this.element = elt;
	this.create();
}
TextAreaResizer.prototype = {
	create : function() {
		var elt = this.element;
		if (elt.title != 'true') {
			var thisRef = this;

			var d = document.createElement('div');
			d.className = 'clearfix';
			elt.parentNode.insertBefore(d, elt);


			var l = document.createElement('a');
			l.className = 'textarea-button';
			l.innerHTML = '#';
			l.title = 'Maximise to textarea content height';
			l.onmouseover = function() { this.style.color = '#fff'; this.style.background = '#69c'; };
			l.onmouseout = function() { this.style.color = '#777'; this.style.background = '#ddd'; };
			l.onclick = function() { thisRef.max(1); return false; };
			d.appendChild(l);


			var l = document.createElement('a');
			l.className = 'textarea-button';
			l.innerHTML = '+';
			l.title = 'Resize to textarea content height that best-fits window';
			l.onmouseover = function() { this.style.color = '#fff'; this.style.background = '#69c'; };
			l.onmouseout = function() { this.style.color = '#777'; this.style.background = '#ddd'; };
			l.onclick = function() { thisRef.max(2); return false; };
			d.appendChild(l);


			var l = document.createElement('a');
			l.className = 'textarea-button';
			l.innerHTML = '=';
			l.title = 'Restore to original textarea height';
			l.onmouseover = function() {
					this.style.color = '#fff';
					this.style.background = '#69c';
				};
			l.onmouseout = function() {
					this.style.color = '#777';
					this.style.background = '#ddd';
				};
			l.onclick = function() { thisRef.max(3); return false; };
			d.appendChild(l);

			var l = document.createElement('a');
			l.className = 'textarea-button';
			l.innerHTML = '-';
			l.title = 'Minimise textarea';
			l.onmouseover = function() { this.style.color = '#fff'; this.style.background = '#69c'; };
			l.onmouseout = function() { this.style.color = '#777'; this.style.background = '#ddd'; };
			l.onclick = function() { thisRef.max(4); return false; };
			d.appendChild(l);


			var h = this.handle = document.createElement("hr");
			h.className = 'handle-normal';
			h.title = '<ul><li>Click & drag to resize</li><li>Double-left-click to maximise</li><li>Right-click best-fit to window</li></ul>';
			addEvent(h, 'dblclick', function(evt){ thisRef.max(1); }, false);
			addEvent(h, 'mousedown', function(evt){
					if (evt.button == 2) thisRef.max(2);
					else thisRef.dragStart(evt);
				}, false);
			addEvent(h, 'mouseover', function(evt){ h.className = 'handle-highlight'; }, false);
			addEvent(h, 'mouseout', this.handleHigh = function(evt){ h.className = 'handle-normal'; }, false);
			addEvent(h, 'contextmenu', function(){ return false; }, false);
			elt.parentNode.insertBefore(h, elt.nextSibling);
/*
			if (elt.offsetWidth > 0) {
				elt.style.visibility = 'hidden';
				this.max(1);
				this.max(3);
				elt.style.visibility = 'visible';
			}
*/
		}
	},

	dragStart : function(evt) {
		var thisRef = this;

		// lock cursor shape
		document.getElementsByTagName('body')[0].style.cursor = 's-resize';

		// save mouseover handler from dom-tooltips
		this.mouseoverHandler = this.handle.mouseoverHandler;

		// disable mouseover for tooltips - tooltips should be "off" while dragging
		removeEvent(this.handle, 'mouseover', this.handle.mouseoverHandler, false);

		// turn off tooltip
		this.handle.mouseoutHandler();

		// highlight should remain on
		removeEvent(this.handle, 'mouseout', this.handleHigh, false);

		this.element.style.borderStyle = 'dashed';
		this.dragStartY = evt.clientY + 8;
		this.dragStartH = this.element.offsetHeight;

		addEvent(document, 'mousemove', this.dragMoveHdlr = function(evt){ thisRef.dragMove(evt); }, false);
		addEvent(document, 'mouseup', this.dragStopHdlr = function(evt){
				thisRef.dragStop(evt);

				// restore default cursor shape
				document.getElementsByTagName('body')[0].style.cursor = 'default';

				// restore mouseover for tooltips after drag stop
				addEvent(thisRef.handle, 'mouseover', thisRef.mouseoverHandler, false);

				// restore highlight handler
				thisRef.handle.className = 'handle-normal';
				addEvent(thisRef.handle, 'mouseout', thisRef.handleHigh = function(evt){ thisRef.handle.className = 'handle-normal'; }, false);
			}, false);
	},
	
	dragMove : function(evt) {
		var height = this.dragStartH + evt.clientY - this.dragStartY;
		this.element.style.height = (height > 0 ? height : 0) + 'px';
	},
	
	dragStop : function(evt) {
		this.element.style.borderStyle = 'solid';
		removeEvent(document, 'mousemove', this.dragMoveHdlr, false);
		removeEvent(document, 'mouseup', this.dragStopHdlr, false);
	},

	destroy : function() {
		var elt = this.element;
		elt.parentNode.removeChild(this.handle);
		elt.style.height = '';
	},

	max : function(mode) {
		if (!this.defHeight) this.defHeight = this.element.offsetHeight;
		if (!this.defWidth) this.defWidth = this.handle.offsetWidth;

		this.element.style.height = '1px';

		if (mode == 4) return false;

		if (mode == 3) {
			this.element.style.height = this.defHeight - 8 + 'px';
			return false;
		}

		str = '';
		for (i = 0; i < parseInt(this.element.scrollWidth / 10); i++) str += '		';
		this.element.value += str;

		// IE Bug? Need to retrieve scrollWidth first to init it!
		var dummy = this.element.scrollWidth;
		if (this.element.scrollWidth == this.element.clientWidth)
			wrap = 1;
		else
			wrap = 0;
		this.element.value = this.element.value.replace(str,'');


		// IE Bug? Need to retrieve scrollHeight first to init it!
		var dummy = this.element.scrollHeight;
		var maxHeight = this.element.scrollHeight +
			(checkIt('msie') ?
				wrap ?
					-6
				:	9
			: 	wrap ?
					0
				:	this.element.scrollWidth > this.element.clientWidth ?
						20
					:	0
			);


		if (mode == 2 && maxHeight > winSize()[1]) maxHeight = winSize()[1] - (checkIt('msie') ? 60 : 90);
		// For some reason Netscape won't accept style.height greater than 10000px
		this.element.style.height = maxHeight + (checkIt('msie') ? 2 : 0) + 'px';
/*
		if (checkIt('msie')) {
			this.handle.style.width = this.defWidth - 4 + 'px';
			this.element.style.width = this.defWidth - 20 + 'px';
		}
		scrollTo(0, findPosY(this.element) - (checkIt('msie') ? 60 : 70));
*/
		return false;
	}
};

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;
}

function winSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth - 16;
		myHeight = window.innerHeight - 16;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth - 20;
		myHeight = document.documentElement.clientHeight - 20;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth - 20;
		myHeight = document.body.clientHeight - 20;
	}
	return new Array(myWidth, myHeight);
}

// safari, omniweb, opera, webtv, icab, msie
function checkIt(string) {
	var detect = navigator.userAgent.toLowerCase();
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function textarea_init() {
	var textareas = document.getElementsByTagName('textarea');
	// Somehow var i was being corrupted.
	// Only when max(3) or max(2) called.
	// Using z instead. Weird.
	for (z = 0; z < textareas.length; z++) {
		new TextAreaResizer(textareas[z]);
	}
	// Re-init dom-tooltips (if available), so tooltips are shown for handles
	typeof tooltip != 'undefined' ? tooltip.init(new Array('hr', 'a')) : 0;
}

if (typeof schedule != 'function') {
	function addEvent(obj, evType, fn, useCapture) {
		if (obj.addEventListener) {
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.attachEvent) {
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		} else {
			alert('Handler could not be attached');
		}
	} 
	
	function removeEvent(obj, evType, fn, useCapture){
		if (obj.removeEventListener){
			obj.removeEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.detachEvent){
			var r = obj.detachEvent('on' + evType, fn);
			return r;
		} else {
			alert('Handler could not be removed');
		}
	} 
	
	addEvent(window, 'load', function() { textarea_init(); }, false);
} else schedule('textarea_init()');
