﻿// =========================================================
var XEDITOR_STATUS_NOTLOADED = 0;
var XEDITOR_STATUS_ACTIVE = 1;
var XEDITOR_STATUS_COMPLETE = 2;
var XEDITOR_TRISTATE_OFF = 0;
var XEDITOR_TRISTATE_ON = 1;
var XEDITOR_TRISTATE_DISABLED = -1;
var XEDITOR_UNKNOWN = -9;
var CNT = 0;
var recordhistory;
var XEDITOR_ZOOMFACTOR = 1;

String.prototype.Contains = function(A){return (this.indexOf(A)>-1);};
var strBrowserInfo = navigator.userAgent.toLowerCase();
var XBrowserInfo = {
	IsIE:strBrowserInfo.Contains('msie'),
	IsIE7:strBrowserInfo.Contains('msie 7'),
	IsGecko:strBrowserInfo.Contains('gecko/'),
	IsSafari:strBrowserInfo.Contains('safari'),
	IsOpera:strBrowserInfo.Contains('opera')
};


XBrowserInfo.IsGeckoLike = XBrowserInfo.IsGecko||XBrowserInfo.IsSafari||XBrowserInfo.IsOpera;
var XSelection = new Object();
XSelection.GetType=function()
{  
//	if(XBrowserInfo.IsGeckoLike)
//	{
//		selectionType = 'Text';
//		var A;
//		try 
//		{ 
//			A = window.WinEditor.getSelection();
//		}
//		catch (e) {};
//		if (A&&A.rangeCount==1)
//		{
//			var B=A.getRangeAt(0);
//			if (B.startContainer==B.endContainer&&(B.endOffset-B.startOffset)==1&&B.startContainer.nodeType!=Node.TEXT_NODE) 
//				selectionType='Control';
//		};
//		return selectionType; 
//	}
//	else
//	{ 
		return window.DocEditor.selection.type;
//	}
};
XSelection.GetSelectedElement=function()
{
	if (this.GetType()=='Control')
	{
		var A=window.DocEditor.selection.createRange();
		if (A&&A.item)
		{  
			return window.DocEditor.selection.createRange();
	    }
	}
};






//XSelection.GetSelectedText = function()
//{
//	if (this.GetType() == "Text")
//	{
//		if(XBrowserInfo.IsGeckoLike){
//			var selection=window.WinEditor.getSelection();
//			return selection;
//		}
//		else
//		{
//			var selection = window.DocEditor.selection;
//			return selection;
//		}
//	}
//	return null;
//}


