/*
	Textarea Resizer 0.1
	Allows the user to expand and shrink the height of HTML textareas
	by Chris Barr of Eject Media - http://ejectmedia.net

Based on Mootools 1.1 - http://mootools.net
Requires:
 - Window.DomReady
 - Fx.Style
 - Fx.Styles
 - Fx.Transitions
*/
var textareaSizer = {
	init: function(){
		//options
		tbResizeAmount = 50;				//amount each button press will resize by
		tbResizeDuration = 700;				//how long each resize takes
		tbResizeTransition = Fx.Transitions.Expo.easeOut; //the transition :P
		tbMinSize = 75; 					//always leave a min size or it looks strange
		tbMaxSize = 1000;					//set to 0 for no maximum
		tbDescription = 'textbox size: ';	//text to display next to the buttons
		tbSkipClass = 'nogrow';				//a class defined on textareas that you do NOT want to resize
		
		//make new arrays of elements
		tbEffects = [];
		tbGrowers = [];
		tbShrinkers = [];
		
		//select each textarea without the class to skip
		$$('textarea').each(function(el,i){
			if (!el.hasClass(tbSkipClass)){
				textareaSizer.makeButtons(el,i);
			}					 					 
		});
	},
	makeButtons: function(textbox,i){
		//create the elements
		wrapper = new Element('div').addClass('tbSizeChanger').setStyle('width',textbox.getStyle('width')).injectAfter(textbox);
		tbGrowers[i] = new Element('a').setStyles({'float':'right','display':'block'}).setProperty('href','#').addClass('tbLarger').injectInside(wrapper).setHTML('大');
		tbShrinkers[i] = new Element('a').setStyles({'float':'right','display':'block'}).setProperty('href','#').addClass('tbSmaller').injectInside(wrapper).setHTML('小');
		new Element('div').addClass('tbSizeText').setHTML(tbDescription).setStyle('float','right').injectInside(wrapper);
		new Element('div').setStyle('clear','both').injectInside(wrapper);
		//create new effect
		tbEffects[i] = new Fx.Style(textbox, 'height', {duration: tbResizeDuration ,transition: tbResizeTransition});
		//add click events
		tbGrowers[i].addEvent('click', function(ev){
			new Event(ev).stop();
			toSize = textbox.getStyle('height').toInt()+tbResizeAmount;
			if(toSize >= tbMaxSize && tbMaxSize !=0){
				tbEffects[i].start(tbMaxSize);
			}else{
				tbEffects[i].start(toSize);
			}
		});
		tbShrinkers[i].addEvent('click', function(ev){
			new Event(ev).stop();
			toSize = textbox.getStyle('height').toInt()-tbResizeAmount;
			if(toSize <= tbMinSize){
				tbEffects[i].start(tbMinSize);
			}else{
				tbEffects[i].start(toSize);
			}
		});
	}
};
window.addEvent('domready', textareaSizer.init.bind(textareaSizer));
