var sRTE;
var editpos;

function ged(id) { return document.getElementById(id)}
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 : ged(sNodeBox).contentDocument.designMode = sMode;
}
function display_switch(id,mode) {
	if(mode) {
		id.style.display=mode
	} else {
		if(id.style.display=="inline") id.style.display="none"
		else id.style.display="inline"
	}
}
function swid(name) {
	display_switch(ged(name),"none")
	var oRTE = getFrameNode(sRTE);
	if(document.all) {
		editpos.select();
	} else oRTE.focus()
}
function oInputFunc(sNode) {
	var oRTE = getFrameNode(sRTE);
	oRTE.focus();
	var sEditBox = document.getElementById(sNode + "_heditor_tool");
	var sEditMode = sEditBox.style.visibility == "hidden" ? true : false;
	if(sEditMode){
		sEditBox.style.visibility = "visible";
		docStyle = oRTE.document.getElementsByTagName("style");
		docStyle[0].disabled = "";
		if(window.Event){
			var oMozText = oRTE.document.body.ownerDocument.createRange();
			oMozText.selectNodeContents(oRTE.document.body);
			oRTE.document.body.innerHTML = oMozText.toString();
		}else{
			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{
		sEditBox.style.visibility = "hidden";
		var innerHTML = oRTE.document.body.innerHTML;
		docStyle = oRTE.document.getElementsByTagName("style");
		docStyle[0].disabled = "true";
		if(window.Event){
			var oMozText = oRTE.document.createTextNode(innerHTML);
			oRTE.document.body.innerHTML = "";
			oRTE.document.body.appendChild(oMozText);
		}else{
			oRTE.document.body.innerText = innerHTML;
		}
	}
}

function heditor_save(sNode) {
	var oRTE = getFrameNode(sRTE);
	ged(sNode).value=getFrameNode(sRTE).document.body.innerHTML
	//alert("save")
}

function heditor(sNode, sMode, sHeight,sWidth, sShowCode, sPath) {
	ged(sNode).style.display="none"
	  
	var oTool = new Object;
	var oArea= new Object
	var editor=document.createElement("div");
	editor.innerHTML='<TABLE class=editerToolsBox id='+sNode+'_heditor_table cellSpacing=0 cellPadding=0 width="'+sWidth+'" border=0>\
  <TR><TD><div class=editerToolsBG id="'+sNode+'_heditor_tool"></div></TD></TR>\
  <TR><TD class=editerTextBG><IFRAME class="editerTextArea" id='+sNode+'_heditor_area style="HEIGHT: '+sHeight+'px" frameBorder="0" width="100%" scrolling="yes"></IFRAME></TD></TR></TABLE>'
	ged(sNode).parentNode.appendChild(editor);

	sRTE = sNode + "_heditor_area";
	editor_tool=ged(sNode+'_heditor_tool')
	editor_area=ged(sNode+'_heditor_area')
	oArea=ged(sNode)

	oForm=oArea.form
	var orSubmit=oForm.submit
	if(window.addEventListener) {         //for FF.etc
		oForm.addEventListener("submit", function() {heditor_save(sNode);}, false);
	}     else {        //for IE
		oForm.attachEvent("onsubmit", function() {heditor_save(sNode);});
	}
/*
	if (! oForm.submitEditor) oForm.submitEditor = new Array() ;
	oForm.submitEditor[oForm.submitEditor.length] = function() {heditor_save(sNode)};
	if (! oForm.originalSubmit) {
		oForm.originalSubmit = oForm.submit ;
		oForm.submit = function() {
			if (this.submitEditor) {
				for (var i = 0 ; i < this.submitEditor.length ; i++) {
					alert(i)
					this.submitEditor[i]() ;
				}
			}
			this.originalSubmit() ;
		}
	}
*/ 
var tmp_val=ged(sNode).value;
oArea.value = ged(sNode).innerHTML;
// alert(ged(sNode).innerHTML);
 //var oRTE = getFrameNode(sRTE);
// oRTE.document.body.innerText ='111';
	
	 
  	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"}
	];

	for(var i = 0; i < posArr.length; i ++) {
		oTool = document.createElement("img");
		posArr[i]["id"] ? oTool.id = posArr[i]["id"] : function(){};
		oTool.src = sPath+"blank.gif";
		oTool.className = "editerToolsIMG";
		oTool.style.width = posArr[i]["w"] + "px";
		oTool.style.backgroundPosition = posArr[i]["x"] + "px 0px";
		if(posArr[i]["func"] != null) {
			oTool.title = posArr[i]["t"];
			oTool["func"] = posArr[i]["func"];
			oTool.onmouseover = function () {
				var bp = this.style.backgroundPosition;
				this.style.backgroundPosition = bp.split(" ")[0] + " -20px";
			}
			oTool.onmouseout = function () {
				var bp = this.style.backgroundPosition;
				this.style.backgroundPosition = bp.split(" ")[0] + " 0px";
			}
			oTool.onclick = function () {
				wordChiCommand(this["func"]);
			}
		}
		editor_tool.appendChild(oTool);
	}
	var ddiv=document.createElement('div')
	ddiv.id="editer_dg"
	ddiv.className="editerDiv"
	editor_tool.appendChild(ddiv);
	if(sShowCode) {
		editor_tb=ged(sNode+'_heditor_table')
		var newRow = editor_tb.insertRow(-1);
		var newcell=newRow.insertCell(-1);
		newcell.className='editerTextBottomBG'
		newcell.innerHTML = '<INPUT id='+sNode+'_heditor_check type=checkbox onclick="oInputFunc(\''+sNode+'\')"><LABEL style="FONT-SIZE: 12px" for='+sNode+'_heditor_check>显示源代码</LABEL>';

	}

	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><head><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: 'Tahoma','Courier New',  'Verdana', 'Courier';\n\
			}</style></head><body>" + sHTML + "</body></html>";
		}
	
		var oRTE = getFrameNode(sNodeBox).document;
		oRTE.open();
		oRTE.write(frameHTML);
		oRTE.close();
	}
	 //alert(tmp_val);
	writeDesignMode(sRTE, tmp_val, sMode);
	getFrameNode(sRTE).focus()
}

