var sRTE;
var docStyle = "";
function word(sNodeBox, sHTML, sMode, sHeight, sShowCode){
	//alert(sNodeBox);
	var oHead = new Object;
	var oBody = new Object;
	var oArea = document.createElement("textarea");
	var posArr = [
	{w: 3, x: -2},
	{w: 20, x: -225, t: "粗体", func: "Bold", id: "c1"},
	{w: 20, x: -245, t: "斜体", func: "Italic", id: "c2"},
	{w: 20, x: -265, t: "下划线", func: "Underline", id: "c3"},
	{w: 2, x: 0, id: "c4"},
	{w: 20, x: -285, t: "左对齐", func: "justifyleft", id: "d1"},
	{w: 20, x: -305, t: "居中", func: "justifycenter", id: "d2"},
	{w: 20, x: -325, t: "右对齐", func: "justifyright", id: "d3"},
	{w: 2, x: 0, id: "d5"},
	{w: 20, x: -145, t: "添加图片", func: "img", id: "e1"},
	{w: 20, x: -759, t: "插入Flash", func: "flash", id: "e6"},
	{w: 20, x: -779, t: "插入视频", func: "video", id: "e7"},
	{w: 20, x: -105, t: "插入链接", func: "link", id: "e2"},
	{w: 20, x: -485, t: "插入文字框", func: "textarea", id: "e5"},
	{w: 2, x: 0, id: "e4"},
	{w: 20, x: -165, t: "横线", func: "inserthorizontalrule", id: "f1"},
	{w: 20, x: -185, t: "文本颜色", func: "forecolor", id: "f2"},
	{w: 20, x: -205, t: "背景色", func: "hilitecolor", id: "f3"},
	{w: 2, x: 0, id: "f4"},
	{w: 40, x: -545, t: "字体", func: "fontname", id: "g2"},
	{w: 60, x: -585, t: "字体大小", func: "fontsize", id: "g3"}
	];
	oHead["table"] = document.createElement("table");
	oHead["table"].width = "100%";
	oHead["table"].cellSpacing = "0px";
	oHead["table"].cellPadding = "0px";
	oHead["table"].border = "0px";
	oHead["table"].id = sNodeBox + "Table";
	oHead["table"].className = "editerToolsBox";
	oHead["tbody"] = document.createElement("tbody");
	oHead["tr"] = document.createElement("tr");
	oHead["td"] = document.createElement("td");
	oHead["td"].className = "editerToolsBG";
	for(var i = 0; i < posArr.length; i ++) {
		oHead["items"] = document.createElement("img");
		posArr[i]["id"] ? oHead["items"].id = posArr[i]["id"] : function(){};
		oHead["items"].src = "/editor/blank.gif";
		oHead["items"].style.margin = "1px";
		oHead["items"].className = "editerToolsIMG";
		oHead["items"].style.width = posArr[i]["w"] + "px";
		oHead["items"].style.backgroundPosition = posArr[i]["x"] + "px 0px";
		if(posArr[i]["func"] != null) {
			oHead["items"].title = posArr[i]["t"];
			oHead["items"]["func"] = posArr[i]["func"];
			oHead["items"].onmouseover = function () {
				var bp = this.style.backgroundPosition;
				this.style.backgroundPosition = bp.split(" ")[0] + " -20px";
			}
			oHead["items"].onmouseout = function () {
				var bp = this.style.backgroundPosition;
				this.style.backgroundPosition = bp.split(" ")[0] + " 0px";
			}
			oHead["items"].onclick = function () {
				wordChiCommand(this["func"]);
			}
		}
		oHead["td"].appendChild(oHead["items"]);
	}
	oHead["tr"].appendChild(oHead["td"]);
	oHead["tbody"].appendChild(oHead["tr"]);
	oHead["table"].appendChild(oHead["tbody"]);
	document.getElementById(sNodeBox).appendChild(oHead["table"]);

	oBody["table"] = document.createElement("table");
	oBody["table"].width = "100%";
	oBody["table"].cellSpacing = "0px";
	oBody["table"].cellPadding = "0px";
	oBody["table"].border = "0px";
	oBody["table"].className = "editerTextBox";
	oBody["table"].id = "editerTextBox";
	oBody["tbody"] = document.createElement("tbody");
	oBody["tr"] = document.createElement("tr");
	oBody["td"] = document.createElement("td");
	oBody["td"].className = "editerTextBG";
	oBody["td"].id = sNodeBox + "Iframes";
	oBody["tr"].appendChild(oBody["td"]);
	oBody["tbody"].appendChild(oBody["tr"]);

	oBody["tr"] = document.createElement("tr");
	oBody["td"] = document.createElement("td");
	oBody["td"].className = "editerTextBottomBG";
	oBody["input"] = document.createElement("input");
	oBody["input"].type = "checkbox";
	oBody["input"].id = "EditerCMD";
	
	function oInputFunc(){
		if(sShowCode==0){
			alert('您不是斑竹，无权源代码编辑！');
			return '';
		}
		var oRTE = getFrameNode(sRTE);
		oRTE.focus();
		var sEditBox = document.getElementById(sNodeBox + "Table");
		var sEditMode = sEditBox.style.display == "none" ? true : false;
		if(sEditMode){
			sEditBox.style.display = "";
			docStyle = oRTE.document.getElementsByTagName("style");
			docStyle[0].disabled = "";
			if(! window.Event){
				var sOutText = escape(oRTE.document.body.innerText);
				sOutText = sOutText.replace("%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E");
				sOutText = sOutText.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");
				oRTE.document.body.innerHTML = unescape(sOutText);
			}else{
				var oMozText = oRTE.document.body.ownerDocument.createRange();
				oMozText.selectNodeContents(oRTE.document.body);
				oRTE.document.body.innerHTML = oMozText.toString();
			}
		}else{
			sEditBox.style.display = "none";
			var innerHTML = oRTE.document.body.innerHTML;
			docStyle = oRTE.document.getElementsByTagName("style");
			docStyle[0].disabled = "true";
			if(!window.Event){
				oRTE.document.body.innerText = innerHTML;
			}else{
				var oMozText = oRTE.document.createTextNode(innerHTML);
				oRTE.document.body.innerHTML = "";
				oRTE.document.body.appendChild(oMozText);
			}
		}
	}
	oBody["input"].onclick = oInputFunc;
	oBody["label"] = document.createElement("label");
	oBody["label"].style.fontSize = '12px';
	oBody["label"].htmlFor = "EditerCMD"
	oBody["label"].innerHTML = "显示源代码";
	oBody["td"].appendChild(oBody["input"]);
	oBody["td"].appendChild(oBody["label"]);
	oBody["tr"].appendChild(oBody["td"]);
	oBody["tbody"].appendChild(oBody["tr"]);

	oBody["table"].appendChild(oBody["tbody"]);
	document.getElementById(sNodeBox).appendChild(oBody["table"]);

	sRTE = sNodeBox + "_area";
	document.getElementById(sNodeBox + "Iframes").innerHTML = "<iframe frameborder='0' width='100%' scrolling='yes' id=" + sRTE + " class='editerTextArea' style='height:" + sHeight + "px'></iframe>";

	oArea.name = sNodeBox;
	oArea.id = sNodeBox + "_textarea";
	oArea.style.display = "none";
	oArea.value = sHTML;
	document.getElementById(sNodeBox).appendChild(oArea);
	this.save = function(){
		if(oBody["input"].checked == true) {
			oInputFunc();
			oBody["input"].checked = false;
		}
		var oRTE = getFrameNode(sRTE);
		oRTE.focus();
		var sEditBox = document.getElementById(sNodeBox + "Table");
		var sEditMode = sEditBox.style.display == "none" ? true : false;
		if(sEditMode){
			if(!indow.Event){
				oArea.value = oRTE.document.body.innerText;
			}else{
				var oMozText = oRTE.document.body.ownerDocument.createRange();
				oMozText.selectNodeContents(oRTE.document.body);
				oArea.value = oMozText.toString();
			}
		}else{
			oArea.value = oRTE.document.body.innerHTML;
		}
	}
	
	function writeDesignMode(sNodeBox, sHTML, sMode) {
		enableDesignMode(sNodeBox, "on");
		var sFix = window.Event ? "<div></div>" : "";
		if(sMode == 1) {
			var frameHTML = sHTML + "\n\
			" + sFix;
		} else {
			var frameHTML = "\
			<html>\n\
			<head>\n\
			<style>\n\
			body {\n\
				background: #ffffff;\n\
				margin:0px;\n\
				padding:0px;\n\
				font-size:12px;\n\
				overflow:auto;\n\
				scrollbar-face-color:#fff;\n\
				scrollbar-highlight-color:#c1c1bb;\n\
				scrollbar-shadow-color:#c1c1bb;\n\
				scrollbar-3dlight-color:#ebebe4;\n\
				scrollbar-arrow-color:#cacab7;\n\
				scrollbar-track-color:#f4f4f0;\n\
				scrollbar-darkshadow-color:#ebebe4;\n\
				word-wrap: break-word;\n\
				font-family: 'Courier New', 'Tahoma', 'Verdana', 'Courier', '宋体';\n\
			}\n\
			</style>\n\
			</head>\n\
			<body>\n\
			" + sHTML + "\n\
			" + sFix + "\n\
			</body>\n\
			</html>";
		}
	
		var oRTE = getFrameNode(sNodeBox).document;
		oRTE.open();
		oRTE.write(frameHTML);
		oRTE.close();
	}
	writeDesignMode(sRTE, sHTML, sMode);
}

