﻿function HtmlEditor(frameId){
		
	this.editorId = frameId;
	
	this.editor = null;

	this.isIE = document.all;

	this.BasePath = "";

	//this.IEVer = this._GetIEVer();
		
    //** privete , 获取IE的版本 *///
	this._GetIEVer = function(){
		var iVerNo = 0;
		var sVer = navigator.userAgent;
		if(sVer.indexOf("MSIE")>-1){
			var sVerNo = sVer.split(";")[1];
			sVerNo = sVerNo.replace("MSIE","");
			iVerNo = parseFloat(sVerNo);
		}
		return iVerNo;
	}
    
	//** public , 初始化 *///
	this.Init = function(){
		this.editor = document.getElementById(this.editorId).contentWindow;
        this.editor.name = this.editorId;
		if(this.editor){
			//但是IE与FireFox有点不同，为了兼容FireFox，所以必须创建一个新的document。 *///
			this.editor.document.open();
			this.editor.document.writeln('<html><body></body></html>');
			this.editor.document.close();
			this.editor.document.body.setAttribute("leftMargin","3");
			this.editor.document.body.setAttribute("topMargin","3");
			this.editor.document.body.setAttribute("marginWidth","3");
			this.editor.document.body.style.fontSize = "14px";
			this.editor.document.body.style.fontFamily = "Tahoma";
			if(this.isIE){
				this.editor.document.body.contentEditable = true;
			} 
			else{
				this.editor.document.designMode = 'On';
				this.editor.document.execCommand("useCSS",false, true);
				this.editor.document.execCommand('enableObjectResizing', false, true);
				this.editor.document.execCommand('enableInlineTableEditing', false, true);
			}
		}else{
			alert('can not get editor object');
		}
		return this;
	}

    //** public , 获取编辑器对象(iframe 对象)  *///
	this.GetEditor = function(){
		return this.editor;
	}

    //** public , 为编辑框添加事件  *///
	this.AddEventListener = function(event,func){
		if (this.editor.document.addEventListener){
			this.editor.document.addEventListener(event,func,true);
		}else{
			this.editor.document.attachEvent(event,func);
		}
	}

    //** 获取一个编辑器的当前光标位置  *///
	this.GetCursorPos = function(){
		this.editor.document.focus();
		var workRange = this.editor.document.selection.createRange();
		workRange.select();
		var allRange = this.editor.document.selection.createRange();
		workRange.moveToPoint(0,0); //** 先移到绝对零点 *///
		workRange.setEndPoint("StartToStart",allRange);
		var len = workRange.text.length;
		workRange.collapse(false);
		workRange.select();
		return len;
	}

	//** 设置编辑器的当前光标位置 *///
    this.SetCursorPos = function(iPos){
		var range = this.editor.document.selection.createRange(); 
		range.collapse(true); 
        range.moveToPoint(0,0); //** 先移到绝对零点 *///
		range.moveStart('character',iPos);
        range.select();
    }

	//** public , 获取编辑器的内容 *///
    this.GetHtmlContent = function(){
		return this.editor.document.body.innerHTML ;
	}

	//** public , 获取编辑器的纯文本内容 *///
    this.GetTextContent = function(){
		return this.editor.document.body.innerText ;
	}
    
	//** public , 设置编辑器的内容 *///
	this.SetHtmlContent = function(html){
		this.editor.document.body.innerHTML = html ;
	}

	//** public , 设置编辑器的纯文本内容 *///
	this.SetTextContent = function(text){
		this.editor.document.body.innerText = text ;
	}
    
	//** public , 将指定的内容添加到编辑器原内容的后面 *///
	this.AddHtmlContent = function(html){
		this.editor.document.body.innerHTML += html;
	}

	//** public , 将指定的内容添加到编辑器原内容的后面(纯文本) *///
	this.AddTextContent = function(text){
		this.editor.document.body.innerText += text;
	}

	//** public , 粘贴一段 html *///
	this.PasteHTML = function(html)
    {
		if (this.editor.document.selection.type.toLowerCase() != "none")
		{
			this.editor.document.selection.clear() ;
		}
		this.editor.document.selection.createRange().pasteHTML(html) ; 
    }

	//** public , 粘贴一段 纯文本 *///
	this.PasteText = function(text)
    {
		if (this.editor.document.selection.type.toLowerCase() != "none")
		{
			this.editor.document.selection.clear() ;
		}
		this.editor.document.selection.createRange().text = text; 
    }

	//** public , 清空编辑器的内容 *///
	this.ClearHtmlContent = function(){
		this.SetHtmlContent('');
	}
    
	//** public , 设置前景色 *///
    this.ForeColor = function(e){
		var sColor = this._DisplayColorBoard();
		this.Format("foreColor", sColor);
	}

    //** public , 设置背景色 *///
	this.BackColor = function(e){
		var sColor = this._DisplayColorBoard();
		this.Format("backColor", sColor);
	}

    //** public , 格式化格式 *///
    this.Format = function(type, para){
		this.editor.focus();
		if(!para){
			if(this.isIE){
				this.editor.document.execCommand(type);
			}else{
				this.editor.document.execCommand(type,false,false);
			}
		}else{
			this.editor.document.execCommand(type,false,para);
		}
		this.editor.focus();
	}
	
	//** private , 显示颜色选取框 *///
	this._DisplayColorBoard = function(){
		var arr = showModalDialog(this.BasePath + "ColorSelect.htm", "", "font-family:Verdana; font-size:12; status:no; dialogWidth:245px; dialogHeight:220px");
		if (arr != null) return arr;
		return;
	}

	//** public , 添加表情 *///
	this.AddPortrait = function(){
		var src = showModalDialog(this.BasePath + "portraitSelect.htm","", "font-family:Verdana; font-size:12; status:no; unadorned:yes; scroll:no; resizable:yes;dialogWidth:40em; dialogHeight:21em");
		if (src != null) this.Format("InsertImage", src);
		return;
	}

	//** public , 设置字体 *///
	this.FontFace = function(){
		var fontFace = showModalDialog(this.BasePath + "FontFaceSelect.htm","", "font-family:Verdana; font-size:12; status:no; unadorned:yes; scroll:no; resizable:yes;dialogWidth:400px; dialogHeight:100px");
		if (fontFace != null) this.Format("fontname", fontFace);
		return;
	}

	//** public , 设置字体大小 *///
	this.FontSize = function(){
		var FontSize = showModalDialog(this.BasePath + "FontSizeSelect.htm","", "font-family:Verdana; font-size:12; status:no; unadorned:yes; scroll:no; resizable:yes;dialogWidth:100px; dialogHeight:150px");
		if (FontSize != null) this.Format("fontsize", FontSize);
		return;
	}
}