//函数控制工具栏的显示与隐藏
var SetToolbarStatus = function(selectedType)
{
    
	var arrayDisabledItems;
	selectedType = selectedType.toLowerCase();
//	var p = Ext.Element.get('skypanel');
//	alert(p.collapsed);
//    if(p.collapsed == false)
//    {
//        
//       p.expand(true);
//	}   
	if(myHistory.position > 0)
	{
	    document.getElementById('div3').style.display = "block";
	}
	else
	{
	    document.getElementById('div3').style.display = "none";
	}
	
	switch(selectedType)
	{
	
		case "none":
		
		    document.getElementById('modifyAlign').style.display = "none";
	        document.getElementById('modifyText2').style.display = "none";
	        document.getElementById('modifyBackPic').style.display = "none";
	        document.getElementById('divline').style.display = "none";
	        break;
		case "text":
		
            document.getElementById('modifyAlign').style.display = "none";
	        document.getElementById('modifyText2').style.display = "none";
	        document.getElementById('modifyBackPic').style.display = "none";
	        document.getElementById('divline').style.display = "none";
	        break;
		case "control":
		
			//var selectedElement = XSelection.GetSelectedElement();
			var selectedElement = window.SelectObject;
			//如果2个以上
			if(selectedElement && selectedElement.length > 1)
			{
			    //如果都是文字(取第一个文字的字体，字号，)
			    var flagdiv = 0;
		        for(var i=0;i<selectedElement.length;i++)
		        {
		            if(selectedElement.item(i).tagName.toUpperCase() == "DIV" && selectedElement.item(i).type != "LINE" )
		            {
		                flagdiv++;
		            }
				}
				if(flagdiv < selectedElement.length)
				{
			        document.getElementById('modifyText2').style.display = "none";
				}
				else
				{
				    document.getElementById('modifyText2').style.display = "block";
				    //如果高级模式，显示文字层中的输入框，简单模式不显示
				    if(document.getElementById("H1").value == "1")
				    {
				        document.getElementById('SelectTextTable').style.display = "block";
				    }
				    else
				    {
				        document.getElementById('SelectTextTable').style.display = "none";
				    }
				    document.getElementById('h_currentselectid').value = selectedElement.item(0).id;
			        document.getElementById('SelectText').value = selectedElement.item(0).innerHTML;
			        var ss = selectedElement.item(0).outerHTML;
			        var sss = selectedElement.item(0).style;
                    //默认绑定字体
                    if(sss.fontFamily != "")
                    {
                        document.getElementById('fontfamily').value = sss.fontFamily;
                    }
                    else
                    {
                        document.getElementById('fontfamily').value = "黑体";
                    }
                    //默认绑定字号
                    if(sss.fontSize != "")
                    {
                        document.getElementById('fontsize').value = parseInt(sss.fontSize);
                    }
                    else
                    {
                        document.getElementById('fontsize').value = "22";
                    }
                    //绑定默认加粗
                    if(sss.fontWeight != "" && sss.fontWeight == "bold")
                    {
                        document.getElementById('bold').src = "/images/text_bold_3.gif";
                    }
                    else
                    {
                        document.getElementById('bold').src = "/images/text_bold_1.gif";
                    }
                    //绑定默认斜体
                    if(sss.fontStyle != "" && sss.fontStyle == "italic")
                    {
                        document.getElementById('italic').src = "/images/text_italic_3.gif";
                    }
                    else
                    {
                        document.getElementById('italic').src = "/images/text_italic_1.gif";
                    }
                    if(document.getElementById('CENTER_LIST') != null)
                    {
                        //默认绑定匹配居中
                        if(ss.indexOf("|1|") != -1)
                        {
                           document.getElementById('CENTER_LIST').src = "/images/29_2.gif";
                        }
                        else
                        {
                            document.getElementById('CENTER_LIST').src = "/images/29_1.gif";
                        }
                    }
                    if(document.getElementById('RIGHT_LIST') != null)
                    {
                        //默认绑定匹配居中
                        if(ss.indexOf("|2|") != -1)
                        {
                           document.getElementById('RIGHT_LIST').src = "/images/29_2_you.gif";
                        }
                        else
                        {
                            document.getElementById('RIGHT_LIST').src = "/images/29_1_you.gif";
                        }
                    }
				}
			    //如果不都是文字
			    document.getElementById('modifyAlign').style.display = "block";
			    document.getElementById('modifyBackPic').style.display = "none";
			    document.getElementById('divline').style.display = "none";
			}
			else  //一个或者没选
			{
		        //文字
			    if(selectedElement && selectedElement.item(0).tagName && selectedElement.item(0).tagName.toUpperCase() == "DIV" && selectedElement.item(i).type != "LINE")
			    {
			        document.getElementById('modifyAlign').style.display = "none";
			        document.getElementById('modifyText2').style.display = "block";
			        document.getElementById('modifyBackPic').style.display = "none";
			        document.getElementById('divline').style.display = "none";
			        //如果高级模式，显示文字层中的输入框，简单模式不显示
				    if(document.getElementById("H1").value == "1")
				    {
				        document.getElementById('SelectTextTable').style.display = "block";
				    }
				    else
				    {
				        document.getElementById('SelectTextTable').style.display = "none";
				    }
			        //这个是关联text和修改关联
			        document.getElementById('h_currentselectid').value = selectedElement.item(0).id;
			        document.getElementById('SelectText').value = selectedElement.item(0).innerHTML;
			        var ss = selectedElement.item(0).outerHTML;
			        var sss = selectedElement.item(0).style;
                    //默认绑定字体
                    if(sss.fontFamily != "")
                    {
                        document.getElementById('fontfamily').value = sss.fontFamily;
                    }
                    else
                    {
                        document.getElementById('fontfamily').value = "黑体";
                    }
                    //默认绑定字号
                    if(sss.fontSize != "")
                    {
                        document.getElementById('fontsize').value = parseInt(parseInt(sss.fontSize)/XEDITOR_ZOOMFACTOR);
                    }
                    else
                    {
                        document.getElementById('fontsize').value = parseInt(22/XEDITOR_ZOOMFACTOR);
                    }
                    //绑定默认加粗
                    if(sss.fontWeight != "" && sss.fontWeight == "bold")
                    {
                        document.getElementById('bold').src = "/images/text_bold_3.gif";
                    }
                    else
                    {
                        document.getElementById('bold').src = "/images/text_bold_1.gif";
                    }
                     //绑定默认斜体
                    if(sss.fontStyle != "" && sss.fontStyle == "italic")
                    {
                        document.getElementById('italic').src = "/images/text_italic_3.gif";
                    }
                    else
                    {
                        document.getElementById('italic').src = "/images/text_italic_1.gif";
                    }
                   
                    if(document.getElementById('CENTER_LIST') != null)
                    {
                        //默认绑定匹配居中
                        if(ss.indexOf("|1|") != -1)
                        {
                           document.getElementById('CENTER_LIST').src = "/images/29_2.gif";
                        }
                        else
                        {
                            document.getElementById('CENTER_LIST').src = "/images/29_1.gif";
                        }
                    }
                    if(document.getElementById('RIGHT_LIST') != null)
                    {
                        //默认绑定匹配居中
                        if(ss.indexOf("|2|") != -1)
                        {
                           document.getElementById('RIGHT_LIST').src = "/images/29_2_you.gif";
                        }
                        else
                        {
                            document.getElementById('RIGHT_LIST').src = "/images/29_1_you.gif";
                        }
                    }
			    }
			    else if(selectedElement && selectedElement.item(0).tagName && selectedElement.item(0).tagName.toUpperCase() == "IMG")
			    {
			        document.getElementById('modifyAlign').style.display = "none";
			        document.getElementById('modifyText2').style.display = "none";
			        document.getElementById('modifyBackPic').style.display = "block";
			        document.getElementById('divline').style.display = "none";
			        document.getElementById('h_currentselectid').value = selectedElement.item(0).id;
			    }
			    else if(selectedElement && selectedElement.item(0).tagName && selectedElement.item(0).tagName.toUpperCase() == "DIV" && selectedElement.item(i).type == "LINE")
			    {
			        document.getElementById('modifyAlign').style.display = "none";
			        document.getElementById('modifyText2').style.display = "none";
			        document.getElementById('modifyBackPic').style.display = "none";
			        document.getElementById('divline').style.display = "block";
			        if(selectedElement.item(0).style.borderTopStyle)
			        {
			            document.getElementById('LineType').value = selectedElement.item(0).style.borderTopStyle;
			            document.getElementById('LineSize').value = selectedElement.item(0).style.borderTopWidth;
			        }
			        else if(selectedElement.item(0).style.borderLeftStyle)
			        {
			            document.getElementById('LineType').value = selectedElement.item(0).style.borderLeftStyle;
			            document.getElementById('LineSize').value = selectedElement.item(0).style.borderLeftWidth;
			        }
			        else
			        {
			            document.getElementById('LineType').value = "solid";
			            document.getElementById('LineSize').value = "2px";
			        }
			    }
			    else
			    {
			        document.getElementById('modifyAlign').style.display = "none";
			        document.getElementById('modifyText2').style.display = "none";
			        document.getElementById('modifyBackPic').style.display = "none";
			        document.getElementById('divline').style.display = "none";
			    }
			}
			break;
	    case "all":
            alert("all");
	        break;
		default:
			alert("default");
			break;
	}
}

