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();
            }else{
                this.setSelector(options.css_name);
                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 path = this.img_path;
        $(document.body).getElements(this.selector).each(function(textArea){
            if(Browser.Engine.webkit){
                textArea.setStyle('width','95%');
            }else{            
                var conteiner = new Element('div',{
                    'styles':{
                        'border':'1px #CCCCCC solid',
                        'position':'relative',
                        'padding':'0 4PX 7px 0'
                    }
                });

                conteiner.inject(textArea,'before')
                textArea.inject(conteiner,'top');
                var grab = new Element('div',{
                    'styles':{
                        'width':'7px',
                        'height':'7px',
                        'background':"url('"+path+"') no-repeat",
                        'position':'absolute',
                        'right':'1px',
                        'bottom':'-12px',
                        'padding':'0'
                    }
                }).inject(conteiner,'bottom');
                conteiner.makeResizable({
                    'handle':grab
                
                });
                textArea.setStyles({
                    'border':'none 0',
                    'width':'100%',
                    'height':'97%',
                    'padding':'2px',
                    'margin':'0'
                });
            }
        });

    },
    bindEventIE7:function(){
        var path = this.img_path;
        $(document.body).getElements(this.selector).each(function(textArea){
        
            
            var conteiner = new Element('div',{
                'styles':{
                    'border':'1px #CCCCCC solid',
                    'position':'relative',
                    'padding':'0 0 7px 0'
                }
            });

            conteiner.inject(textArea,'before')
            textArea.inject(conteiner,'top');
            var grab = new Element('div',{
                'styles':{
                    'width':'7px',
                    'height':'7px',
                    'background':"url('"+path+"') no-repeat",
                    'position':'absolute',
                    'right':'1px',
                    'bottom':'-12px',
                    'padding':'0'
                }
            }).inject(conteiner,'bottom');
            
            conteiner.makeResizable({
                'handle':grab,
                'onDrag':function(el){
                    if(el.getStyle('height').toInt()<30){
                        this.fireEvent('onCancel');
                        el.setStyle('height',30);
                    }else{
                        textArea.setStyle('height',(el.getStyle('height').toInt()-5));
                    }
                    if(el.getStyle('width').toInt()<30){
                        this.fireEvent('onCancel');
                        el.setStyle('width',30);
                    }
                    

                }
            });
            textArea.setStyles({
                'border':'none 0',
                'width':'100%',
                'height':'100%',
                'padding':'2px',
                'margin':'0'
            });

        });

    }
});
