/*
Нужно переделать по-человечески... 
*/
Proto.UI.editor=Class.create({
                bkpStart:0,
                bkpEnd:0,
        initialize: function() 
        {
                      
        	this.options = Object.extend({
        	
        	                   brackets: [ "[" , "]" ],
        	                   buttons : [
                    	                       {element:$$(".editorBold")[0],"tag":'b'},
                    	                       {element:$$(".editorItalic")[0],"tag":'i'},
                    	                       {element:$$(".editorUnderline")[0],"tag":'u'},
                    	                       {element:$$(".editorQuote")[0],"tag":'quote'},
                    	                       {element:$$(".editorLink")[0],"tag":'url','bbStyle':0,'question':'Введите URL'}
        	                                ],
        	                   textarea : _('id_comment')
        	                   
        	                          }, arguments[0] || { });
        	                          
        	                     this.area = _(this.options.textarea);
        	                     this.options.buttons.each(function(o){
        	                     try{
        	                           o.element.observe("mousedown",function(e){
        	                                   Event.stop(e);
        	                                   this.addTag(o);
        	                               }.bind(this));
        	                               }catch(x){}
        	                       }.bind(this));
        	            return(this);              
        },


            
            addTag: function(tag,params,single)
            { 
                    if(typeof tag == "object")
                    {
                        bak = tag;
                        tag = bak.tag;
                    }
                    if(!params){params={}}
                    var begin,selection,end; 
                    
                    if (document.selection && document.selection.createRange) 
                    {
                        this.area= {}
                        var range = document.selection.createRange();
                        var txt = selection = range.text;

                    }
                    else
                    {
                    
                    
                    
                    var bkpStart = this.area.selectionStart;
                    var bkpEnd = this.area.selectionEnd;
                    
                    begin = _(this.options.textarea).value.substr(0, this.area.selectionStart);  
                    selection = _(this.options.textarea).value.substr(this.area.selectionStart, this.area.selectionEnd - this.area.selectionStart);  
                    end = _(this.options.textarea).value.substr(this.area.selectionEnd); 
                    
                    }
                    var paramsstr="";
                    Proto.toHash(params,'').each(function(h){
                        paramsstr+=" "+h.key+"='"+h.value+"'";
                    });
                     if(bak.question)
                     {
                     var url = prompt(bak.question,'http://');
                     if(url=="http://" || url=="" || !url){ return; }
                     if(tag=="url"){
                        var reg = /http:\/\/[A-Za-z0-9\.-]+\.[A-Za-z]{2,5}/i;
                        
                        if(! reg.test(url)){
                            alert('Ошибка в URL'); 
                            return;
                        }
                        if(selection=="")
                        {
                            selection=url
                        }
                     }
                     startTag = this.options.brackets[0]+tag+"="+url+paramsstr+this.options.brackets[1]; 
                     }
                     else
                     {
                     startTag = this.options.brackets[0]+tag+paramsstr+this.options.brackets[1]; 
                     }
                    
                    endTag =  ((!single)?this.options.brackets[0]+"/"+tag+this.options.brackets[1]:"");  
                    
                    
                    if (document.selection && document.selection.createRange) 
                    {
                       
                       range.text = startTag + selection + endTag;

                    }
                    else
                    {
                    _(this.options.textarea).value = begin + startTag + selection + endTag + end;
                    
                    if(bkpStart==bkpEnd)
                    {
                        this.area.selectionEnd = this.area.selectionStart = bkpEnd+startTag.length;
                    }
                    else
                    {
                         this.area.selectionStart = bkpStart ;
                         this.area.selectionEnd = bkpEnd+(startTag + endTag).length; 
                    }
               
                }
            }
        
});