//初始化
function Initialize(strContent)
{
 	window.DocEditor.body.innerHTML = strContent;
 	//禁止右键
	window.DocEditor.oncontextmenu = function()
	{
		return false;
	}
	if(window.DocEditor.attachEvent)
	{ 
		window.DocEditor.attachEvent("onmouseup",OnSelectionChange);
	    window.DocEditor.attachEvent("onkeydown",OnDirectionKeyUp);
	    document.attachEvent("onkeydown",OnDirectionKeyUp);
	}
	else
	{ 
		window.DocEditor.addEventListener('mouseup',OnSelectionChange,false);
	}
	window.DocEditor.ondblclick = function dclick() 
    { 
        if(window.SelectObject)
        {
            if(window.SelectObject && window.SelectObject.length == 1)
            {
			    //如果都是文字(取第一个文字的字体，字号，)
    		    window.SelectObject.item(0).focus();
                window.SelectObject.item(0).blur();
                if(window.SelectObject.item(0).tagName.toUpperCase() == "DIV" && window.SelectObject.item(0).type != "LINE")
                {
                    Ext.MessageBox.show({
                            title: '请输入您要修改的内容',
                            buttons:{"ok":"确定","cancel":"取消"},
                            fn:showResultText,
                            width:250,
                            prompt:true,
                            closable:false,
                            value:window.SelectObject.item(0).innerText
                    });
                }
            }
        }
    };
	MakeEditable();	
    recordhistory = window.setInterval("saveHistory()", 1000);
}

function showResultText(btn, text)
{
    if(btn == "ok")
    {
        setselectitemtext1(text);
    }
    window.SelectObject.select();
};

var OnSelectionChange = function(evt)
{
    var oEventSrc;
	if(!evt)
		evt = window.event;
	if(evt.srcElement)
		oEventSrc = evt.srcElement;
	else
		oEventSrc = evt.target;
    var oSelect = XSelection.GetSelectedElement();
    if(oSelect != null && oSelect.item(0) != null)
    {
        window.SelectObject = oSelect;
        if(oSelect.length == 1)
        {
           oSelect.item(0).focus();
           oSelect.item(0).blur();
           window.SelectObject.select();
           
           //CNT = evt.type;
        }
    }
    else
    {
        window.SelectObject = null;
    }
	var type = XSelection.GetType();
	SetToolbarStatus(type);
	//saveHistory();
}

//var OnDirectionKeyDown=function(evt)
//{  
//    if( evt.ctrlKey &&  evt.keyCode == 90 ){alert(myHistory.position);goHistory(-1);return false;}
//	  else if( evt.ctrlKey && evt.keyCode == 89 ) {goHistory(1);evt.returnValue=false;}
//}

var OnDirectionKeyUp=function(evt)
{  
    if( evt.ctrlKey &&  evt.keyCode == 90 ){Undo();return false;}
	else if( evt.ctrlKey && evt.keyCode == 89 ) {Redo();evt.returnValue=false;}
    if(window.SelectObject && evt.srcElement.tagName == "IFRAME")
    {
         var count = window.SelectObject.length;
         var rng;
         if(evt.keyCode== 37)
          {
              for(var i=0;i<count;i++)
              {
                rng=window.SelectObject.item(i);  
                var leftLong=rng.style.left;
                var leftvalue=parseInt(leftLong)-1;
                if(leftvalue>0)
                   rng.style.left=leftvalue+"px";
              }
               evt.returnValue=false;
          }
          else if(evt.keyCode== 38)
          { 
              for( var i=0;i<count;i++)
              {
                rng=window.SelectObject.item(i); 
                var leftLong=rng.style.top;
                var leftvalue=parseInt(leftLong)-1;
                if(leftvalue>0)
                     rng.style.top=leftvalue+"px";
              }
               evt.returnValue=false;
          }
          else if(evt.keyCode== 39)
          {
              for( var i=0;i<count;i++)
                {
                    rng=window.SelectObject.item(i);  
                    var leftLong=rng.style.left;
                    var leftvalue=parseInt(leftLong)+1;
                    rng.style.left=leftvalue+"px";
                }
                 evt.returnValue=false;
          }
          else if(evt.keyCode== 40)
          {
               for( var i=0;i<count;i++)
                {
                    rng=window.SelectObject.item(i);
                    var leftLong=rng.style.top;
                    var leftvalue=parseInt(leftLong)+1;
                    rng.style.top=leftvalue+"px";
                }
                 evt.cancelBubble = true;
                 evt.returnValue=false;
          }
          else if(evt.keyCode== 46 || evt.keyCode== 8)
          {
              window.DocEditor.execCommand('Delete',false,false);
              evt.returnValue=false;
          }
          //saveHistory();
    }
}

