function HtmlEditor(name,inputor,faceDiv)
{
    this._name = name;
    this.inputor =  document.getElementById(inputor).contentWindow ? document.getElementById(inputor).contentWindow : null;
    this.faceDiv  = document.getElementById(faceDiv) ? document.getElementById(faceDiv) : null;
    
    this.setInputor();
    
    if(this.faceDiv !=null)
    {
        var lghtml = new InstQuery("/template/share/face.html", null, function(faceDiv, oReq){document.getElementById(faceDiv).innerHTML = oReq.responseText;});
        lghtml.setParams(faceDiv);
	    lghtml.execute();
    }
    
    this.getHtml =function(){
        if(this.inputor == null) return '';
        var return_html =  this.inputor.document.body.innerHTML;
	   if(return_html.toLowerCase()=="<p>&nbsp;</p>"
		  || return_html.toLowerCase()=="<br/>"
		  || return_html.toLowerCase()=="<br />"
		  || return_html.toLowerCase()=="<br>")	return '';
		
        return return_html;
    }
    
    this.clearHtml = function() {
        if(this.inputor == null) return false;
        this.inputor.document.body.innerHTML="";
    }
    
    
    this.SetSayFocus = function() {
        if(this.inputor == null) return false;
        this.inputor.focus();
    }
    
    this.ShowFaceDiv  = function (obj,x,y)
    {
        //alert(this.faceDiv);
      if(this.faceDiv == null || this.faceDiv.innerHTML =='' ) return false;
      if(this.faceDiv.style.display == 'none')
      {
        x = parseInt(x);
        y = parseInt(y);
        //alert(this.x(obj)+','+this.y(obj)+','+x+','+y);   
        this.faceDiv.style.left = this.x(obj)+x+"px";
	    this.faceDiv.style.top = this.y(obj)+y+"px";
	    this.faceDiv.style.display = "block";
      }else
      {
        this.faceDiv.style.display = "none";  
      }
      return false;
    }
    
    
	//取得一个对象的X坐标
    this.x = function (element){
		var positionX = 0;
		while (element != null)
		{
			positionX += element.offsetLeft;
			element = element.offsetParent;
		}
		return positionX;
    }
    
    //取得一个对象的y坐标
	this.y= function (element){
		var positionY = 0;
		while (element != null)
		{
			positionY += element.offsetTop;
			element = element.offsetParent;
		}
		return positionY;
	}
	
	if(this.inputor != null)
	{
	   //alert("set");
	   //this.inputor.setAttribute('doname',this._name);
	   if(isIE())
	   {
	       this.inputor.className  = this._name;//找一个属性来用
	       this.inputor.onfocus = function () {
		      this._timeout = setTimeout(this.className+".HiddenFaceDiv()",10);
		      //clipboardData.clearData('URL');
		      //clipboardData.clearData('HTML');
		      //clipboardData.clearData('Image');
	       }
	   }
	}
};

HtmlEditor.prototype.setInputor = function(){
    if(this.inputor != null && this.inputor.document!= null)
    {
       var bodyHtml="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<meta http-equiv=\"content-language\" content=\"utf-8\" />\n<style type=\"text/css\">body{font-size:12px;font-family:Arial;background-color:#fff}\n *{margin:0px;padding:0px}\n</style>\n</head>\n<body></body>\n</html>"; 
	   this.inputor.document.open(); 
	   this.inputor.document.write(bodyHtml); 
	   this.inputor.document.close(); 
	   this.inputor.document.designMode="On"; 
	   this.inputor.focus();
    }
      
}

HtmlEditor.prototype.setFaceDiv = function(){
   if( this.faceDiv.innerHTML == '')
   {
       this._timeout = setTimeout(this._name+".setFaceDiv()",200);
   }else
   {
       var ch = this.faceDiv.childNodes;
       for(var i=0 ; i<ch.length;i++)
       {
	      //alert(ch[i].tagName);return;
	       if(ch[i].tagName == 'IMG')
	      {
	          ch[i].className='face_out';
	          ch[i].setAttribute('doname',this._name);
	          ch[i].onmouseover = function(){this.className='face_in';}
	          ch[i].onmouseout = function(){this.className='face_out';}
	          ch[i].onclick= function(){
	              setTimeout(this.getAttribute('doname')+".putSayVal('"+this.src+"','"+this.title+"')",10);
	          }
	      }
       }
   }
}
HtmlEditor.prototype.clearFaceDiv =function(){
    if(this.faceDiv == null)return false;
    this.faceDiv.innerHTML='';
}

HtmlEditor.prototype.putSayVal=function(val,title){
    var chatInputObj = this.inputor;
    chatInputObj.focus();
    var html='<img title="'+title+'" src="'+val+'" />';
     ///*   
    if(isIE())
    {
        /*
          var tm='face_img_'+getTimeStamp();
          //alert(tm);
          chatInputObj.document.execCommand('InsertInputImage',false,tm); 
          chatInputObj.document.getElementById(tm).src = val;
          chatInputObj.document.getElementById(tm).title = title;
          chatInputObj.document.getElementById(tm).blur();
          */
        
        var range = chatInputObj.document.selection.createRange();
		var seltype = chatInputObj.document.selection.type;
		if(range && seltype != "Control")
		{
			range.pasteHTML(html);
			range.select();
		}
		          
    }else
    {
       
        //alert(html);
        chatInputObj.document.execCommand("insertHTML", false, html);
    }
	//*/	
	chatInputObj.focus();
	this.HiddenFaceDiv();
	
}

HtmlEditor.prototype.HiddenFaceDiv = function (){
    this.faceDiv.style.display = "none";
}

function isIE() {
		var ua = navigator.userAgent;
		var s = "MSIE";
		var i;
		if ((i = ua.indexOf(s)) >= 0) {
			return true;
		}
		return false;
	}
