var face_count=40;
var face_head="/home/main/images/smile/";
var face_tail=".gif";

function checkBrowserType()
{
  var app=navigator.appName;
  if (app.indexOf('Microsoft') != -1) {
    return 0;
  }
  if (app.indexOf('Netscape') != -1) {
    return 1;
  }
}

function EasyEditor(editorId){
	this.editorId=editorId;
}
EasyEditor.prototype={	
	addFaceSelector:function(){
	var face_selector_div="<div><img src=\""+face_head+"ec_1"+face_tail+"\" id=\"faceSelector_"+this.editorId+"\"/></div>";
	document.write(face_selector_div);
	this.faceSelector=document.getElementById("faceSelector_"+this.editorId);
	},
	addFaceImageDiv:function(){
		var facesDiv="<div id=\"facesDiv_"+this.editorId+"\" class=\"faceDiv\">";	  
		for(var i=1;i<=face_count;i++){
			var _img="<img src=\""+face_head+i+face_tail+"\" id=\"face_image_"+this.editorId+"_"+i+"\" class=\"facesImg\"/>";
			facesDiv+=_img;        
		}
		facesDiv+="</div>";
		document.write(facesDiv);
		this.facesDiv=document.getElementById("facesDiv_"+this.editorId);	
	},
	addFrame:function(){
		var frameId="frame_"+this.editorId;
		var frameString="<iframe frameborder=\"0\" scrolling=\"auto;\" marginwidth=\"0\" id=\""+frameId+"\" class=\"editorFrame\"></iframe>&nbsp;";
		document.write(frameString);
		this.frame=document.getElementById(frameId);
		this.editorArea= this.frame.contentWindow;
		this.editorArea.document.designMode = "on";
		this.editorArea.document.contentEditable = true; 
		this.editorArea.document.open(); 
		this.editorArea.document.writeln('<html><head>');
		this.editorArea.document.writeln("<style>body{cursor:text;background: #ffffff; margin:1px; padding:1px; font-size:14px; overflow:auto;word-wrap: break-word; font-family: Arial, \"宋体\";} p {padding: 0px; margin: 0px;}</style>");
		this.editorArea.document.writeln('</head>');
		this.editorArea.document.writeln('<body></body>');
		this.editorArea.document.writeln('</html>');
		this.editorArea.document.close();	
	},
	attachFaceAction:function(){
		for(var i=1;i<=face_count;i++){
			var imgObj=document.getElementById("face_image_"+this.editorId+"_"+i);
			imgObj.insertFace=this.insertFace;
			imgObj.insertHtml=this.insertHtml;
			imgObj.editorArea=this.editorArea;
	    imgObj.setup=this.setup;
	    imgObj.facesDiv=this.facesDiv;
			imgObj.onmouseover=function(){this.style.cursor="pointer";this.style.border="1px solid #D01E3B";};
			imgObj.onmouseout=function(){this.style.border="1px solid #CCCCCC";};
	    imgObj.onclick=function(){this.insertFace(this.src);this.facesDiv.style.display="none";};
	}
	},
	setup:function(command,content){
		this.editorArea.focus();
		this.editorArea.document.execCommand(command, false, content);	
	},
	insertHtml:function(content){
		if(checkBrowserType()==0){
		  this.editorArea.focus();
		  this.editorArea.document.selection.createRange().pasteHTML(content);
		}else{			
		  this.setup("insertHTML",content);
		}
	},
	getHtml:function(){
	    return this.editorArea.document.body.innerHTML;	
	},
    insertFace:function(imgPath){
		var htmlContent="<img src=\""+imgPath+"\"></img>";
		this.insertHtml(htmlContent);
	},	
 keyPress:function(){
 	  var ev=this.editorArea.event;
    if(ev.keyCode==13){
        this.insertHtml("<br/>");
        return false;  
    }  
     return true;  
  },
	init:function(){
		 this.addFaceSelector();
		 this.addFaceImageDiv();
		 this.addFrame();
		 this.attachFaceAction();
	 	 this.faceSelector.facesDiv=this.facesDiv;
		 this.faceSelector.onmouseover=function(){this.style.cursor="pointer";};
		 this.faceSelector.onclick=function(){var divStyle=this.facesDiv.style;divStyle.display=="block"?divStyle.display="none":divStyle.display="block";};
	}
}