function movepx(value,type)
{
    if(window.SelectObject)
    {
         var count = window.SelectObject.length;
         var rng;
         if(type== "left")
          {
              for(var i=0;i<count;i++)
              {
                rng=window.SelectObject.item(i);  
                var leftLong=rng.style.left;
                var leftvalue=parseInt(leftLong)-parseInt(value);
                if(leftvalue>0)
                   rng.style.left=leftvalue+"px";
              }
          }
          else if(type== "up")
          { 
              for( var i=0;i<count;i++)
              {
                rng=window.SelectObject.item(i); 
                var leftLong=rng.style.top;
                var leftvalue=parseInt(leftLong)-parseInt(value);
                if(leftvalue>0)
                     rng.style.top=leftvalue+"px";
              }
          }
          else if(type== "right")
          {
              for( var i=0;i<count;i++)
                {
                    rng=window.SelectObject.item(i);  
                    var leftLong=rng.style.left;
                    var leftvalue=parseInt(leftLong)+parseInt(value);
                    rng.style.left=leftvalue+"px";
                }
          }
          else if(type== "down")
          {
               for( var i=0;i<count;i++)
                {
                    rng=window.SelectObject.item(i);
                    var leftLong=rng.style.top;
                    var leftvalue=parseInt(leftLong)+parseInt(value);
                    rng.style.top=leftvalue+"px";
                }
          }
    }
    else
    {
        alert('您没有选择需要移动的对象');
    }
}

var MakeEditable=function()
{
	if (window.DocEditor.body.contentEditable) {
	    window.DocEditor.execCommand("2D-Position",false,true);
     	window.DocEditor.execCommand("LiveResize",false,true);
     	window.DocEditor.execCommand("MultipleSelection",false,true);
		window.DocEditor.body.contentEditable=true;
	}
	else
	{
		window.DocEditor.designMode="On";
		window.DocEditor.execCommand("useCSS",false,true);
		window.DocEditor.execCommand("enableObjectResizing",false,false);
		window.DocEditor.execCommand("enableInlineTableEditing",false,false);
	}
	//window.WinEditor.focus();
	window.DocEditor.body.onkeydown = function(evt){
	    if(!evt)
		    evt = window.WinEditor.event;
	    var evtSrc = evt.target || evt.srcElement;
	    if(evtSrc.tagName.toUpperCase() == "BODY"){
	        return false;
	    }
	}
}

var DisEditable=function(){
	if (window.DocEditor.body.contentEditable) {
		window.DocEditor.body.contentEditable=false;
	}else{
		window.DocEditor.designMode="Off";
		window.DocEditor.execCommand("useCSS",false,true);
		window.DocEditor.execCommand("enableObjectResizing",false,true);
		window.DocEditor.execCommand("enableInlineTableEditing",false,false);
	}
}


