text: /**
   * NicEdit Instance
   * This class creates an editable area out of any block level node and converting textarea nodes into editable areas. Instances of this class use the contentEditable attribute and besides textareas do not modify the orginal node.
   * @author: Brian Kirchoff
   * @requires: nicCore
   * @version 0.9
   */
  
  var nicEditorInstance = bkClass.extend({
  	isSelected : false,
  
  	construct : function(e,options,nicEditor) {
  		this.ne = nicEditor;
  		this.elm = this.e = e;
  		this.options = options || {};
  
  		var newX = parseInt(e.getStyle('width'), 10) || e.clientWidth;
  		var newY = parseInt(e.getStyle('height'), 10) || e.clientHeight;
  		this.initialHeight = newY-8;
  
  		var isTextarea = (e.nodeName.toLowerCase() == "textarea");
  		if(isTextarea || this.options.hasPanel) {
  			var ie7s = (bkLib.isMSIE && !((typeof document.body.style.maxHeight != "undefined") && document.compatMode == "CSS1Compat"))
  			var s = {width: this.ne.options.getEditorWidth(e), border : '1px solid #ccc', borderTop : 0, overflowY : 'auto', overflowX: 'hidden' };
  			s[(ie7s) ? 'height' : 'maxHeight'] = (this.ne.options.maxHeight) ? this.ne.options.maxHeight+'px' : null;
  			this.editorContain = new bkElement('DIV').setStyle(s).appendBefore(e);
  
      	var ElmExtraoptions = {};
  		if(typeof this.ne.options.editor_outlineColor != "undefined")
  			ElmExtraoptions['outlineColor']=this.ne.options.editor_outlineColor;
  
  			var editorElm = new bkElement('DIV').setStyle(bkExtend({ margin: '4px', minHeight : newY+'px'},ElmExtraoptions)).addClass('main').appendTo(this.editorContain);
  
  			e.setStyle({display : 'none'});
  
  			editorElm.innerHTML = e.innerHTML;
  			if(isTextarea) {
  				editorElm.setContent(e.value);
  				this.copyElm = e;
  				var f = e.parentTag('FORM');
  				if(f) { bkLib.addEvent( f, 'submit', this.saveContent.closure(this)); }
  			}
  			editorElm.setStyle((ie7s) ? {height : newY+'px'} : {overflow: 'hidden'});
  			this.elm = editorElm;
  		}
  		this.ne.addEvent('blur',this.blur.closure(this));
  
  		this.init();
  		this.blur();
  	},
  
  	init : function() {
  		this.elm.setAttribute('contentEditable','true');
  		//this returns <br> when initial content is empty
  		//if (this.getContent() == "" && this.options.initWithLineBreak) {
  		//	this.setContent('<br />');
  		//}
  		this.instanceDoc = document.defaultView;
  		this.elm.addEvent('mousedown',this.selected.closureListener(this)).addEvent('keypress',this.keyDown.closureListener(this)).addEvent('focus',this.selected.closure(this)).addEvent('blur',this.blur.closure(this)).addEvent('keyup',this.selected.closure(this));
  		this.ne.fireEvent('add',this);
  	},
  
  	remove : function() {
  		this.saveContent();
  		if(this.copyElm || this.options.hasPanel) {
  			this.editorContain.remove();
  			this.e.setStyle({'display' : 'block'});
  			this.ne.removePanel();
  		}
  		this.disable();
  		this.ne.fireEvent('remove',this);
  	},
  
  	disable : function() {
  		this.elm.setAttribute('contentEditable','false');
  	},
  
  	getSel : function() {
  		return (window.getSelection) ? window.getSelection() : document.selection;
  	},
  
  	getRng : function() {
  		var s = this.getSel();
  		if(!s || s.rangeCount === 0) { return null; }
  
  		//an exception can occur when on a double click some text gets selected unintentionally
  		try{
  			//return (s.rangeCount > 0) ? s.getRangeAt(0) : s.createRange();
  			return (s.rangeCount > 0) ? s.getRangeAt(0) : s.addRange();
  			//or ? //to be checked again
  			//return (s.rangeCount > 0) ? s.getRangeAt(0) :      s.createRange && s.createRange() || document.createRange();
  
  		}
  		catch(e){}
  	},
  
  	selRng : function(rng,s) {
  		if(window.getSelection) {
  			s.removeAllRanges();
  			s.addRange(rng);
  		} else {
  			rng.select();
  		}
  	},
  
  	selElm : function() {
  		var r = this.getRng();
  		if(!r) { return; }
  		if(r.startContainer) {
  			var contain = r.startContainer;
  			if(r.cloneContents().childNodes.length == 1) {
  				for(var i=0;i<contain.childNodes.length;i++) {
  					var rng = contain.childNodes[i].ownerDocument.createRange();
  					rng.selectNode(contain.childNodes[i]);
  					if(r.compareBoundaryPoints(Range.START_TO_START,rng) != 1 &&
  						r.compareBoundaryPoints(Range.END_TO_END,rng) != -1) {
  						return $BK(contain.childNodes[i]);
  					}
  				}
  			}
  			return $BK(contain);
  		} else {
  			return $BK((this.getSel().type == "Control") ? r.item(0) : r.parentElement());
  		}
  	},
  
  	saveRng : function() {
  		this.savedRange = this.getRng();
  		this.savedSel = this.getSel();
  	},
  
  	restoreRng : function() {
  		if(this.savedRange) {
  			this.selRng(this.savedRange,this.savedSel);
  		}
  	},
  
  	keyDown : function(e,t) {
  		this.ne.fireEvent('keyDown', this, e);
  
  		if(e.ctrlKey) {
  			this.ne.fireEvent('key',this,e);
  		}
  	},
  
  	selected : function(e,t) {
  		if(!t && !(t = this.selElm)) { t = this.selElm(); }
  		if(!e.ctrlKey) {
  			var selInstance = this.ne.selectedInstance;
  			if(selInstance != this) {
  				if(selInstance) {
  					this.ne.fireEvent('blur',selInstance,t);
  				}
  				this.ne.selectedInstance = this;
  				this.ne.fireEvent('focus',selInstance,t);
  			}
  			this.ne.fireEvent('selected',selInstance,t);
  			this.isFocused = true;
  			this.elm.addClass('selected');
  		}
  		return false;
  	},
  
  	blur : function() {
  		this.isFocused = false;
  		this.elm.removeClass('selected');
  	},
  
  	saveContent : function() {
  		if(this.copyElm || this.options.hasPanel) {
  			this.ne.fireEvent('save',this);
  			(this.copyElm) ? this.copyElm.value = this.getContent() : this.e.innerHTML = this.getContent();
  		}
  	},
  
  	getElm : function() {
  		return this.elm;
  	},
  
  	getContent : function() {
  		this.content = this.getElm().innerHTML;
  		this.ne.fireEvent('get',this);
  		return this.content;
  	},
  
  	setContent : function(e) {
  		this.content = e;
  		this.ne.fireEvent('set',this);
  		this.elm.innerHTML = this.content;
  	},
  
  	nicCommand : function(cmd,args) {
  		document.execCommand(cmd,false,args);
  	}
  });
  