var ResizeTextArea = new Class({
    initialize: function(options){
        if(Browser.Engine.trident && Browser.Engine.version<6){
            this.setSelector(options.css_name);
            this.setImg(options.img_path);
            this.bindEventIE7();
        }else{
            if(!Browser.Engine.webkit){
                this.setSelector(options.css_name);
                this.setImg(options.img_path);
                this.bindEvent();
            }
        }
        
    },
    setSelector:function(selector){
        if($defined(selector)){
            this.selector = "textarea."+selector;
        }else{
            this.selector = "textarea";
        }
    },
    setImg:function(imgpath){
        if($defined(imgpath)){
            this.img_path = imgpath;
        }else{
            this.img_path = "/img/resizer.gif";
        }
    },
    bindEvent:function(){
		var obj = this;
        $(document.body).getElements(this.selector).each(function(textArea){
            var res = obj.makeConteiner(textArea);
			res.cont.makeResizable({
                'handle':res.gr 
            });
        });

    },
	makeConteiner:function(textArea){
		var conteiner = new Element('div',{
            'styles':{
                'border-color':textArea.getStyle('border-color'),
				'border-width':textArea.getStyle('border-width'),
				'border-style':textArea.getStyle('border-style'),
                'position':'relative',
                'padding':'0 0 8px 0',
				'background-color':textArea.getStyle('background-color'),
				'width':textArea.getStyle('width')
            }
        });
		textArea.setStyles({
            'border':'none 0',
            'width':'100%',
            'height':'97%',
            'padding':'0',
            'margin':'0'
        });
		conteiner.inject(textArea,'before')
        textArea.inject(conteiner,'top');
        var grab = new Element('div',{
            'styles':{
                'width':'7px',
                'height':'7px',
                'background':"url('"+this.img_path+"') no-repeat",
                'position':'absolute',
                'right':'1px',
                'bottom':'1px',
                'padding':'0'
            }
        }).inject(conteiner,'bottom');
            
		return {'cont':conteiner,'gr':grab};
	},
    bindEventIE7:function(){
        var obj = this;
        $(document.body).getElements(this.selector).each(function(textArea){
            var res = obj.makeConteiner(textArea);
			res.cont.makeResizable({
                'handle':res.gr,
                'onDrag':function(el){
                    if(el.getStyle('height').toInt()<30){
                        el.setStyle('height',30);
                    }else{
                        textArea.setStyle('height',(el.getStyle('height').toInt()-5));
                    }
                    if(el.getStyle('width').toInt()<30){
                        el.setStyle('width',30);
                    }
                }
            });
        });
    }
});