var Execute = function(cmdName,evt,zoomFactor){

	if(!evt)
		evt = window.event;
	var evtSrc = evt.srcElement;
	if(window.SelectObject && window.SelectObject.item(0) != null ||cmdName=="InsertImage")
	{
	
	    var count = window.SelectObject == null ?0: window.SelectObject.length;
		var oTarget = window.SelectObject;
		    switch (cmdName)
		    {
	
			    case "InsertImage":
				    this.InsertImage();
				    break;
//			    case "InsertSpecialChar":
//			        window.TargetCurr = oTarget;
//				    this.InsertSpecialChar();
//				    break;
//			    case "SwapBackImage":
//			        this.SwapBackImage();
//				    break;
//			    case "CanvasSize":
//				    this.CanvasSize();
//				    break;
			    case "TextHorizontal":  //竖式
				    if(oTarget && oTarget.item(0))
				    {
		                for( var i=0;i<count;i++)
		                {
					       oTarget.item(i).style.writingMode = "lr-tb";
					    }
					}
				    break;
			    case "TextVertical":  //横式
				    if(oTarget && oTarget.item(0))
				    {
		                for( var i=0;i<count;i++)
		                {
		                   oTarget.item(i).style.writingMode = "tb-rl";
					    }
					}
				    break;
			    case "ForeColor"://字色[单]
			        this.PickColor();
				    break;
				case "LineColor"://字色[单]
			        this.PickLineColor();
				    break;
//			    case "BackColor":
//				    var color = "#ffcc00";
//				    if(oTarget && oTarget.style)
//					    oTarget.style.backgroundColor = color;
//				    break;
			    case "FontName":   //字体
				    var fontName = evtSrc.value;
				    if(fontName != "" && oTarget && oTarget.item(0).style)
				    {
		                for( var i=0;i<count;i++)
		                {
		                    oTarget.item(i).style.fontFamily = fontName;
					    }
				    }
				    break;
			    case "FontSize":  //字号
                    if(zoomFactor == null)
                        zoomFactor = 1;
				    var fontSize = Math.round(evtSrc.value*zoomFactor);
				    if(fontSize != "" && oTarget && oTarget.item(0).style){
		                for( var i=0;i<count;i++)
		                {
		                    oTarget.item(i).style.fontSize = fontSize;
					    }
				    }
				    break;
			    case "Bold":  //粗体[多]
				    if(oTarget && oTarget.item(0).style)
				    { 
		                if(oTarget.item(0).style.fontWeight != "bold")
		                {
		                    document.getElementById('bold').src = "/images/text_bold_3.gif";
		                    option = "bold";
		                }
		                else
		                {
		                    document.getElementById('bold').src = "/images/text_bold_1.gif";
		                    option = "normal";
		                }
		                for( var i=0;i<count;i++)
		                {
				          oTarget.item(i).style.fontWeight = option;
		                }
				    }
				    break;
			    case "Italic":   //斜体[多]
				    if(oTarget && oTarget.item(0).style)
				    {
		                if(oTarget.item(0).style.fontStyle != "italic")
		                {
		                    document.getElementById('italic').src = "/images/text_italic_3.gif";
		                    option = "italic";
		                }
		                else
		                {
		                    document.getElementById('italic').src = "/images/text_italic_1.gif";
		                    option = "normal";
		                }
		                for( var i=0;i<count;i++)
		                {
					      oTarget.item(0).style.fontStyle = option;
				        }
				     }
				    break;
			    case "JustifyLeft":
				    if(oTarget && oTarget.item(0).style)
				    {
		                for( var i=0;i<count;i++)
		                {
		                    oTarget.item(i).style.position = "absolute";
		                    oTarget.item(i).style.left = "12px";
		                    oTarget.item(i).style.textAlign = "left";
                        }		
				    }
				    break;
			    case "JustifyCenter":
				    if(oTarget && oTarget.item(0).style){
		                for( var i=0;i<count;i++)
		                {
				             oTarget.item(i).style.position = "absolute";   
				             oTarget.item(i).style.textAlign = "center";          
                             var distance=(window.DocEditor.body.offsetWidth-parseFloat(oTarget.item(i).clientWidth))/2;
                             oTarget.item(i).style.left=distance+"px";
                             oTarget.item(i).style.textAlign = "left";
                        }
				    }
				    break;
			    case "JustifyRight":  //
				    if(oTarget && oTarget.item(0).style)
				    {
				        var width=window.DocEditor.body.offsetWidth;
		                for( var i=0;i<count;i++)
		                {
				           oTarget.item(i).style.position = "absolute";
				           oTarget.item(i).style.zIndex = 2;
	                       oTarget.item(i).style.left = (width-parseFloat(oTarget.item(i).clientWidth)-20)+"px";
	                       oTarget.item(i).style.textAlign = "left";
                        }
				    }
				    break;
		         case "Left":   //左对齐[多]
			          if(oTarget && oTarget.item(0).style){
				        var position= oTarget.item(0).style.left;
		                for( var i=1;i<count;i++)
		                {
				             oTarget.item(i).style.position = "absolute";
                             oTarget.item(i).style.left=position;
                             oTarget.item(i).style.textAlign = "left";
                        }		
				    }
				    break;
			    case "Center":  //居中对齐[多]
			        if(oTarget && oTarget.item(0).style)
			        {
				        var left=parseFloat(parseFloat(oTarget.item(0).style.left));
				        var len=parseFloat(oTarget.item(0).clientWidth);
		                for( var i=1;i<count;i++)
		                {  
		                    oTarget.item(i).style.left=((left+(len-parseFloat(oTarget.item(i).clientWidth))/2)+"px");
				            oTarget.item(i).style.position = "absolute";
                            oTarget.item(i).style.textAlign = "left";
                        }		
				    }
				    break;
			    case "Right":  //右对齐[多]
			     if(oTarget && oTarget.item(0).style)
			     {
				        var left=parseFloat(parseFloat(oTarget.item(0).style.left));
				        var len=parseFloat(oTarget.item(0).clientWidth);
		                for( var i=1;i<count;i++)
		                {
		                    oTarget.item(i).style.left=((left+len-parseFloat(oTarget.item(i).clientWidth))+"px"); 
				            oTarget.item(i).style.position = "absolute";
                            oTarget.item(i).style.textAlign = "left";
                        }		
				    }
				    break;
		         case "Top":  //顶部[多]
			             if(oTarget && oTarget.item(0).style)
			             {
				            var top=parseFloat(parseFloat(oTarget.item(0).style.top));
		                    for( var i=1;i<count;i++)
		                    {
   			                    oTarget.item(i).style.top=top+"px";
				                oTarget.item(i).style.position = "absolute";
                                oTarget.item(i).style.textAlign = "left";        
                            }		
				          }
					      break;
				  case "Bottom"://底部[多]
			          if(oTarget && oTarget.item(0).style)
			          {
				        var top=parseFloat(parseFloat(oTarget.item(0).style.top));
				        var len=parseFloat(oTarget.item(0).clientHeight);
		                for( var i=1;i<count;i++)
		                {
   			                oTarget.item(i).style.top=((top+len-parseFloat(oTarget.item(i).clientHeight))+"px");
				            oTarget.item(i).style.position = "absolute"; 
                        }		
				      }
					    break;
				    case "Middel"://居中[多]
			          if(oTarget && oTarget.item(0).style)
			          {
				        var top=parseFloat(parseFloat(oTarget.item(0).style.top));
				        var len=parseFloat(oTarget.item(0).clientHeight);
		                for( var i=1;i<count;i++)
		                {
   			                oTarget.item(i).style.top=((top+(len-parseFloat(oTarget.item(i).clientHeight))/2)+"px");
				            oTarget.item(i).style.position = "absolute";            
                        }		
				       }
					    break;
		        case "Paste":
			    case "Cut":case "Copy":case "Delete":case "Undo":case "Redo":
			    case "InsertOrderedList":case "InsertUnorderedList":
			    case "Underline":case "StrikeThrough":case "Superscript":case "Subscript":
			    case "JustifyFull":
				    window.DocEditor.execCommand(cmdName);
				    //window.location.reload();
				    break;
			    case "Style":
				    //alert("Style");
				    break;
			    case "Source":
				    //alert("Source");
				    break;
			    case "FitWindow":
				    //alert("FitWindow");
				    break;
                case "AddSpace":  //增加字间距[多]
              
                    if(oTarget && oTarget.item(0).style)
                    {
                      for( var i=0;i<count;i++)
                      {
                           var spacing= oTarget.item(i).style.letterSpacing; 
                           if(spacing==undefined || spacing=="" || spacing==null)
                           {
                              oTarget.item(i).style.letterSpacing="1px"; 
                           }
                           else 
                           {
                                
                               var spaceNum=parseInt(spacing);
                               //alert(spaceNum);
                               if(spaceNum<50)
                               {
                                   oTarget.item(i).style.letterSpacing=parseInt(spaceNum+1)+"px";
                               }
                               else
                               {
                                   alert('已达到最大字间距');
                               }
                           }
                        } 
    		        }
	    	        break;
			    case "MinerSpace": //减小字间距[多]
                    if(oTarget && oTarget.item(0).style)
                    {
                        for( var i=0;i<count;i++)
		                {      
                               var spacing=oTarget.item(i).style.letterSpacing;   
                               if(spacing==undefined || spacing=="" || spacing==null ||spacing=="normal")
                               {
                               }
                               else 
                               {
                                   var spaceNum=parseInt(spacing);
                                   if(spaceNum>0)
                                   {
                                     oTarget.item(i).style.letterSpacing=parseInt(spaceNum-1)+"px";
                                   }
                               }
                         }         
			         }
				    break;
			    case "LetterSpacing":
                    if(oTarget && oTarget.item(0).style)
                    {
                        var spacing = evtSrc.value;
		                for( var i=0;i<count;i++)
		                {
                           oTarget.item(i).style.letterSpacing=spacing;                    
                        }    
				    }
				    break;
			    case "setCenter"://pdf生成设置居中[多]
    				if(oTarget){
    				   //默认绑定匹配居中
                        var rng1;
    		            var option;
    		            rng1=oTarget.item(0);
    		            if(rng1.cmykcenter.indexOf("|")==-1 || rng1.cmykcenter.indexOf("|0|")!= -1)
    		            {
    		              document.getElementById('CENTER_LIST').src = "/images/29_2.gif";
    		              option = "|1|";
    		            }
    		            else
    		            {
    		              document.getElementById('CENTER_LIST').src = "/images/29_1.gif";
    		              option = "|0|";
    		            }
    				    var count= oTarget.length;
    		            var rng;
    		            for( var i=0;i<count;i++)
    		            {
    		                 rng=oTarget.item(i); 
    		                 if(rng.cmykcenter.indexOf("|")==-1)
    				            rng.cmykcenter=rng.cmykcenter+option;  
    		                 else if(rng.cmykcenter.indexOf("|0|")!= -1)
    		                 {
    		                    var temp = rng.cmykcenter;
    		                    rng.cmykcenter = temp.replace("\|0\|",option);
    		                 }
    		                 else if(rng.cmykcenter.indexOf("|1|")!= -1)
    		                 {
    		                    var temp = rng.cmykcenter;
    		                    rng.cmykcenter = temp.replace("\|1\|",option);
    		                 }
                        }
    				}
				    break;
				    case "setRight"://pdf生成设置居中[多]
    				if(oTarget){
    				   //默认绑定匹配居中
                        var rng1;
    		            var option;
    		            rng1=oTarget.item(0);
    		            if(rng1.cmykcenter.indexOf("|")==-1 || rng1.cmykcenter.indexOf("|0|")!= -1)
    		            {
    		              document.getElementById('RIGHT_LIST').src = "/images/29_2_you.gif";
    		              option = "|2|";
    		            }
    		            else
    		            {
    		              document.getElementById('RIGHT_LIST').src = "/images/29_1_you.gif";
    		              option = "|0|";
    		            }
    				    var count= oTarget.length;
    		            var rng;
    		            for( var i=0;i<count;i++)
    		            {
    		                 rng=oTarget.item(i); 
    		                 if(rng.cmykcenter.indexOf("|")==-1)
    				            rng.cmykcenter=rng.cmykcenter+option;  
    		                 else if(rng.cmykcenter.indexOf("|0|")!= -1)
    		                 {
    		                    var temp = rng.cmykcenter;
    		                    rng.cmykcenter = temp.replace("\|0\|",option);
    		                 }
    		                 else if(rng.cmykcenter.indexOf("|2|")!= -1)
    		                 {
    		                    var temp = rng.cmykcenter;
    		                    rng.cmykcenter = temp.replace("\|2\|",option);
    		                 }
                        }
    				}
				    break;
			    default:
				    //alert("尚未实现！");
				    break;
		    }
    //执行所有的命令后都保存一下		    
    //saveHistory();
	}
	else
	{
	    alert('请先选择您要操作的对象');
	}
}

