/*
autoexpand.js

Author: Scrivna
WWW:	http://scrivna.com
Description:	Will automagically resize any textarea... that's amazing
				So far it works on IE7, Firefox 2, Safari 3 and Opera 

Usage:
Simply use a classname of "autoExpand" on your textarea and include this file in your html

If you are doing something a bit more fancy ie ajax, you can simply call autoExpand(document.getElementById('yourtextarea')) to auto expand that area

<script language="javascript" type="text/javascript" src="autoexpand.js"></script>

Love you!

*/

function ae_getElementsByClassName(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function ae_addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}

function ae_getStyle(el, style) {
   if(!document.getElementById) return;
   
     var value = el.style[ae_toCamelCase(style)];
   
    if(!value)
        if(document.defaultView)
            value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
       
        else if(el.currentStyle)
            value = el.currentStyle[ae_toCamelCase(style)];
    
    if(value.length>0){
   		value = ((value.charAt(value.length-1,1) == "x") ? parseInt(value.substring(0,value.length-2)) : value);
   	}
    return value;
}
function ae_toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)   
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ?
       oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
}

function ae_init()
{
	elements = ae_getElementsByClassName('autoExpand');
	if (elements.length> 0)
	{
		for(var i = 0;i < elements.length;i++)
		{
			autoExpand(elements[i]);
		}
	}
}

var autoExpand = function(t)
{
	var textarea = t;
	var __minHeight = textarea.offsetHeight;
	var a = parseInt(ae_getStyle(textarea, 'min-height'));
	if (a>0) __minHeight = a;

	// adjust the textarea
	textarea.style.overflow = 'hidden';
	textarea.style.overflowX = 'auto';
	// set the width and height to the correct values
	textarea.style.width = ae_getStyle(textarea, 'width')+'px';
	textarea.style.height = ae_getStyle(textarea, 'height')+'px';
	
	// create a new element that will be used to track the dimensions
	var dummy_id = Math.floor(Math.random()*99999) + '_dummy';
	var div = document.createElement('div');
	div.setAttribute('id',dummy_id);
	document.body.appendChild(div);
	var dummy = document.getElementById(dummy_id);

	// match the new elements style to the textarea
	dummy.style.fontFamily = ae_getStyle(textarea, 'font-family');
	dummy.style.fontWeight = ae_getStyle(textarea, 'font-weight');
	dummy.style.fontSize = ae_getStyle(textarea, 'font-size')+'px';
	dummy.style.width = ae_getStyle(textarea, 'width')+'px';
	dummy.style.padding = ae_getStyle(textarea, 'padding')+'px';
	dummy.style.margin = ae_getStyle(textarea, 'margin')+'px';
	dummy.style.overflowX = 'auto';
	// hide the created div away
	dummy.style.position = 'absolute';
	dummy.style.top = '0px';
	dummy.style.left = '-9999px';
	dummy.innerHTML = '&nbsp;42';
	
	var __lineHeight = dummy.offsetHeight;
	var checkExpand = function(){
		var html = textarea.value;
		html = html.replace(/\n/g, '<br>new');
		if (dummy.innerHTML != html)
		{
			dummy.innerHTML = html;
			var __dummyHeight = dummy.offsetHeight;
			var __textareaHeight = textarea.offsetHeight;
			ae_debug('Textarea: '+ __textareaHeight);
			ae_debug('Dummy: '+ __dummyHeight);
			if (__textareaHeight != __dummyHeight)
			{
				if (__dummyHeight > __minHeight)
				{
					textarea.style.height = (__dummyHeight+__lineHeight) + 'px';
				}
				else 
				{
					textarea.style.height = __minHeight+'px';
				}
			}
		}
	}
	
	var ae_startExpand = function()
	{
		interval = window.setInterval(function() {checkExpand()}, 500);
	}
	var ae_stopExpand = function()
	{
		clearInterval(interval);
	}
	
	var ae_debug = function(str)
	{
		if(ae_debug==true) document.getElementById('debug').innerHTML += '<br />'+str;
	}
	
	ae_addEvent(textarea, 'focus', ae_startExpand);
	ae_addEvent(textarea, 'blur', ae_stopExpand);
	checkExpand();
}
ae_debug = false;
ae_addEvent(window,'load', ae_init);