function getFrameNode(sNode){
	return document.frames ? document.frames[sNode] : document.getElementById(sNode).contentWindow;
}
	
function enableDesignMode(sNodeBox, sMode){
	document.frames ? document.frames[sNodeBox].document.designMode = sMode : document.getElementById(sNodeBox).contentDocument.designMode = sMode;
}

function wordChiCommand(_sCmd) {
	var oRTE = document.frames ? document.frames[sRTE] : document.getElementById(sRTE).contentWindow;
	function oPenWin(_sTitle, _sWidth, _sHeight, _sUrl, _bDialog, _open){
		if(_open) {
			window.open(_sUrl,"win","menubar=no,location=no,resizable=no,scrollbars=no,status=no,left=200,top=200,width="+_sWidth+",height="+_sHeight);
		} else {
			if(window.Event) {
				window.open(_sUrl,"win","menubar=no,location=no,resizable=no,scrollbars=no,status=no,innerWidth="+_sWidth+",innerHeight="+_sHeight);
			} else {
				if(_bDialog == true) {
					showModelessDialog(_sUrl, window, "dialogHeight:"+(_sHeight+20)+"px;dialogWidth:"+_sWidth+"px;status:no;help:no;resizable:yes;status:no;tustatus:no;");
				} else {
					showModalDialog(_sUrl, window, "dialogHeight:"+(_sHeight+20)+"px;dialogWidth:"+_sWidth+"px;status:no;help:no;resizable:yes;status:no;tustatus:no;");
				}
			}
		}
	}
	switch(_sCmd){
		
		case "":
			break;
		case "link":
			oPenWin("请输入网页地址", 300, 140, "/editor/InsertLink.html");
			break;
		case "table":
			oPenWin("请选择表格属性", 300, 240, "/editor/InsertTable.html");
			break;
		case "img":
			oPenWin("请输入图片地址", 380, 120, "/editor/InsertImg.html", 1, 1);
			break;
		case "flash":
			oPenWin("请输入图片地址", 300, 160, "/editor/InsertFlash.html");
			break;
		case "video":
			oPenWin("请输入图片地址", 300, 160, "/editor/InsertVideo.html");
			break;
		case "forecolor":
			oPenWin("请选择文本颜色", 140, 162, "/editor/ForeColor.html", true);
			break;
		case "hilitecolor":
			oPenWin("请选择背景颜色", 140, 162, "/editor/BackColor.html", true);
			break;
		case "formatblock":
			oPenWin("请选择段落风格", 160, 242, "/editor/FormatBlock.html", true);
			break;
		case "fontsize":
			oPenWin("请选择字体大小", 160, 186, "/editor/FontSize.html", true);
			break;
		case "fontname":
			oPenWin("请选择字体样式", 160, 280, "/editor/FontName.html", true);
			break;
		case "textarea":
			insetQUER();
			break;
		default:
			oRTE.focus();
			oRTE.document.execCommand(_sCmd, false, null);
			oRTE.focus();
			break;
	}
}
function insetQUER() {
	var _sVal;
	var rng = {};
	var oRTE = getFrameNode(sRTE);
	if (document.all) {
		var selection = oRTE.document.selection; 
		if (selection != null) {
			rng = selection.createRange();
		}
	} else {
		var selection = oRTE.getSelection();
		
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
		rng.text = rng.toString();
	}
	_sVal = rng.text == "" ? "请在文本框输入文字" : rng.text;
	var html = "<table style='border:1px solid #999;width:80%;font-size:12px;' align='center'><tr><td>"+ _sVal +"</td></tr></table>";
	
	insertHtml(html);
}
function publish_article(){
	OXEeditor.save();
}
function insertHtml(html) {
	
	//html = html.toLowerCase();
	var oRTE = getFrameNode(sRTE);
	
	if(window.Event){
		oRTE.document.execCommand('insertHTML', false, html);
	} else {
		oRTE.focus();
		var oRng = oRTE.document.selection.createRange();
		oRng.pasteHTML(html);
		oRng.collapse(false);
		oRng.select();
	}
}
