// JavaScript Document
//新版本编辑器，TOM bbs copyright.
//-------------
//2007-11-8 C:hanshu
//
//=============

var sRTE;
var base="/script/editor/";
var editor_code   = 'editor_code';
var editor_design = 'editor_design';

function word(sNodeBox, sHTML, is_video , is_audio, active_div){
    
    editor_code   = active_div+'editor_code';
    editor_design = active_div+'editor_design'; 
    
    //当前设计模式 id
    var flag=editor_design;
    //当前代码模式
    var curr_code = active_div+'editor_code';


    if((typeof(is_video) == undefined) && (is_video == null))   is_video = 0;
    if((typeof(is_audio) == undefined) && (is_audio == null))   is_audio = 0;
    
    if(typeof(active_div) == null || active_div == undefined){
        active_div = 'main';
    }

   //写道 main 这个层里
   var main_embed = document.getElementById(active_div);
   if(main_embed == null){
        tom.Alert('编辑器要有一个叫 '+active_div+' 的层');
        return false;
   }






   //内结构
   //临时变量
   var tmp1 = Object;
   var tmp2 = Object;
   var tmp3 = Object; 
   var tmp4 = Object; 
   var tmp5 = Object; 
   var tmp6 = Object; 
   //--------------------------------

   //控制按钮屏蔽层
   var mask_control_buttons = document.createElement('div');
   mask_control_buttons.id  = flag+'editor_unactive_tools_bar';
   mask_control_buttons.className = 'disable';
   mask_control_buttons.style.display = 'none';

   main_embed.appendChild(mask_control_buttons);

   //控制按钮
   //var control_buttons = document.createElement('div');
       //control_buttons.id = 'editor_active_tools_bar';
       //control_buttons.className = 'edit';

   //工具栏
   var tools_bar = document.createElement('div');
       tools_bar.id            = flag+'editor_active_tools_bar';
       tools_bar.className     = 'function';
       tools_bar.style.display = 'block';
       
       //左边三大按钮,copy,paste,xcopy
       tmp1 = document.createElement('div');
       tmp1.className = 'functionleft';

       tmp2 = document.createElement('div');
       tmp2.className = 'leftcol';

           tmp3 = document.createElement('div');
           //paste
           tmp3.appendChild(this.set_button('a' , '粘贴' , 'clay' , 'paste' , flag));

       
           tmp4 = document.createElement('div');
           tmp4.className = 'clright';
           //cut
           tmp4.appendChild(this.set_button('a' , '剪切' , 'cut' , 'cut'  , flag)); 
           tmp4.appendChild(this.set_button('a' , '复制' , 'copy' , 'copy' , flag));

       tmp2.appendChild(tmp3);
       tmp2.appendChild(tmp4);
       
       tmp1.appendChild(tmp2);
       
       tools_bar.appendChild(tmp1);
       
       //释放这些按钮
       this.free(tmp1,tmp2,tmp3,tmp4);
       
       
       //右边一些零碎的按钮
       tmp1 = document.createElement('div');
       tmp1.className = 'functionright';

            //===================================
            //插入表情、插入图片、图片排版那层
            tmp2 = document.createElement('div');
            tmp2.className = 'panel1';
            //插入图片
            tmp2.appendChild(this.set_button('a' , '插入图片' , 'cutpic' , 'img' , flag));
            //插入表情
            tmp2.appendChild(this.set_button('a' , '插入表情' , 'cutface' , 'emotion', flag));
            //图片排版
            tmp2.appendChild(this.set_button('a' , '图片排版' , 'cutpaper' , 'image_type', flag));
            
            
            tmp1.appendChild(tmp2);
            this.free(tmp2);

                    
            //====================================
            //插入链接、删除连接那层
            tmp2 = document.createElement('div');
            tmp2.className = 'panel2';
            //插入连接
            tmp2.appendChild(this.set_button('a' , '插入链接' , 'cuturl' , 'link', flag));
            //删除连接
            tmp2.appendChild(this.set_button('a' , '删除链接' , 'delurl' , 'unlink', flag));
            tmp1.appendChild(tmp2);
            this.free(tmp2);

                    
                     
            //====================================
            //撤销、重做、清除格式那层
            tmp2 = document.createElement('div');
            tmp2.className = 'panel3';
            //撤销
            tmp2.appendChild(this.set_button('a' , '撤销' , 'repeal' , 'Undo', flag));
            //重做
            tmp2.appendChild(this.set_button('a' , '恢复' , 'rework' , 'Redo', flag));

            //清除格式
            tmp2.appendChild(this.set_button('a' , '清除格式' , 'delfomate' , 'removeformat', flag));
            tmp1.appendChild(tmp2);
            this.free(tmp2);

            
            //====================================
            //插入自动排版
            tmp2 = document.createElement('div');
            tmp2.className = 'panel4';
			

			tmp2.appendChild(this.set_button('a' , '自动排版' , 'cutpaper' , 'format', flag));
            tmp1.appendChild(tmp2);
            this.free(tmp2);
                     
                     
            //====================================
            //字体、字体大小、背景色那层
            tmp2 = document.createElement('div');
            tmp2.className = 'panel1';
            
            //字体
            tmp2.appendChild(this.set_button('a' , '字体名称' , 'cutfont' , 'fontname', flag));
            
            //字体大小
            tmp2.appendChild(this.set_button('a' , '字体大小' , 'cutsize' , 'fontsize', flag));

            //字体颜色
            tmp2.appendChild(this.set_button('a' , '字体颜色' , 'cutcolor' , 'forecolor', flag));

            //背景色
            tmp2.appendChild(this.set_button('a' , '背景色' , 'cutbk' , 'hilitecolor', flag));
            tmp1.appendChild(tmp2);
            this.free(tmp2);


            //====================================
            //粗体、斜体、下划线那层
            tmp2 = document.createElement('div');
            tmp2.className = 'panel2';
            
            //粗体
            tmp2.appendChild(this.set_button('a' , '粗体' , 'addwide' , 'Bold', flag));
            
            //斜体
            tmp2.appendChild(this.set_button('a' , '斜体' , 'addslan' , 'Italic', flag));

            //下划线
            tmp2.appendChild(this.set_button('a' , '下划线' , 'underline' , 'Underline', flag));
            tmp1.appendChild(tmp2);
            this.free(tmp2);


            //====================================
            //左对齐、右对齐、减少缩进、增加缩紧 那层
            tmp2 = document.createElement('div');
            tmp2.className = 'panel3';
            
            //左对齐
            tmp2.appendChild(this.set_button('a' , '左对齐' , 'sleft' , 'justifyleft', flag));
            
            //居中
            tmp2.appendChild(this.set_button('a' , '中对齐' , 'scenter' , 'justifycenter', flag));
             
            //右对齐
            tmp2.appendChild(this.set_button('a' , '右对齐' , 'sright' , 'justifyright', flag));

            //减少缩进
            tmp2.appendChild(this.set_button('a' , '减少缩进' , 'cutindent' , 'outdent', flag));

            //增加缩进
            tmp2.appendChild(this.set_button('a' , '增加缩进' , 'addindent' , 'indent', flag));


            tmp1.appendChild(tmp2);
            this.free(tmp2);


           

/*
            tmp2 = document.createElement('div');
            tmp2.className = 'panel4';
            
            
            


            tmp1.appendChild(tmp2);
            this.free(tmp2);
  */   
            //====================================
            //添加音乐、添加动画、添加视频 那层
            tmp2 = document.createElement('div');
            tmp2.className = 'panel5';


			if(parseInt(is_audio)){
                //添加音乐
                tmp2.appendChild(this.set_button('a' , '插入' , 'addmusic' , 'audio', flag));
            }

            
            if(parseInt(is_video)){
                //添加动画
                tmp2.appendChild(this.set_button('a' , '插入' , 'addflash' , 'flash', flag));
             
                //添加视频
                tmp2.appendChild(this.set_button('a' , '插入' , 'addvideo' , 'video', flag));
            }


            tmp1.appendChild(tmp2);
            this.free(tmp2);

     tools_bar.appendChild(tmp1);
     this.free(tmp1);
     main_embed.appendChild(tools_bar);
     //control_buttons.appendChild(tools_bar);
     
     //中间的空结构   
     //tmp1 = document.createElement('div');
     //tmp1.className = 'panel5';

     //control_buttons.appendChild(tmp1);

    
    if(document.getElementById(flag+'box')==null){
        //增加显示辅助层
        var assist = document.createElement("div");
        assist.id  = flag+'box';
        assist.className  = 'box';
        assist.onmouseout = function(){
            //motl you are so BT and unformaty.
            //this.className = 'box';
        }

        //辅助层内镶 iframe
        var assist_iframe  = document.createElement('iframe');
        assist_iframe.name = flag+'box_iframe'
        assist_iframe.id   = flag+'box_iframe';
        assist_iframe.frameBorder = "0";

        
        assist.appendChild(assist_iframe);
        //control_buttons.appendChild(assist);
        main_embed.appendChild(assist);

    }



     //插入底下的内容
     tmp1 = document.createElement('div');
     tmp1.className = 'pattern';
            
            //上头标签那块
            tmp2 = document.createElement('div');
            tmp2.className = 'tabnav';
       
                tmp3 = document.createElement('div');
                tmp3.id = flag+'tb1';
                tmp3.className = 'tabon';
                //tmp3.onclick  =  word.swap_mode();
                tmp3.innerHTML = '可视模式';

                tmp2.appendChild(tmp3);
                this.free(tmp3);
                
                
                tmp3 = document.createElement('div');
                tmp3.id = flag+'tb2';
                tmp3.className = 'tabout';
                //tmp3.onclick   = word.swap_mode();
                tmp3.innerHTML = '代码模式';

                tmp2.appendChild(tmp3);
                this.free(tmp3,tmp4);
       
     tmp1.appendChild(tmp2);
     this.free(tmp2);
            
            
            //代码框
            tmp2 = document.createElement('div');
            tmp2.className = 'ptcnt';

                tmp3 = document.createElement('div');
                tmp3.className = 'workarea';
                tmp3.style.display = 'block';
                tmp3.id = flag+'tp1'; 
                
                //编辑模式的 iframe
                tmp4 = document.createElement('iframe');
                tmp4.id   = editor_design;
                tmp4.name = editor_design;
                tmp4.style.display = 'block';
                tmp4.style.height  = '258px';
                tmp4.style.width   = '100%';
                tmp4.style.cssFloat   = 'left';
                tmp4.frameBorder  = '0';                 
                
                tmp3.appendChild(tmp4);
                //<iframe id='edit_design' style='display:block;height:100%;width:100%;'></iframe>
                
                tmp2.appendChild(tmp3);
                this.free(tmp3,tmp4);


                tmp3 = document.createElement('div');
                tmp3.className = 'workarea';
                tmp3.style.display = 'none';
                tmp3.id = flag+'tp2'; 
                
                //<textarea name='' id='edit_code' style='display:none;height:255px;width:632px;'>Fesco</textarea>
                tmp4 = document.createElement('textarea');
                tmp4.id = curr_code;
                tmp4.value = sHTML;
                tmp4.name= sNodeBox;
                tmp4.style.display = 'none';
                tmp4.style.height  = '255px';
                tmp4.style.width   = '593px';
                tmp4.style.border  = '0';
               
                tmp3.appendChild(tmp4);
                //tmp3.innerHTML = '<textarea name="'+sNodeBox+'" id=\'editor_code\' style=\'display:none;height:255px;width:593px;\'></textarea>';
       
                tmp2.appendChild(tmp3);
                
                
                this.free(tmp3,tmp4);



    tmp1.appendChild(tmp2);
    this.free(tmp2);
    
    //control_buttons.appendChild(tmp1);
    //tools_bar.appendChild(tmp1);
    //this.free(tmp1);
    
    //tmp1 = document.createElement('div');
    //tmp1.className = 'clear';
    
    //control_buttons.appendChild(tmp1);
    //this.free(tmp1);
  
    main_embed.appendChild(tmp1);
    this.free(tmp1);
    
    
  
    //上下扩大缩小的按钮
    //<div class="button2"><button class="bottom"></button><button class="top"></button></div>
    tmp1 = document.createElement('div');
    tmp1.className = 'button2';

    //缩小按钮
    
    tmp2 = document.createElement('button');
    tmp2.title     = '缩小编辑器'; 
    tmp2.className = 'bottom';
    tmp2.style.cursor = 'pointer';
    tmp2.onclick = function(){
        //258,255
        if(parseInt(document.getElementById(flag).style.height)<308){
            document.getElementById(flag).style.height = 258;
        }else{
            document.getElementById(flag).style.height=parseInt(document.getElementById(flag).style.height)-50;
        }
        
        if(parseInt(document.getElementById(flag).style.height)<305){
            document.getElementById(editor_code).style.height = 255;
        }else{
            document.getElementById(editor_code).style.height=parseInt(document.getElementById(editor_code).style.height)-50;
        }

        
    }
    //tmp1.appendChild(tmp2);
    this.free(tmp2);

    //扩大按钮
    tmp2 = document.createElement('button');
    tmp2.title     = '扩大编辑器'; 
    tmp2.className = 'top';
    tmp2.style.cursor = 'pointer';
    tmp2.onclick =  function(){
        //258,255
        if(parseInt(document.getElementById(flag).style.height)<208){
            document.getElementById(flag).style.height = 258;
        }else{
            document.getElementById(flag).style.height=parseInt(document.getElementById(flag).style.height)+50;
        }
        
        if(parseInt(document.getElementById(editor_code).style.height)<205){
            document.getElementById(editor_code).style.height = 255;
        }else{
            document.getElementById(editor_code).style.height=parseInt(document.getElementById(editor_code).style.height)+50;
        }
    
    }

    
    //tmp1.appendChild(tmp2);
    this.free(tmp2);

    main_embed.appendChild(tmp1);
    this.free(tmp1);

   //假按钮层
   /*
   var control_disable_buttons = control_buttons;
   control_disable_buttons.id            = 'editor_unactive_tools_bar';
   control_disable_buttons.className     = 'edit';
   control_disable_buttons.style.display = 'none';

   main_embed.appendChild(control_disable_buttons);
    */



    document.getElementById('debuging').value += main_embed.innerHTML;

    document.getElementById(flag+'tb1').onclick = function(){
        //tb1=table of view
        //tb2=table of code
        
        //tp1=iframe of designMode
        //tp2=textarea of codeMode
        var is_design_mode = (document.getElementById(flag+'tb1').className == 'tabon');
        if(!is_design_mode){
            //enable the tools bar,first
            document.getElementById(flag+'editor_active_tools_bar').style.display = 'block';
            document.getElementById(flag+'editor_unactive_tools_bar').style.display = 'none';
            
        
            
            document.getElementById(flag+"tp2").style.display="none";
            document.getElementById(flag+"tp1").style.display="block";
            document.getElementById(flag+"tb1").className="tabon";
            document.getElementById(flag+"tb2").className="tabout";
            document.getElementById(curr_code).style.display="none";
            document.getElementById(flag).style.display="block";
            
            getFrameNode(flag).document.body.innerHTML = document.getElementById(curr_code).value;
            getFrameNode(flag).focus();
        }
         
        
        return false

    };

    
    document.getElementById(flag+'tb2').onclick = function(){
     //tb1=table of view
        //tb2=table of code
        
        //tp1=iframe of designMode
        //tp2=textarea of codeMode

        var is_design_mode = (document.getElementById(flag+'tb1').className == 'tabon');
        if(is_design_mode){
        
            //disable the tools bar,first
            document.getElementById(flag+'editor_active_tools_bar').style.display     = 'none';
            document.getElementById(flag+'editor_unactive_tools_bar').style.display = 'block';
 

            document.getElementById(flag+"tp2").style.display="block";
            document.getElementById(flag+"tp1").style.display="none";
            document.getElementById(flag+"tb1").className="tabout";
            document.getElementById(flag+"tb2").className="tabon";
            document.getElementById(curr_code).style.display="block";
            document.getElementById(flag).style.display="none";

            document.getElementById(curr_code).value = getFrameNode(flag).document.body.innerHTML;
        }
        
        return false

    };

    /* 
    //增加浮动层
     var float_div = document.createElement('div');
     float_div.style.position = 'absolute';
     float_div.id = 'box';
     float_div.style.display = 'none';

     //浮动层内 iframe
     var float_div_iframe = document.createElement('iframe');
     float_div_iframe.id  = 'box_iframe';
     float_div_iframe.frameborder = 0;
     float_div_iframe.style.display = 'none'; 
   
     float_div.appendChild(float_div_iframe);
     main_embed.appendChild(float_div);
    */

       //preview功能
    this.preview = function(){
        document.getElementById('preview').innerHTML = getFrameNode(sRTE).document.body.innerHTML;
        if(document.getElementById('preview').style.display=='none'){
            document.getElementById('preview').style.display='';
        }else{
            document.getElementById('preview').style.display='none';
        }

        return false;
    }

    //重置内容
    this.reset_content = function(content){
        if(typeof(content) == null || typeof(content) == undefined){content = ''}
        

        var oRTE = getFrameNode(flag);
        
        if(document.getElementById(curr_code).style.display=='none'){
            oRTE.focus();
            oRTE.document.body.innerHTML = content;
            document.getElementById(curr_code).value = content;
        }else{
            document.getElementById(curr_code).value = content;

        }
	    
    
    }


	this.ubb2html=function(html)
	{
		html=html.replace(/\[media\swidth=\"(.+?)\"\sheight=\"(.+?)\"\ssrc=\"(.+?)\"\]/ig,this.tohtml("media",'$3','$1','$2'));
		html=html.replace(/\[audio\swidth=\"(.+?)\"\sheight=\"(.+?)\"\ssrc=\"(.+?)\"\]/ig,this.tohtml("audio",'$3','$1','$2'));
		html=html.replace(/\[flash\swidth=\"(.+?)\"\sheight=\"(.+?)\"\ssrc=\"(.+?)\"\]/ig,this.tohtml("flash",'$3','$1','$2'));
		return html;
	}
	this.tohtml = function(type,src,w,h)
	{
		var html;
		switch (type)
		{
			
			case "media":
				html='<object id="MediaPlayer1" width="'+ w +'" height="'+ h +'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '
				+'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"'
				+'align="baseline" border="0" standby="Loading Microsoft Windows Media Player components..." '
				+'type="application/x-oleobject">'
				+'<param name="URL" value="'+ src +'">'
				+'<param name="autoStart" value="true">'
				+'<param name="invokeURLs" value="false">'
				+'<param name="playCount" value="10">'
				+'<param name="defaultFrame" value="datawindow">'
				+'<param name="uiMode" value="full">'
				+'<embed src="'+ src +'"'
				+'align="baseline" border="0" width="'+ w +'" height="'+ h +'"'
				+'type="application/x-mplayer2"'
				+'pluginspage=""'
				+'name="MediaPlayer1" showcontrols="1" showpositioncontrols="0"'
				+'showaudiocontrols="1" showtracker="1" showdisplay="0"'
				+'showstatusbar="1"'
				+'autosize="0"'
				+'showgotobar="0" showcaptioning="0" autostart="1" autorewind="0"'
				+'animationatstart="0" transparentatstart="0" allowscan="1"'
				+'enablecontextmenu="1" clicktoplay="0" '
				+'defaultframe="datawindow" invokeurls="0">'
				+'</embed>'
				+'</object>';
				break;
			case "audio":
				html='<object id="MediaPlayer1" width="'+ w +'" height="'+ h +'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '
				+'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"'
				+'align="baseline" border="0" standby="Loading Microsoft Windows Media Player components..." '
				+'type="application/x-oleobject">'
				+'<param name="URL" value="'+ src +'">'
				+'<param name="autoStart" value="true">'
				+'<param name="invokeURLs" value="false">'
				+'<param name="playCount" value="10">'
				+'<param name="defaultFrame" value="datawindow">'
				+'<param name="uiMode" value="full">'
				+'<embed src="'+ src +'"'
				+'align="baseline" border="0" width="'+ w +'" height="'+ h +'"'
				+'type="application/x-mplayer2"'
				+'pluginspage=""'
				+'name="MediaPlayer1" showcontrols="1" showpositioncontrols="0"'
				+'showaudiocontrols="1" showtracker="1" showdisplay="0"'
				+'showstatusbar="1"'
				+'autosize="0"'
				+'showgotobar="0" showcaptioning="0" autostart="1" autorewind="0"'
				+'animationatstart="0" transparentatstart="0" allowscan="1"'
				+'enablecontextmenu="1" clicktoplay="0" '
				+'defaultframe="datawindow" invokeurls="0">'
				+'</embed>'
				+'</object>';
				break;
			case "flash":
				html= '<object '
				+ '  classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
				+ '  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"'
				+ '  width="' + w + '"'
				+ '  height="' + h + '">'
				+ '  <param name="movie" value="' + src + '">'
				+ '  <param name="quality" value="autohigh">'
				+ '  <param name="wmode" value="opaque">'
				+ '  <embed src="' + src + '" width="' + w + '" height="' + h + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'
				+ '</object>';
			default:
				break;
		}
		return html;
	}
    this.save_no_prompt = function(){
		var oRTE = getFrameNode(flag);
        
        this.content = '';

        
        if(document.getElementById(curr_code).style.display=='none'){
            oRTE.focus();
            this.content = oRTE.document.body.innerHTML;
            document.getElementById(curr_code).value = this.content;
        }else{
            this.content = document.getElementById(curr_code).value;
        }
	    
		if(document.all)
		{
			return this.content;
		}
		else
		{
			return this.ubb2html(this.content);
		}
	}
	

    this.save = function(){
		var oRTE = getFrameNode(flag);
        
        this.content = '';

        
        if(document.getElementById(curr_code).style.display=='none'){
            oRTE.focus();
            this.content = oRTE.document.body.innerHTML;
            document.getElementById(curr_code).value = this.content;
        }else{
            this.content = document.getElementById(curr_code).value;
        }
	    
         
        if (this.content == "" || /^(?:<br\/?>|\s|<\/?p>|&nbsp;)+$/gi.test(this.content)) {
			tom.Alert("请您填写内容");
			return '';
		}else if(JHshStrLen(this.content) > 40000){
				tom.Alert("内容超过了2万汉字");
				return '';
		}else{
			return this.content;
		}
	}
	


     
    //显示代码
    this.show_src = function(){
        var oRTE = getFrameNode(sRTE);
        tom.Alert(oRTE.document.body.innerHTML);
    }

   //插入一个滚动信息(){}
   this.insert_scroll = function(msg){
        if(msg==null || msg.length<1)    return false;
        
        var  hhtml = '<marquee  behavior="alternate" scrollamount="3"><h2>'+msg+'</h2></marquee>';
        insertHtml(hhtml);
        return false;
   }


    //缩进
    this.indent = function(){
        var oRTE = getFrameNode(flag);
        
        oRTE.focus();
		oRTE.document.execCommand("Indent", false, null);
		oRTE.focus();
    }


    //取消缩进
    this.outdent = function(){
		var oRTE = getFrameNode(sRTE);
        
        oRTE.focus();
		oRTE.document.execCommand("Outdent", false, null);
		oRTE.focus();
    }


    //执行一个标准命令
    this.run_standary_command = function(cmd){
            var oRTE = getFrameNode(sRTE); 
            
            if(oRTE.contentDocument){
                oRTE.contentWindow.focus();
                oRTE.contentDocument.execCommand(cmd , false , null);
                oRTE.contentWindow.focus();
             }else{           
                oRTE.focus();
                oRTE.execCommand(cmd ,false , null);
                oRTE.focus();
             } 
    }


    function writeDesignMode(ifr_desg , sHTML) {
    
		try{enableDesignMode(ifr_desg , 'on');}catch(e){tom.Alert(e.name + ": " + e.message);}
		var sFix = window.event ? "<div></div>" : "";
		
         var style = "<style type=\"text/css\">\n\
			body {\n\
				background: #ffffff;\n\
				margin:0;\n\
				padding:0;\n\
				font-size:14px;\n\
				overflow:auto;\n\
				line-height:1.5;\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\
			" + (document.all ? "\
			p {\n\
				margin: 0;\n\
				padding:0;\n\
				border:0;\n\
			}" : "") + "\
			</style>";
			
			var frameHTML = "\
			<html>\n\
			<head>\n\
			" + style + "\n\
			</head>\n\
			<body>\n\
			" + sHTML + "\n\
			" + sFix + "\n\
			</body>\n\
			</html>";
	
		var oRTE = getFrameNode(ifr_desg).document;
    oRTE.open();
       
		oRTE.write(frameHTML);
		oRTE.close();
	
    }
	writeDesignMode(flag , sHTML);
	
    	
    
    getFrameNode(flag).document.body.onclick = function(){
        document.getElementById(flag+"box").className="box";
    }


}

function getFrameNode(sNode){
   /* 
    var ee = '';
    var oo =document.frames.editor_design.document;
    for(dd in (oo)){
        ee += dd+"\n";
    }
    ee += "\n================\n"
    $('debuging').value = ee;
    */
    
    return document.frames ? document.frames[sNode] : document.getElementById(sNode).contentWindow;
    //return document.frames ? $(sNode).window : document.getElementById(sNode).contentWindow;
}
	
function enableDesignMode(ifr , sm){
    document.frames ? document.frames[ifr].document.designMode = sm : document.getElementById(ifr).contentDocument.designMode = sm;
}

function wordChiCommand(_sCmd , flag) {
    
	var oRTE = document.frames ? document.frames[flag] : document.getElementById(flag).contentWindow;


    function oPenWin(_sTitle, _sWidth, _sHeight, _sUrl, flag){
        document.getElementById(flag+'box').className = 'box'; 
        _sUrl = _sUrl+'?flag='+flag;
        
        var _bDialog = false;
        var _open    = false;

		var left = (window.screen.availWidth - _sWidth) / 2;
		var top = (window.screen.availHeight - _sHeight) / 2;

        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 {
        	window.open(_sUrl,"win","menubar=no,location=no,resizable=no,scrollbars=no,status=no,width="+_sWidth+",height="+_sHeight+",left="+left+",top="+top);
        	/*
            if(window.Event) {
                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;");
                }
            } else {
				window.open(_sUrl,"win","menubar=no,location=no,resizable=no,scrollbars=no,status=no,width="+_sWidth+",height="+_sHeight+",left="+left+",top="+top);
            }
            */
        }
    }

   function oPenDiv(_sClass,_sUrl,_sWidth ,flag){
        document.getElementById(flag+"box").className=_sClass;
        document.getElementById(flag+"box_iframe").src=_sUrl+'?flag='+flag;
        document.getElementById(flag+"box_iframe").width=_sWidth;
    }
	function format(oRTE)
	{
		try
		{
			var content = oRTE.document.body.innerHTML.replace(/\n/ig, "");
			//杀死js
			content = content.replace(/<script.*?>.*?<\/scrip[^>]*>/ig,"");
			content = content.replace(/<.*?javascript:[^>]*>/ig,"");

			//杀死style
			content = content.replace(/<style.*?>.*?<\/styl[^>]*>/ig,"");

			//清理多余html
			content = content.replace(/<table[^>]*>/ig,'');
			content = content.replace(/<tbody[^>]*>/ig,'');
			content = content.replace(/<tr[^>]*>/ig,'');
			content = content.replace(/<td[^>]*>/ig,'');
			content = content.replace(/<font[^>]*>/ig,'');
			content = content.replace(/<span[^>]*>/ig,'');
			content = content.replace(/<\/?dd[^>]*>/ig,'');
			content = content.replace(/<div[^>]*>/ig,'<br>');
			content = content.replace(/<h2[^>]*>/ig,'');
			content = content.replace(/<h3[^>]*>/ig,'');
			content = content.replace(/<code[^>]*>/ig,'<br>');
			content = content.replace(/<center[^>]*>/ig,'');
			content = content.replace(/<th[^>]*>/ig,'');
			content = content.replace(/<sohu[^>]*>/ig,'');
			content = content.replace(/<form[^>]*>/ig,'');
			content = content.replace(/<input[^>]*>/ig,'');
			content = content.replace(/<select[^>]*>/ig,'');
			content = content.replace(/<textarea[^>]*>/ig,'');
					content = content.replace(/<o\:p>/ig,'');

			content = content.replace(/<\/table>/ig,'');
			content = content.replace(/<\/tbody>/ig,'');
			content = content.replace(/<\/tr>/ig,'');
			content = content.replace(/<\/td>/ig,'');
			content = content.replace(/<\/font>/ig,'');
			content = content.replace(/<\/span>/ig,'');
			content = content.replace(/<\/div>/ig,'<br>');
			content = content.replace(/<\/h2>/ig,'');
			content = content.replace(/<\/h3>/ig,'');
			content = content.replace(/<\/code>/ig,'<br>');
			content = content.replace(/<\/center>/ig,'');
			content = content.replace(/<\/th>/ig,'');
			content = content.replace(/<\/sohu[^>]*>/ig,'');
			content = content.replace(/<\/form[^>]*>/ig,'');
			content = content.replace(/<\/input[^>]*>/ig,'');
			content = content.replace(/<\/select[^>]*>/ig,'');
			content = content.replace(/<\/textarea[^>]*>/ig,'');
				content = content.replace(/<\/o\:p>/ig,'');
			
			content = content.replace(/<\?xml[^>]*>/ig,'');
			content = content.replace(/<\!--.*?-->/ig,'');
			//content = content.replace(//ig,'');
			//content = content.replace(//ig,'');
			//content = content.replace(//ig,'');
			
			//处理图片
			//content = content.replace(/<img.*?id=([\S]+).*?src=([^ |>]*)[^>]*>/ig,"<img id=$1 src=$2 border=0  >");
			content = content.replace(/<img[^>]+src=([/"|/']http:[^ |>]*)[^>]*>/ig,"<img src=$1 border=0>");
			content = content.replace(/(<img[^>]*>)/ig , "<center>$1<\/center>");

			//去掉链接中的无用参数
			content = content.replace(/<a.*?href="([^"]*)"[^>]*>/ig,"<a href=\"$1\">");

			//去掉iframe
			content = content.replace(/<iframe[^>]*>/ig,"");
			content = content.replace(/<\/iframe[^>]*>/ig,"");

			//格式文本
			content = "MM163brMM" + content;
			content = content.replace(/<br>\s*<br>/ig, 'MM163brMM');
			content = content.replace(/<center>\s*<center>/ig, '<center>');
			content = content.replace(/<\/center>\s*<\/center>/ig, '</center>');
			content = content.replace(/<center>/ig, 'MM163brMM<center>');
			content = content.replace(/<\/center>/ig, '</center>MM163brMM');
			content = content.replace(/<br>/ig, 'MM163brMM');
			content = content.replace(/<p[^>]*>/ig, 'MM163brMM');
			content = content.replace(/<\/p[^>]*>/ig, '');
			content = content.replace(/&nbsp;/ig, '');
			content = content.replace(/　/ig, '');
			content = content.replace(/\r/ig, '');
			content = content.replace(/\n/ig, '');
			content = content.replace(/MM163brMM\s*MM163brMM/ig,'MM163brMM');
			content = content.replace(/MM163brMM/ig,'</P><P style="TEXT-INDENT: 2em">');
			content = content.replace("</P>","");

			oRTE.document.body.innerHTML = content;
		}
		catch(e)
		{
			tom.Alert(e);
		}
	}
    switch(_sCmd){
		
		case "":
			break;
		case "link":
		    oPenWin("请输入网页地址", 300, 120, base+"InsertLink.html" , flag);
            break;
		case "emotion":
			oPenDiv("box1",base+"InsertEmotion.html","140" , flag);
			break;
		case "img":
			oPenWin("请输入图片地址", 360, 80, base+"InsertImg.html" , flag);
            break;
	   	case "format":
			format(oRTE);
			break;
        case "flash":
			oPenWin("请输入Flash地址", 300, 160, base+"InsertFlash.html" , flag)
            break;
		case "video":
			oPenWin("请输入视频地址", 300, 160, base+"InsertVideo.html" , flag);
            break;
		case "forecolor":
			oPenDiv("box4", base+"ForeColor.html","135" , flag);
			break;
		case "hilitecolor":
			oPenDiv("box5", base+"BackColor.html", "135" , flag);
			break;
        case "flash":
            oPenWin("请输入Flash地址", 300, 160, base+"InsertFlash.html" , flag);
            break;
        case "audio":
            oPenWin("请输入音频地址", 300, 160,  base+"InsertAudio.html" , flag);
            break;

        case "preview":
			window.open("/preview.html","","");
			break;
		case "fontsize":
			oPenDiv("box3",base+"FontSize.html","40" , flag) ;
			document.getElementById(flag+"box_iframe").height="134";
			break;
		case "fontname":
			oPenDiv("box2",base+"FontName.html","50" , flag);
			break;
		case "cut":
			if (document.all) {
				oRTE.focus();
				oRTE.document.execCommand("cut", false, null);
				oRTE.focus();
			} else {
				tom.Alert("您的浏览器安全设置不允许编辑器自动执行剪切操作\n请使用(Ctrl+X)来完成。");
			}
			break;
		case "copy":
			if (document.all) {
				oRTE.focus();
				oRTE.document.execCommand("copy", false, null);
				oRTE.focus();
			} else {
				tom.Alert("您的浏览器安全设置不允许编辑器自动执行复制操作\n请使用(Ctrl+C)来完成。");
			}
			break;
		case "paste":
			if (document.all) {
				oRTE.focus();
				oRTE.document.execCommand("paste", false, null);
				oRTE.focus();
			} else {
				tom.Alert("您的浏览器安全设置不允许编辑器自动执行粘贴操作\n请使用(Ctrl+V)来完成。");
			}
			break;
		
		case 'image_type':
            var cur_elem= get_curr_element(flag);
            if((cur_elem == false) || (cur_elem == null)){
                tom.Alert('请选择一个图片');
                break;
            }
            if(cur_elem.tagName.toLowerCase() == 'img'){
                oPenDiv("box7",base+"image_type.html","190" , flag);
            }else{
                tom.Alert('请选择一个图片');
            }
            break;

        default:
			oRTE.focus();
			oRTE.document.execCommand(_sCmd, false, null);
			oRTE.focus();
			break;
	}
}

function publish_article(){
	et.save();
}
function insertHtml(html , flag) {
	var oRTE = getFrameNode(flag);
    if(window.Event){
        oRTE.focus();
        oRTE.document.execCommand('insertHTML' , false , html);
        oRTE.focus();
	} else {
		oRTE.focus();
		var oRng = oRTE.document.selection.createRange();
		oRng.pasteHTML(html);
		oRng.collapse(false);
		oRng.select();
	}
}

function insertIMAGE(src , flag) {
    if((!/^http:\/\//i.test(src)) && (!/^\//i.test(src))){
        src = 'http://'+src;
    }
   
    flag += 'editor_design';
    if(navigator.appName == "Microsoft Internet Explorer"){
        var oRTE = getFrameNode(flag);
        oRTE.focus();
        oRTE.document.execCommand('insertIMAGE', false, src);
        oRTE.focus();
    } else {
        html = '<img src='+src+'>';
        insertHtml(html , flag);
    }
}


function setCmd(_sSort, _sFont , flag){
	
	var oRTE = getFrameNode(flag);
	oRTE.focus();

    /*
    oRTE.document.execCommand(_sSort, '', _sFont);
    */
    
    if (_sSort == "FontName") {
		oRTE.document.execCommand(_sSort, '', _sFont);
	} else {
		formatFont(oRTE, _sFont);
	}
	
	oRTE.focus();
}

//hack fontsize command
function formatFont(ifra, v){
  //if IE
  if(document.all){
    ifra.document.execCommand("fontname", "", "TomRE_FN_TMP_XXX");
    var allfont = ifra.document.body.getElementsByTagName("font");
    for (var i = 0; i < allfont.length; i++){
      var font = allfont[i];
      if (font.getAttribute("face") == "TomRE_FN_TMP_XXX"){
        _delInFont(font, v);
        font.style.fontSize = v;
        font.removeAttribute("face");
      }
    }
  } else{
	var fsArr = ['', '10px', '14px', '16px', '18px', '24px', '32px'];
    ifra.document.execCommand("fontsize", "", v);
    var allfont = ifra.document.getElementsByTagName("font");
    for (var i = 0; i < allfont.length; i++){
      var font = allfont[i];
      if (font.getAttribute("size") == v){
        _delInFont(font, fsArr[v]);
        font.style.fontSize = fsArr[v];
      }
    }
  }
}
function _delInFont(elem, v){
  if (elem.tagName == "FONT" || elem.tagName == "SPAN") {
  	elem.style.fontSize = v;
  }
  var children = elem.childNodes;
  if (children) {
  	for (var i = 0; i < children.length; i++) {
      _delInFont(children[i], v);
  	}
  }
}



 
    //设置一个按钮
word.prototype.set_button = function(tag_name ,title , className , onclick_cmd , flag){
        var sb = document.createElement(tag_name);
        sb.title      = title;
        sb.className  = className;
        sb.href       = '#';
        sb.id         = onclick_cmd;

        sb.onclick = function(){
            execute_command(onclick_cmd , flag);
            return false;
        }

        return sb;
};
   

    //交换文本模式/编辑模式
word.prototype.swap_mode = function(){
        //tb1=table of view
        //tb2=table of code
        
        //tp1=iframe of designMode
        //tp2=textarea of codeMode

        var is_design_mode = (document.getElementById(editor_design+'tb2').style.display == 'none');
        
        if(is_design_mode){
            document.getElementById(editor_design+"tp2").style.display="block";
            document.getElementById(editor_design+"tp1").style.display="none";
            document.getElementById(editor_design+"tb1").className="tabout";
            document.getElementById(editor_design+"tb2").className="tabon";
            document.getElementById(editor_code).style.display="block";
            document.getElementById(editor_design).style.display="none";
        }else{
            document.getElementById(editor_design+"tp2").style.display="none";
            document.getElementById(editor_design+"tp1").style.display="block";
            document.getElementById(editor_design+"tb1").className="tabon";
            document.getElementById(editor_design+"tb2").className="tabout";
            document.getElementById(editor_code).style.display="none";
            document.getElementById(editor_design).style.display="block";

        }

        
        return false
};
  

//高亮一下
word.prototype.shine_me = function(){
        var is_design_mode = (document.getElementById(flag+'tb2').style.display == 'none');
        
        if(is_design_mode){
            new Effect.Highlight(editor_code , '{duration:1}');
        }else{
            new Effect.Highlight(editor_design , '{duration:1}');
        }

        
        return false
};
  



 

    //假设释放
word.prototype.free = function(){
        //仅仅用来提示释放 
        for(i=0 ; i<arguments.length ; i++)
            //this.free.arguments[i] = new Object;
            delete this.free.arguments[i];

};
 
                
//执行一个命令
execute_command = function(cmd , flag){
        //document.getElementById('box').className='box';
        //tom.Alert("隐藏浮动层");
        wordChiCommand(cmd , flag);
        if(window.event){
            event.cancelBubble=true;
        }else{
            return false;
        }

}

//内嵌层移动到指定位置并显示
display_float_div = function(obj_id,width,src){
    var box = document.getElementById(editor_design+'box');
    var box_iframe = document.getElementById(editor_design+'box_iframe');
    var obj = document.getElementById(obj_id);
    
    //顶端
    var top  = parseInt(obj.style.top) + parseInt(obj.style.height) +1 ;

    //左边
    var left = parseInt(obj.style.left)+(parseInt(obj.style.width) - width)/2;

    var zzz = '';
    for (var ee in box){
        zzz += ee+':'+box.getAttribute(ee)+"\n";
    }
    document.getElementById('debuging').style.display = 'block';
    document.getElementById('debuging').value = zzz;
   
    //设置移动层
    box_iframe = src;
    box_iframe.width = width+'px';
    box.style.left   = left+'px';
    box.style.top    = top+'px';
    box.style.width  = (width+4)+'px';

    box.style.display = 'block';
    // box_iframe.style.display = 'block';
}

//隐藏内嵌浮动层
hiden_float_div = function(){
    document.getElementById(editor_design+'box').style.display = 'none';
    //document.getElementById('box_iframe').style.display = 'none';
}



//选中元素返回
get_curr_element = function(flag) {
        var element = null;
        var sel;
        var range;
        
        if(is_ie()) {
            selectional = getFrameNode(flag).document.selection;
            switch(selectional.type.toLowerCase()) {
                case "none": {
                    element = null;
                    break;
                }
                case "text": {
                    range = selectional.createRange();
                    element = range.parentElement();
                    break;
                }
                case "control": {
                    var ranges = selectional.createRange();

                    element = ranges.item(0);
                    break;
                }
            }
        }else{
            sel = getFrameNode(flag).getSelection();
            if(sel.rangeCount > 0){
                range = sel.getRangeAt(0);
                if(range.startContainer == range.endContainer)
                {
                    if(range.startContainer.nodeType != 3)
                        element = range.startContainer.childNodes[range.startOffset];
                    else 
                        element = range.startContainer;
                }else{
                    element = range.commonAncestorContainer;
                }
            }
            if(element && element.nodeType == 3) element = element.parentNode;
        }

        
        return element;
}


    
   //判断是否是IE
is_ie = function(){
        return navigator.appName == "Microsoft Internet Explorer";
   } 
    

set_img_position = function(pos_flag , flag){
        curr_element = get_curr_element(flag);
        if(curr_element == null || curr_element.tagName.toLowerCase() != 'img'){
            tom.Alert("请您选中一张图片");
            return false;
        }
        
        if(pos_flag == "left") {
                    curr_element.style.margin = "4px";
                    curr_element.style.float = "left";
                    curr_element.style.display = "block";
                    curr_element.style.textAlign = "";
        }else if(pos_flag == "center") {
                    curr_element.style.margin = "4px auto";
                    curr_element.style.display = "block";
                    curr_element.style.float = "";
                    curr_element.style.textAlign = "center";
                    curr_element.setAttribute("align", "");
                
         }else if(pos_flag == "right") {
                    curr_element.style.margin = "10px";
                    curr_element.style.float = "right";
                    curr_element.style.display = "block";
                    curr_element.style.textAlign = "right";
          }
        
        getFrameNode(flag).document.body.innerHTML = getFrameNode(flag).document.body.innerHTML;
        return false;
   }



 
   //缩放编辑器
   resize = function(size , flag){
        size = parseInt(size);
        
        RTE       = flag;
        sRTE_text = editor_code; 
        document.getElementById(RTE).height=parseInt(document.getElementById(RTE).height)+size;
        document.getElementById(sRTE_text).style.height=parseInt(document.getElementById(sRTE_text).style.height)+size;

        return false;
    }



function JHshStrLen(sString)
{
    var sStr,iCount,i,strTemp;

    iCount = 0 ;
    sStr = sString.split("");
    for (i = 0 ; i < sStr.length ; i ++)
    {
    strTemp = escape(sStr[i]);
    if (strTemp.indexOf("%u",0) == -1)
    {
    iCount = iCount + 1 ;
    }
    else
    {
    iCount = iCount + 2 ;
    }
    }
    return iCount ;
}