function GetPos() {
	if (document.selection) {
		editpos = getFrameNode(sRTE).document.selection.createRange();
	}
	else editpos = getFrameNode(sRTE).getSelection;
	//selectionStart
}

function insertHTML(html) {
	var oRTE = getFrameNode(sRTE);
	oRTE.focus();
	if(window.Event) {
		oRTE.document.execCommand('insertHTML', false, html);
	} else {
		var oRng = oRTE.document.selection.createRange();
		editpos.pasteHTML(html);
		editpos.collapse(false);
	}
	swid("editer_dg")
}
function insertHTML_link(html){
		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();
	}
	
	}
function insertLink() {
	var t = "_blank";
	var a = ged("wordEditer_Link_ADS").value;
	var v = ged("wordEditer_Link_TEXT").value;
	if(a == "") return;
	if(v == "") v = a;
	var html = "<a href='" + a + "' target=\"" + t + "\">" + v + "</a>";
	insertHTML(html)
}

function insetIMG(_sVal) {
	if(_sVal == "") return;
	var html = "<img alt='' src='" + _sVal + "' />";
	insertHTML(html);
}

function insetFlash() {
	var html = '<embed   src="'+ged("wordEditer_flash_url").value+'" width="'+ged("wordEditer_flash_width").value+'" height="'+ged("wordEditer_flash_height").value+'" id="test"></embed>'
	insertHTML(html);
}
function insetVideo() {
	var html = '<embed src="'+ged("wordEditer_video_url").value+'" type="application/x-mplayer2" border="0" width="'+ged("wordEditer_video_width").value+'" height="'+ged("wordEditer_video_height").value+'" showcontrols="1" showaudiocontrols="1" showstatusbar="1" autostart="1" loop="0" enablecontextmenu="1"></embed>'
	insertHTML(html);
}