function SaveAs(){
	var dialogUrl = "SaveAs.aspx?docId=";
	//OpenDialog(theURL,500,300);
	//OpenWindow(theURL,500,300,"WindowSaveAs");
	OpenDialog(dialogUrl,"dialogSaveAs",300,100,false);
}

function InsertText()
{
	var oDiv = window.DocEditor.createElement("div");
	oDiv.style.position = "absolute";
	//oDiv.style.overflow = "visible";
    oDiv.style.left = "20px";
	oDiv.style.top = "100px";
	oDiv.style.zIndex = 2;
	oDiv.style.height = 25;
	oDiv.style.whiteSpace = "nowrap";
	oDiv.style.fontFamily = "黑体";
	oDiv.style.fontSize = "20px";
	oDiv.innerHTML = "请输入文字";
	window.DocEditor.body.appendChild(oDiv);
	oDiv.setActive();
}

function InsertImage(){
    var sp = document.getElementById('hfsizeparm');
    if( sp != null)
    {
        var theURL = "/Design_MyPicBase_Standard.aspx?mul="+sp.value.split('|')[2];
	    windowOpening(theURL);
    }
	else
    {
        var theURL = "/Design_MyPicBase_Standard.aspx";
	    windowOpening(theURL);
    }
}


function InsertSpecialChar(){
	var theURL = "SpecialChar.htm";
	OpenWindow(theURL,500,300,"WindowSpecialChar");
}

