﻿var charset="utf-8";
var isexpand=false;
var mode=1;
var irange=null;
var current=null;
var ie=window.ActiveXObject?true:false;
function insert(a,b){
	var s = editor.window.getSelection() ;
	for ( var i = 0 ; i < s.rangeCount ; i++ ) s.getRangeAt(i).deleteContents() ;
	var r = s.getRangeAt(0) ;
	if(b)r.insertNode(b) ;
	if(a)r.insertNode(a) ;
	editor.focus();
	hideit();
	editor.document.designMode="On";
}
function paste(s){
	editor.focus();
	if(irange) irange.pasteHTML(s);
	else editor.document.selection.createRange().pasteHTML(s);
	hideit();
}
function edit_init(charset){
	if (charset!=null) charset="utf-8";
	
	editor.document.designMode="On";
	editor.document.contentEditable = true;
	editor.document.open();
	editor.document.write ('<html><head><style type=text/css>img{border:none}\n.quote{border:1px solid #ccc; background:#f4f4f4 }\np{margin:10px 0px;line-height:140%}\ntable{border:1px solid #ccc;border-collapse:collapse}\ntd{border:1px solid #ccc;padding:5px}\nbody,td,font{font:12px 宋体}\n.page_spacer{width:100%;height:15px;overflow:hidden;background:url(/editor/ico/page_spacer.gif) no-repeat}</style></head><body bgcolor="#ffffff"></body></html>');
	editor.document.close();
	
	editor.document.charset="utf-8";
	if(ie) editor.document.attachEvent("onmouseup",select);
	else editor.document.addEventListener("mouseup",function(event){select(event)},true);
	edit_style();
	apply_swap("tdw");
	apply_swap("tdh");
}
function edit_style(){
	var bs = editor.document.body.style;
	bs.fontFamily="Verdana";
	bs.fontSize="12px";
	bs.lineHeight="140%";
	bs.scrollbar3dLightColor= 'white';
	bs.scrollbarArrowColor= '#f60';
	bs.scrollbarBaseColor= 'white';
	bs.scrollbarDarkShadowColor= 'white';
	bs.scrollbarFaceColor= 'white';
	bs.scrollbarHighlightColor= '#ccc';
	bs.scrollbarShadowColor= '#ccc';
	bs.scrollbarTrackColor= '#f8f8f8';
	bs.border='0';
}
function edit_setmode(o){
	hideit();
	if(mode==1){
		if (ie) editor.document.body.innerText=editor.document.body.innerHTML;
		else{								
			var s=document.createTextNode(editor.document.body.innerHTML);
			editor.document.body.innerHTML = "";
			editor.document.body.appendChild(s);
		}
	}
	else{
		if (ie) editor.document.body.innerHTML=editor.document.body.innerText;
		else{
			var s = editor.document.body.ownerDocument.createRange();
			s.selectNodeContents(editor.document.body);
			editor.document.body.innerHTML = s.toString();
		}
	}
	editor.document.body.style.fontSize=(mode==2&&isexpand)?"14px":"12px";
	o.checked=mode==1;
	document.getElementById("tool_htmltext").style.color=mode==1?"black":"#999";
	mode=mode==1?2:1;
	editor.document.body.style.backgroundColor=mode==1?"white":"#f1f4f8";
	editor.document.designMode="On";
	editor.document.contentEditable = true;
}
function valid(){
	if (mode==1) return true;
	alert("源代码模式下不能使用该功能！");
	editor.focus();
	return false;
}
function select(e){
	if(!ie) current=e.explicitOriginalTarget;
	else if(editor.document.selection.type == "Control") current=editor.document.activeElement;
	else current= editor.document.selection.createRange().parentElement();
	apply_action();
}
function apply_swap(n){
	var img = new Image();
	img.src="/editor/tool/tleft_red.gif";
	img.src="/editor/tool/tright_red.gif";
	img.src="/editor/tool/tup_red.gif";
	img.src="/editor/tool/tdown_red.gif";
	img.src="/editor/tool/tdel_red.gif";
	l=document.getElementById(n).getElementsByTagName("IMG");
	for(var i=0;i<l.length;i++){
		l[i].style.cursor="pointer";
		l[i].onmouseover=function(){if(this.src.indexOf("_red")==-1)this.src=this.src.replace(".gif","")+"_red.gif"};
		l[i].onmouseout=function(){this.src=this.src.replace("_red","")};
		if(n=="tdh")l[i].style.display="block";
	}
}
function apply_action(){
	hide_tdtool();
	if(current==null) return;
	switch(current.tagName)
	{	case "TD":
			if(!ie) break;
			var f=document.getElementById("edit_frame");
			var p=document.getElementById("tdw");
			p.style.left = (left(current)+left(f)+(current.offsetWidth/2)-10) + "px";
			p.style.top = (top(current)+top(f)-6) + "px";
			p.style.display="block";
			p=document.getElementById("tdh");
			p.style.left = (left(current)+left(f)-8) + "px";
			p.style.top = (top(current)+top(f)+(current.offsetHeight/2)-8) + "px";
			p.style.display="block";
			break;
	}
}
function add_col(d){
	if(current==null || current.tagName!="TD") return;
	var o=current.parentNode,t=o.parentNode,index=0;
	for(var i=0;i<o.cells.length;i++){
		if(o.cells[i]==current){ index=i; break;} 
	}
	for(var i=0;i<t.rows.length;i++){
		t.rows[i].insertCell(d==1?index:index+1);
	}
	apply_action();
}
function add_row(d){
	if(current==null || current.tagName!="TD") return;
	var o=current.parentNode,t=o.parentNode,tr=null;
	for(var i=0;i<t.rows.length;i++){
		if(t.rows[i]==o){
			tr=t.insertRow(d==1?i:i+1);
			break;
		}
	}
	if(tr==null) return;
	for(var i=0;i<o.cells.length;i++){
		tr.insertCell();
	}
	apply_action();
}
function del_col(){
	var o=current.parentNode,t=o.parentNode,index=0;
	for(var i=0;i<o.cells.length;i++){
		if(o.cells[i]==current){ index=i; break;} 
	}
	for(var i=0;i<t.rows.length;i++){
		t.rows[i].deleteCell(index);
	}
	current=null;
	hide_tdtool();
}
function del_row(){
	if(current==null || current.tagName!="TD") return;
	var o=current.parentNode,t=o.parentNode,tr=null;
	for(var i=0;i<t.rows.length;i++){
		if(t.rows[i]==o){
			tr=t.deleteRow(i);
			break;
		}
	}
	current=null;
	hide_tdtool();
}
function hide_tdtool(){
	document.getElementById("tdw").style.display="none";
	document.getElementById("tdh").style.display="none";
}
function img(s,n,w,h,a){
	if(document.all){
		var s="<img"+(!a||a=="left"?" align=absmiddle":"")+" src='"+s+"'"+(isNaN(w)?"":" width='"+w+"'")+(isNaN(h)?"":" height='"+h+"'")+(n?" alt=\""+n.replace("\"","")+"\"":"")+">";
		if(a=="center") s="<p align=center>"+s+"</p>"+(n?"<p align=center>"+n+"</p>":"");
		else if(!n) s="<p align="+a+">"+s+"</p>";
		else if (!a||a=="left") s="<p align=left>"+s+" "+n?n:""+"</p>";
		else s="<p align="+a+">"+n+" "+s+"</p>";
		paste(s);
	}
	else{
		var o=document.createElement("IMG");
		o.src=s;
		if(!isNaN(w)) o.width=w;
		if(!isNaN(h)) o.height=h;
		if(!n) o.alt=n;
		var p=document.createElement("P");
		p.align=a;
		if(!a||a=="left"){
			o.setAttribute("align","absmiddle");
			if(n) o.setAttribute("alt",n);
			p.appendChild(o);
			if(n) p.appendChild(document.createTextNode(" "+n));
			parent.insert(p);
		}
		else if(a=="right"){
			if(n) p.appendChild(document.createTextNode(n+" "));
			p.appendChild(o);
			parent.insert(p);
		}
		else{
			p.appendChild(o);
			var note=document.createElement("P");
			note.align=a;
			note.appendChild(document.createTextNode(n));
			insert(p,note);
		}
	}
}
function swf(s,n,w,h){
if(document.all){
	var p=(w?" width=\""+w+"\"":"")+(h?" height=\""+h+"\"":"");
	var s="<p align=center><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\""+p+"><param name=\"movie\" value=\""+s+"\"><param name=\"quality\" value=\"high\"><param name=\"scale\" value=\"exactfit\"><param name=\"wmode\" value=\"window\"><embed src=\""+s+"\""+p+" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" scale=\"exactfit\"></embed></object></p>";
	if(n) s+="<p align=center>"+n+"</p>";
	paste(s);
}
else{
		var p=document.createElement("P");
		p.align="center";
		var o=document.createElement("OBJECT");
		o.setAttribute("classid","clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
		o.setAttribute("codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0");
		if(w) o.setAttribute("width",w);
		if(h) o.setAttribute("height",h);
		var m=document.createElement("PARAM");
		m.name="movie";
		m.value=s;
		o.appendChild(m);
		m=document.createElement("PARAM");
		m.name="quality";
		m.value="high";
		o.appendChild(m);
		m=document.createElement("PARAM");
		m.name="scale";
		m.value="exactfit";
		o.appendChild(m);
		m=document.createElement("PARAM");
		m.name="wmode";
		m.value="window";
		o.appendChild(m);
		m=document.createElement("EMBED");
		m.setAttribute("src",s);
		m.setAttribute("quality","high");
		m.setAttribute("pluginspage","http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash");
		m.setAttribute("scale","exactfit");
		m.setAttribute("value","high");
		o.appendChild(m);
		p.appendChild(o);
		var note=document.createElement("P");
		note.align="center";
		note.appendChild(document.createTextNode(n));
		insert(p,note);
}
}
function clean(){
	editor.focus();
	if (ie){
		var body = editor.document.body;
		for (var index = 0; index < body.all.length; index++) {
			tag = body.all[index];
			tag.removeAttribute("className","",0);
			tag.removeAttribute("style","",0);
		}
		var html = editor.document.body.innerHTML;
		html = html.replace(/\<p>/gi,"[$p]");
		html = html.replace(/\<\/p>/gi,"[$\/p]");
		html = html.replace(/\<br>/gi,"[$br]");
		html = html.replace(/\<img\s+/gi,"[$img ");
		html = html.replace(/\<[^>]*>/g,"");        ///过滤其它所有"<...>"标签
		html = html.replace(/\[\$p\]/gi,"<p>");
		html = html.replace(/\[\$\/p\]/gi,"<\/p>");
		html = html.replace(/\[\$br\]/gi,"<br>");
		html = html.replace(/\[\$img\s+/gi,"<img ");
		editor.document.body.innerHTML = html;
	}
	else{
		var s = editor.document.body.ownerDocument.createRange();
		s.selectNodeContents(editor.document.body);
		editor.document.body.innerHTML = s.toString();
	}
}
function clean_word(html)
{
	html = html.replace(/<\/?SPAN[^>]*>/gi, "");
	html = html.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	html = html.replace(/<(\w[^>]*) style="([^"]*)"([^>]*)/gi, "<$1$3") ;
	html = html.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3") ;
	html = html.replace(/<\\?\?xml[^>]*>/gi, "") ;
	html = html.replace(/<\/?\w+:[^>]*>/gi, "") ;
	html = html.replace(/&nbsp;/, " " );
	var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ; // Different because of a IE 5.0 error
	html = html.replace( re, "<div$2</div>");
	return html;
}
function check_empty(html)
{
	html = html.replace(/\<[^>]*>/g,"");        ///过滤其它所有"<...>"标签
	html = html.replace(/&nbsp;/gi, "");
	html = html.replace(/o:/gi, "");
	html = html.replace(/\s/gi, "");
	return html;
}
function format(cmd, option){
	if (!valid()) return;
	editor.focus();
	editor.document.execCommand(cmd, false, option);
	editor.focus();
}

function hideit(n){
	var s=['color','emot','table','file','simple'];
	for(var i=0;i<s.length;i++) if(s[i]!=n&&document.getElementById("pop_"+s[i])) document.getElementById("pop_"+s[i]).style.display="none";
	if(n=="file")document.getElementById("pop_file").src="/editor/inc/file.html";
	if(n=="simple")document.getElementById("pop_simple").src="/editor/inc/file_simple.html";
}
function popup(o,n){
	hideit(n);
	if (!valid()) return;
	var p=document.getElementById("pop_"+n);
	if(!p) return;
	p.style.left = left(o) + "px";
	p.style.top = (top(o) + o.offsetHeight) + "px";
	p.style.display=p.style.display=="block"?"none":"block";
	irange=null;
	editor.focus();
	var sel = editor.document.selection; 
	if(sel) irange =sel.createRange();
}
function edit_page(){
	var s='<p class=page_spacer></p>';
	editor.focus();
	if(ie)editor.document.selection.createRange().pasteHTML(s);
	else parent.editor.document.body.innerHTML+=s;
}
function edit_link(){
	if (!valid()) return;
	if(ie) editor.document.execCommand('CreateLink', true);
	else{
		var s=prompt("请填写超级链接地址信息：", "http://");
		editor.focus();
		if (s!="" && s!="http://") editor.document.execCommand('CreateLink', false, s);
	}
}
function show_select(b,o){
	if(!F.ie)return;
	var l=(o||document).getElementsByTagName("SELECT");
	for(var i=0;i<l.length;i++)l[i].style.display=(b?"":"none")
}
function expand(b){
	hideit();
	isexpand=b;
	var o=document.getElementById("editor_div");
	o.style.position=b?"absolute":"relative";
	if(!b){
		o.style.width="100%";
		o.style.height="100%";
		show_select(true);
	}
	else {
		o.style.width=(ie?screen.width-21:screen.width-17)+"px";
		o.style.height="3000px";
		show_select(false);
	}
	//editor.document.body.style.fontSize=(mode==1&&b)?"14px":"12px";
	window.scroll(0,0);
}

function top(o) {
	var t = o.offsetTop,p = o.offsetParent;
	while(p){
		t += p.offsetTop;
		p = p.offsetParent;
	}
	return t;
}

function left(o) {
	var l = o.offsetLeft,p = o.offsetParent;
	while(p) {
		l += p.offsetLeft;
		p = p.offsetParent;
	}
	return l;
}

function rand() {	return parseInt((1000)*Math.random()+1);}

function setHTML(s){
	if(!s)s="";
	if(mode==1) return editor.document.body.innerHTML=s;
	if(ie) return editor.document.body.innerText=s;
	return editor.document.body.innerHTML=s.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}
function getHTML() {
	var s="";
	if(mode==1) s=editor.document.body.innerHTML;
	else if(ie) s=editor.document.body.innerText;
	else s=editor.document.body.innerHTML.replace(/&amp;/g,"&").replace(/&quot;/g,'"').replace(/&#39;/g,"'").replace(/&lt;/g,"<").replace(/&gt;/g,">");
	return s.replace(/<p\s*(align=(left|center|right)|style="text-align: (left|center|right);")?><img src="http:\/\/7735\.com\.cn\/editor\/pic\/ico\/page_spacer\.gif"><\/p>/gi,"<p><img src=\"/editor/ico/page_spacer.gif\"></p>").replace(/&nbsp;/g," ");
}