function returnColor(_oObj){
	return _oObj.currentStyle ? _oObj.currentStyle['backgroundColor'] : window.getComputedStyle(_oObj, "")['backgroundColor'];
}
function setCMD( _sSort, _sColor){
	var oRTE = getFrameNode(sRTE);
	if(document.all) {
		editpos.select();
	}
	oRTE.document.execCommand(_sSort, false, _sColor);
	swid("editer_dg")
}
function reg2color(_sColor){
	if(_sColor.indexOf('rgb') > -1) {
		aColor = _sColor.replace("(","").replace(")","").replace("rgb","").split(", ");
		aColor[0] = (Math.abs(aColor[0])).toString(16);
		aColor[0] += aColor[0].length == 1 ? "0" : "";
		aColor[1] = (Math.abs(aColor[1])).toString(16);
		aColor[1] += aColor[1].length == 1 ? "1" : "";
		aColor[2] = (Math.abs(aColor[2])).toString(16);
		aColor[2] += aColor[2].length == 1 ? "2" : "";
		return "#" + aColor.join('');
	}else{
		return _sColor;
	}
}

function _over(o) {
	o.className="Aover";
}
function _out(o) {
	o.className="Aout";
}
function wordChiCommand(_sCmd) {
	var oRTE = getFrameNode(sRTE)
	oRTE.focus()
	oRTE.blur=GetPos();
	
	var d_id=ged("editer_dg")
	var d_left_bf=d_id.style.left

	switch(_sCmd){
		case "":
			break;
		case "link":
			d_id.style.width="300px";d_id.style.height="120px";d_id.style.left="223px"
			d_id.innerHTML='<table border="0" width="100%" cellpadding="0" cellspacing="0" align="center">\
  <tr><td colspan=2 height="26">请输入链接地址和文字：</td></tr>\
  <tr><td height="26">链接地址:</td><td><input size="35" id="wordEditer_Link_ADS" value="http://"></td></tr>\
  <tr><td height="26">链接文字:</td><td><input size="35" id="wordEditer_Link_TEXT"></td></tr>\
  <tr><td align="center" height="30" colspan="2"><input type="button" onclick="insertLink();" value="确定" /> <input type="button" onclick="swid(\'editer_dg\');" value="取消" /></td></tr></table>';
			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();
			}
			ged('wordEditer_Link_TEXT').value = rng.text;
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "table":
			display_switch(d_id,"none")
			break;
		case "img":
			d_id.style.width="320px";d_id.style.height="100px";d_id.style.left="159px"
			d_id.innerHTML='<table border="0" cellpadding="3" width="100%" cellspacing="3">\
  <tr><td>图片网址: <input size="44" id="wordEditer_IMG_SRC" value="http://"></td></tr>\
  <tr><td align="center"><input type="button" onclick="insetIMG(document.getElementById(\'wordEditer_IMG_SRC\').value)" value="确定" /> <input type="button" onclick="swid(\'editer_dg\');" value="取消" /></td></tr></table>';
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "flash":
			d_id.style.width="300px";d_id.style.height="160px";d_id.style.left="179px"
			d_id.innerHTML='<table border="0" cellpadding="0" cellspacing="4" align="center">\
  <tr><td>Flash网址(.swf文件):<br><input id="wordEditer_flash_url" size="45" onmouseover="this.select()" value="http://"></td></tr>\
  <tr><td> 宽度:<input size="5" id="wordEditer_flash_width" value="600"> 高度:<input size="5" id="wordEditer_flash_height" value="450"></td><td></td></tr>\
  <tr><td align="center" colspan="2"><input type="button" onclick="insetFlash()" value="确定" /> <input type="button" onclick="swid(\'editer_dg\')" value="取消" /></td></tr></table>';
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "video":
			d_id.style.width="300px";d_id.style.height="160px";d_id.style.left="199px"
			d_id.innerHTML='<table border="0" cellpadding="0" cellspacing="4" align="center">\
  <tr><td>视频URL链接(wm*,avi,mpeg文件):<br><input id="wordEditer_video_url" size="45" value="http://"></td></tr>\
  <tr><td> 宽度:<input size="5" id="wordEditer_video_width" value="400"> 高度:<input size="5" id="wordEditer_video_height" value="300"></td><td></td></tr>\
  <tr><td align="center" colspan="2"><input type="button" onclick="insetVideo()" value="确定" /> <input type="button" onclick="swid(\'editer_dg\')" value="取消" /></td></tr></table>';
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "forecolor":
			d_id.style.width="135px";d_id.style.height="140px";d_id.style.left="293px"
			var k = 1;var s=''
			for(var i = 0; i < 6; i ++) {
				for(var j = 0; j < 6; j++) {
					s=s+'<input type="button" id="editerBN" onclick="setCMD(\'ForeColor\' ,reg2color(returnColor(this)))" class="C' + k + '" />';k ++;
				}
			}
			d_id.innerHTML=s;
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "hilitecolor":
			d_id.style.width="135px";d_id.style.height="140px";d_id.style.left="293px"
			var k = 1;var s=''
			for(var i = 0; i < 6; i ++) {
				for(var j = 0; j < 6; j++) {
					s=s+'<input type="button" id="editerBN" onclick="setCMD(\'BackColor\' ,reg2color(returnColor(this)))" class="C' + k + '" />';k ++;
				}
			}
			d_id.innerHTML=s;
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "formatblock":
			break;
		case "fontsize":
			d_id.style.width="100px";d_id.style.height="170px";d_id.style.left="380px"
			d_id.innerHTML='  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontSize\', \'1\')">10 px</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontSize\', \'2\')">14 px</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontSize\', \'3\')">16 px</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontSize\', \'4\')">18 px</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontSize\', \'5\')">24 px</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontSize\', \'6\')">32 px</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontSize\', \'7\')">48 px</div>';
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "fontname":
			d_id.style.width="160px";d_id.style.height="250px";d_id.style.left="340px"
			d_id.innerHTML='  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'黑体\')" style="font-family:黑体;">黑体</div><div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'隶书\')" style="font-family:隶书;">隶书</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'宋体\')" style="font-family:宋体;">宋体</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'楷体_GB2312\')" style="font-family:楷体_GB2312;">楷体</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'幼圆\')" style="font-family:幼圆;">幼圆</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'Arial\')" style="font-family:Arial;">Arial</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'Courier New\')" style="font-family:Courier New;">Courier New</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'Georgia\')" style="font-family:Georgia;">Georgia</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'Times New Roman\')" style="font-family:Times New Roman;">Times New Roman</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'Verdana\')" style="font-family:Verdana;">Verdana</div>\
  <div class="Aout" onmouseover="_over(this)" onmouseout="_out(this)" onclick="setCMD(\'FontName\', \'impact\')" style="font-family:impact;">impact</div>';
			if(d_left_bf==d_id.style.left) display_switch(d_id)
			else display_switch(d_id,"inline")
			break;
		case "textarea":
			display_switch(d_id,"none")
			insetQUER();
			break;
		default:
			display_switch(d_id,"none")
			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 allo(obj) {      
    // 用来保存所有的属性名称和值     
    var props = "";     
    // 开始遍历     
    for(var p in obj){      
       // 方法     
            // p 为属性名称，obj[p]为对应属性的值     
            props+= p + "=" + obj[p] + "\t";     
    }      
    // 最后显示所有的属性     
    alert(props);     
}