function PickColor(){
    var theURL = "/DesignPic/ColorPicker.htm";
	 windowOpening(theURL);
}

function PickLineColor(){
    var theURL = "/DesignPic/ColorPickerLine.htm";
	 windowOpening(theURL);
}

function SwapBackImage(){
    var theURL ="BrowseImage.aspx?type=bgImg"; 
	 windowOpening(theURL);	
}
function  windowOpening(URL)
{
   window.open(URL,'','height=450,width=620,top=150,left=200,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
}	

function CanvasSize(){
	var theURL = "CanvasSize.htm";
	OpenWindow(theURL,260,150,"WindowCanvas");
}


function PopForeColor(){
}

function CreateColorPanel(){	
}

function FixImagePath(imagePath,fixString){
    var bgImgUrl = imagePath.replace("url(","");
    bgImgUrl = bgImgUrl.replace(")","");
    var strTemp = bgImgUrl.substr(0,bgImgUrl.indexOf(fixString));
    strTemp = strTemp + fixString;
    bgImgUrl = bgImgUrl.replace(strTemp,"");
    //alert(bgImgUrl+ fixString+imagePath);
    return bgImgUrl;
}


function OpenWindow(theURL,width,height,winName)
{
    var left = screen.width/2 - width/2;
    var top = screen.height/2 - height/2;
    var winStyle = "location=no,menubar=no,toolbar=no,minimizable=no,resizable=yes,scrollbars=yes,status=yes,";
    winStyle += "dependent=yes,dialog=yes,modal=yes,alwaysRaised=yes,";
    winStyle += "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left + "";
    var oWin = window.open(theURL,winName,winStyle,true);
 
}

function OpenDialog(dialogUrl,dialogName,dialogWidth,dialogHeight,showAsModal,dialogTop,dialogLeft){
	if(!dialogTop)
		dialogTop = (window.screen.height-dialogHeight)/2;
	if(!dialogLeft)
		dialogLeft = (window.screen.width-dialogWidth)/2;
	var features = "location=no,menubar=no,toolbar=no,minimizable=no,resizable=yes,dependent=yes,dialog=yes,modal=yes,alwaysRaised=yes,";
	features += "width=" + dialogWidth + ",height=" + dialogHeight + ",top=" + dialogTop + ",left=" + dialogLeft + "";
	window.SubWin = window.open(dialogUrl,dialogName,features);
	window.SubWin.focus();
	if(showAsModal){
		window.CtrlsDisabled = new Array();
		DisableCtrls("INPUT;TEXTAREA;BUTTON;SELECT");
	}

	function DisableCtrls(tagNameStr){
		var arrTags = tagNameStr.split(";");
		for(var i=0;i<arrTags.length;i++){
			var arrEle = document.getElementsByTagName(arrTags[i]);
			for(var j=0;j<arrEle.length;j++){
				if(!arrEle[j].disabled && !arrEle[j].readOnly){
					window.CtrlsDisabled.push(arrEle[j]);
					arrEle[j].readOnly = true;
					arrEle[j].disabled = true;
				}
			}
		}
	}
	
	window.onfocus = function(){
		if(window.SubWin && showAsModal){
			if(window.SubWin.closed == true || typeof(window.SubWin.closed) == "undefined"){
				for(var i=0;i<window.CtrlsDisabled.length;i++){
					window.CtrlsDisabled[i].readOnly = false;
					window.CtrlsDisabled[i].disabled = false;
				}
			}else{
				window.SubWin.focus();
			}
		}
	}
}

function Align(objRef,objTarget,alignMode){
	
}
function Preview(){
	
}
function Rotate(){
	var obj = window.DocEditor.getElementById("divId");
	if(obj.style.writingMode != "tb-rl")
		obj.style.writingMode = "tb-rl";
	else
		obj.style.writingMode = "lr-tb";
}


/**
 * @author lovebbbird
 */
var myHistory = new Object;
myHistory.data = [];
myHistory.text = [];
myHistory.position = 0;
myHistory.saved = false;

function saveHistory(){
	myHistory.saved = true;
	var html = getHTML();
	var text = getText();
	//html有变化
    //如果备注text文本框信息!=当前文本框信息，则同步。
    if (myHistory.data[myHistory.position] != html) {
	    var nBeginLen = myHistory.data.length;
	    var nPopLen = myHistory.data.length - myHistory.position;
	    for (var i = 1; i < nPopLen; i++) {
		    myHistory.data.pop();
		    myHistory.text.pop();
	    }
	    //myHistory.data.splice(myHistory.position,nPopLen);
	    myHistory.data[myHistory.data.length] = html;
	    myHistory.text[myHistory.text.length] = text;
	    if (nBeginLen != 0) {
		    myHistory.position++;
	    }
    }
}

function Undo()
{
   window.clearInterval(recordhistory);
   goHistory(-1);
   recordhistory = window.setInterval("saveHistory()", 1000);
}

function Redo()
{
    window.clearInterval(recordhistory);
    goHistory(1);
    recordhistory = window.setInterval("saveHistory()", 1000);
}

//
function getHTML(){
	var html;
	html = window.DocEditor.body.innerHTML;
	if(window.DocEditor.getElementById('myrect'))
	{
	    html =  window.DocEditor.body.innerHTML.replace(window.DocEditor.getElementById('myrect').outerHTML,'');
	}
	if ((html.toLowerCase() == "<p>&nbsp;</p>") || (html.toLowerCase() == "<p></p>")) {
		html = "";
	}
	html = html.replace(/UNSELECTABLE="on">\s*/ig,'UNSELECTABLE="on">');
	return html;
}

function getText(){
    var text="";
    var objs = form1.document.getElementsByTagName("INPUT");
    for(var i = 0; i < objs.length; i++){
        if(objs[i].className != null && objs[i].className == "textf1"){
            text += objs[i].id + "|" + objs[i].value + "^";
        }
    }
    text += "SelectText" + "|" + document.getElementById('SelectText').value;
	return text;
}

function initHistory(){
	myHistory.data.length = 0;
	myHistory.text.length = 0;
	myHistory.position = 0;
	myHistory.saved = false;
	//定时1秒保存
}

function goHistory(value){
	if (!myHistory.saved) {
		saveHistory();
	}
	if (value == -1) {
		if (myHistory.position > 0) {
	
			myHistory.position = myHistory.position - 1;
			setText(myHistory.text[myHistory.position]);
			setHTML(myHistory.data[myHistory.position], true);
		}
	}
	else {
		if (myHistory.position < myHistory.data.length - 1) {
		 
			myHistory.position = myHistory.position + 1;
			setText(myHistory.text[myHistory.position]);
			setHTML(myHistory.data[myHistory.position], true);
		}
	}
}

function setText(text){
    var textarray = text.split('^');
    for(i= 0;i< textarray.length;i++)
    {
        var ta = textarray[i].split('|');
        document.getElementById(ta[0]).value = ta[1];
    }
}

function setHTML(html, b_NotSaveHistory){
	//ContentEdit.value = html;
	//alert(html);
	if(document.getElementById("HiddenLoad").value == "TagBack")
	{
		document.getElementById("HFBackContent").value = html;
	}
	else
	{
		document.getElementById("HFFrontContent").value = html;
	}
	if (!b_NotSaveHistory) {
		saveHistory();
	}
	loadbody();
}

function loadbody()
{
        var strInitContent; 
        var templateSide  = document.getElementById("HiddenLoad").value;
        window.IfrmEditor = document.getElementById("ifrmEditor");
        window.WinEditor  = window.IfrmEditor.contentWindow;
        window.DocEditor  = window.WinEditor.document;
        window.IfrmEditor.unselectable = "off";
        loadPic(templateSide);
        if(templateSide=="TagBack")
        {  
           strInitContent = document.getElementById("HFBackContent").value;
        }
        else
        {
           strInitContent = document.getElementById("HFFrontContent").value;
        }
        strInitContent=strInitContent.replace(/amp;/g,"");
        strInitContent=strInitContent.replace(/&lt;/g,"<");
        strInitContent=strInitContent.replace(/&gt;/g,">");
        window.DocEditor.body.innerHTML = strInitContent;
        MakeEditable();	
        
        //alert(window.DocEditor.body.innerHTML);
	   
//        if(document.getElementById("HiddenLoad").value == "TagFront")
//        {
//            if(document.getElementById("imgb") != null)
//            {
//                document.getElementById("imgb").style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=30)";
//            }
//        }
//        else
//        {
//            if(document.getElementById("imgf") != null)
//            {
//                document.getElementById("imgf").style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=30)";
//            }
//        }
//        var imgarr = document.images;
//        if (imgarr != null)
//        {
//            for (i=0;i<imgarr.length;i++)
//            {
//                if(imgarr(i).name == "ImagePic")
//                {
//                    var imag=imgarr(i);
//                    if(imag.width>imag.height)
//                    {
//                        if(imag.offsetwidth>77)
//                        {
//                            imag.style.width="77px";
//                        }
//                        if(imag.offsetHeight>77)
//                        {
//                            imag.style.height="77px";
//                        } 
//                    }
//                    else
//                    {
//                        if(imag.offsetHeight>77)
//                        {
//                            imag.style.height="77px";
//                        } 
//                        if(imag.offsetwidth>77)
//                        {
//                            imag.style.width="77px";
//                        }
//                    }  
//                }
//           }
//       }
//       //字体初始化(indexOf)
//       if(document.getElementById('fontFamily').value == "")
//       {
//         var a = parent.document.all('dlgHelper').fonts;
//         for (i = 1;i <= a.count;i++)
//         { 
//            if(validFonts.indexOf(','+a(i)+',') != -1)
//            {
//                var opt;
//	            opt = document.createElement("OPTION");
//	            opt.text = a(i);
//	            opt.value = a(i);
//	            opt.style.color ="green";
//		        document.getElementById('fontFamily').add(opt);
//            }
//         } 
//	 }
//	 if(document.getElementById("H1").value != "1")
//	 {
//         settextout();
//     }
//     else
//     {
//         gotohigh();
//     }
     return false;
}