
var facetitle = ["大笑","微笑","亲亲","抱抱","色色","好失望哟","好困哦","害羞","酷呆了","晕倒","眨眼","鬼脸","小声点",
"吃惊","翻白眼","干杯","困惑","啥？","睡觉","再见了","眼泪哗哗地","你好讨厌","我吐","怒骂","闭嘴","打你",
"真的生气了","超级棒","不咋地","魅力四射","心都碎了","爱","吻","玫瑰凋谢了","玫瑰盛开了","生日蛋糕","礼物","苹果","西瓜",
"咖啡","足球","握手","星星","精灵","小丑","大怒","生病了","小可爱","小心非典","嘴馋了","警察","抓狂了",
"不爽","汗","思考中","见钱眼开","呲牙咧嘴","晕头转向","好好爱你哦","猪头","便便","月亮","音乐","饭","真衰",
"偷笑","下雨","猫猫","狗狗","骷髅","书呆子","太阳","邮件","帅帅男孩","妩媚女孩","药丸","鄙视","烧香"]


//浏览器类型
var e_isOpera = false;
var e_isIE = false;
var e_isFF = false;
//工具按钮开关
var e_bold = true;
var e_italic = true;
var e_underline = true;
var e_font = true;
var e_fontsize = true;
var e_color = true;
var e_img = true;
var e_link = true;
var e_media = true;
var e_face = true;
var e_uploadimg = false;
var e_piclink = false;
var e_grppiclink = false;
var e_iframeno = 0;

var g_gid = 0;

//回调函数
var e_onkeyup = null;
var e_insFace=null;

function webEditor(id,editorArea,width,height,toolbarpos) {
	//初始化变量
	this.iframeno = e_iframeno++;
	this.id = id;
	this.editorArea = editorArea;
	this.width = width;
	this.height = height;
	this.toolbarpos = toolbarpos;
	//组件
	this.toolbar = null;
	this.iframe = null;
	this.textArea = null;
	//信号量
	this.range = null;
	this.seltype = null;
	this.openPanel = '';
	//上传图片文件
	this.uploadfile = "";
	//相册图片
	this.picfile = "";
	//群相册图片
	this.grppicfile = "";
	
	this.preContent="";
	this.preLength=0;
}

webEditor.prototype.community_url = 'http://happy.xiaoi.com';
webEditor.prototype.images_url = 'http://photo.xiaoi.com';

webEditor.prototype.getUA = function() {
	var UA = navigator.userAgent.toLowerCase();
	e_isOpera  = (UA.indexOf('opera') != -1);
	e_isFF = (UA.indexOf('firefox')!= -1);
	e_isIE = document.all ? true : false;
}

webEditor.prototype.drawEditor = function() {
	var htmlstr;
	
	if(e_isOpera) {
		htmlstr = '<textarea style="font-size:14px;width:'+this.width+'px;height:'+this.height+'px;" id="'+this.id+'_aid" name="'+this.id+'_aid"></textarea>';
	}
	else {
		var framename = this.id+'_fid_'+(++this.iframeno);
		if (this.toolbarpos == "right") {
			htmlstr = '<div id="'+this.id+'_eid" style="float:left;border:1px #808080 solid;width:'+(this.width-50)+'px;height:'+this.height+'px;"><div class="face_sign_it1"><iframe id="'+framename+'" name="'+framename+'" style="width:'+(this.width-50-2)+'px; height: '+(this.height-2)+'px;" frameborder=0 marginwidth=0 scrolling=auto src="'+this.community_url+'/common/innerIframe.html" onfocus="javascript:'+this.id+'.iframeOnfocus();" onblur="javascript:'+this.id+'.iframeOnblur();"></iframe></div></div><div id="'+this.id+'_tid" style="float:left;background-color:#fff;"></div><div style="clear:both;"></div>';
		}
		else {
			htmlstr = '<div id="'+this.id+'_tid" style="width:'+(this.width+2)+'px;background-color:#fff;"></div>'
				+'<div id="'+this.id+'_eid" style="border:1px #808080 solid;width:'+this.width+'px;height:'+this.height+'px;"><div class="it1"><iframe id="'+framename+'" name="'+framename+'" style="width:'+(this.width-2)+'px; height: '+(this.height-2)+'px;" frameborder=0 marginwidth=0 scrolling=auto src="'+this.community_url+'/common/innerIframe.html" onfocus="javascript:'+this.id+'.iframeOnfocus();" onblur="javascript:'+this.id+'.iframeOnblur();"></iframe></div></div>';
		}
	}
	this.editorArea.innerHTML = htmlstr;
}

webEditor.prototype.iframeOnfocus = function() {
	document.getElementById(this.id+"_eid").childNodes[0].className = "face_sign_it2";
}

webEditor.prototype.iframeOnblur = function() {
	if(!this.range) {
		document.getElementById(this.id+"_eid").childNodes[0].className = "it1";
	}
}

webEditor.prototype.genObj = function() {
	if(e_isOpera) {
		this.textArea = document.getElementById(this.id+'_aid');
	}
	else {
		this.toolbar = document.getElementById(this.id+'_tid');
		var framename = this.id+'_fid_'+this.iframeno;
		this.iframe= window.frames[framename];
	}
}

webEditor.prototype.setText = function(str) {
	if(typeof str=='undefined') {
		str = "";
	}
	return str;
}

webEditor.prototype.setKeyupHandler = function (keyHandler) {
	var frameobj = this.iframe;
	var objC = this;
	var doc = frameobj.document;
	if (doc && keyHandler) {
		if (doc.addEventListener) {
			doc.addEventListener(
				'keyup',
				function(event){keyHandler(objC,event);},
				false
			);
		}
		else if (doc.attachEvent) {
			doc.attachEvent(
				'onkeyup',
				function (event) { keyHandler(objC,event); }
			);
		}
		else {
			doc.onkeyup = function(event){keyHandler(objC,event);};
		}
	}
}
webEditor.prototype.frameLoaded  = function() {
	//this.iframe.document.designMode = "On";
	//this.iframe.document.contentEditable = true;
	this.str = this.setText(this.str);
	this.setKeyupHandler(e_onkeyup);
	this.setContent(this.str);
}
webEditor.prototype.init = function(str) {
	var self = this;
	var timer = setInterval(function(){
		try{
			var s = self.iframe.document;
			self.frameLoaded();
			clearInterval(timer);
		}catch(e){}
	},500);
	
	this.getUA();
	this.drawEditor();
	this.genObj();
	this.createToolBar();
	this.str = str;
	
	if(e_isOpera) {
		this.textArea.value = str;
		return;
	}
}

webEditor.prototype.getText = function() {
	if(e_isOpera) {
		return this.textArea.value;
	}
	else if (e_isIE) {
		return this.iframe.document.body.innerText;
	}
	else {
		return this.iframe.document.body.innerHTML;
	}
}

webEditor.prototype.setContent = function(str) {
	if(e_isOpera) {
		this.textArea.value = str;
	}
	else {
		this.iframe.document.body.innerHTML = str;
	}
}

webEditor.prototype.getHtml = function() {
	if(e_isOpera) {
		return this.textArea.value;
	}
	var return_html =  this.iframe.document.body.innerHTML;
	if(return_html.toLowerCase()=="<p>&nbsp;</p>"
		|| return_html.toLowerCase()=="<br/>"
		|| return_html.toLowerCase()=="<br />"
		|| return_html.toLowerCase()=="<br>")
		return '';
	//ie
	return_html = return_html.replace(/<IMG style=\"WIDTH: (\d*)px; HEIGHT: (\d*)px\"[^<]*src=(\"[^\"]+\")[^<]*>/gi, '<img width=$1 height=$2 src=$3>');
	//firefox
        return_html = return_html.replace(/<img style=\"width: (\d*)px; height: (\d*)px;\"[^<]*src=(\"[^\"]+\")[^<]*>/gi, '<img width=$1 height=$2 src=$3>');
	return return_html;
}

webEditor.prototype.getFocus = function() {
	if(e_isOpera) {
		this.textArea.focus();
	}
	else {
		this.iframe.focus();
	}
}

webEditor.prototype.getTextType = function() {
	if(e_isOpera) {
		return "plain";
	}
	else {	
		return "html";
	}
}

webEditor.prototype.selectBegin = function() {
	this.iframe.focus();
	if (e_isIE) {
		this.range = this.iframe.document.selection.createRange();
		this.seltype = this.iframe.document.selection.type;
	}
}

webEditor.prototype.drawPanel = function(inner,name) {
	var pid = this.id + "_" + name;
	if(this.openPanel !='') {
		this.removePanel(this.openPanel);
	}
	this.openPanel = pid;
	tdiv=document.createElement("div");
	tdiv.id = pid;
	tdiv.innerHTML=inner;	
	tdiv.style.display="block";	
	tdiv.style.position="absolute";
	tdiv.style.zIndex = '999999';

	var btn_ele = this.id+"_"+name+'_icon';
	var btn = ((btn_ele=='')?document.getElementById(this.id+'_eid'):document.getElementById(btn_ele));
	var btn_pos = b_getAbsolutePos(btn);
	tdiv.style.top = ((btn_ele=='')?btn_pos.top:(btn_pos.top+28)) + "px";
	tdiv.style.left = (btn_pos.left+1) + "px";
	document.body.appendChild(tdiv);
	tdiv.focus();
}

webEditor.prototype.removePanel = function(panelid) {
	if(document.getElementById(panelid)) {
		document.body.removeChild(document.getElementById(panelid));
		if(e_isIE && this.range && this.seltype != "Control") {
			this.range.select();
		}
		this.range = null;
	}
}

webEditor.prototype.autoClose = function(pid) {
	setTimeout(this.id+".removePanel('"+pid+"')", 1000);
}

function e_swapImgRestore() {
	var i,x,a=document.e_sr; 
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
		x.src=x.oSrc;
}

function e_findObj(n, d) {
	var p,i,x;  
	if(!d) 
		d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length)
	{
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=e_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	return x;
}

function e_swapImage() {
	var i,j=0,x,a=e_swapImage.arguments; 
	document.e_sr=new Array; 
	for(i=0;i<(a.length-2);i+=3)
		if ((x=e_findObj(a[i]))!=null) {
			document.e_sr[j++]=x; 
			if(!x.oSrc) {
			 x.oSrc=x.src; 
			}
			x.src=a[i+2];
		}
}

webEditor.prototype.createToolBar = function() {
	var str ='<table ellpadding="0"cellspacing="0"><tr>';
	//笑脸
	str +=(e_face?'<td width="29"><div id="'+this.id+'_face_icon" onclick="javascript:'+this.id+'.facePanel();" onMouseOut="e_swapImgRestore()" onMouseOver="e_swapImage(\'face\',\'\',\'http://static1.xiaoi.net/images/face/ec_1.gif\',0)"><img src="http://static1.xiaoi.net/images/face/ec_1.gif" name="face" border="0"></div></td>':'');
	str +='</tr></table>';
	this.toolbar.innerHTML = str;
}

webEditor.prototype.insertHTML = function(html) {
	if (e_isIE) {
		if (!this.range) {
			this.selectBegin();
		}
		if(this.range && this.seltype != "Control") {
			this.range.select();
			this.range.pasteHTML(html);
		}
		this.range = null;
	}
	else {
		this.iframe.document.execCommand("insertHTML", false, html);
		this.iframe.focus();
	}
}

webEditor.prototype.facePanel = function() {
	this.selectBegin();

	if(document.getElementById(this.id + '_face')) {
		document.body.removeChild(document.getElementById(this.id + '_face'));
		return;
	}
	var pContent = '';
	pContent +='<div onmouseleave="javascript:'+this.id+'.autoClose(\''+this.id+'_face\')" class="face_sign_e_d1" id="face_sign_ec"><div class="face_sign_e_d2"><div class="face_sign_e_d3"><div class="face_sign_e_d4"><div >';
	for(i=0;i<6;i++) {
		for(j=1;j<=13;j++) {
			var tt = (i*13+j).toString();
			var t = 'http://static1.xiaoi.net/images/face/'+tt+'.gif';
			pContent += '<div class="face_sign_emo_of" onmouseover="this.className=\'face_sign_emo_on\';" onmouseout="this.className=\'face_sign_emo_of\';" onclick="javascript:'+this.id+'.insFace(\''+t+'\');"><img src="'+t+'" title="'+facetitle[i*13+j-1]+'" width="19" height="19" /></div>';
		}
	}
	pContent +='<div class="face_sign_c"></div>  </div></div></div></div></div>';
	this.drawPanel(pContent,'face');
}

webEditor.prototype.insFace = function(img) {
	var arr = img.split('/');
	arr = arr[arr.length-1].split('.');
	var order = arr[0];

	var str = '';
	if (img != '') {
		str = "<img src='"+img+"' title='"+facetitle[order-1]+"' name='disabled' border=0/>";
	}
	
	this.insertHTML(str);
	
	this.removePanel(this.id +'_face');
	
	var obj = this;
	if(e_insFace) {
		e_insFace(obj,str);
	}
}

webEditor.prototype.showerror = function(v_errno, v_error) {
	this.removePanel(this.id+'_image');
}

webEditor.prototype.uploadComplete = function(v_link) {
	this.uploadfile += v_link + ",";
	var imghtml = "<img src='"+v_link+"'>";

	this.insertHTML(imghtml);
	
	this.removePanel(this.id+'_image');
}

webEditor.prototype.selectPicComplete = function(v_link) {
	this.picfile += v_link + ",";
	var imghtml = "<img src='"+v_link+"'>";

	this.insertHTML(imghtml);
	
	this.removePanel(this.id+'_image');
}

webEditor.prototype.selectGrpPicComplete = function(v_link) {
	this.grppicfile += v_link + ",";
	var imghtml = "<img src='"+v_link+"'>";

	this.insertHTML(imghtml);
	
	this.removePanel(this.id+'_image');
}

function b_getAbsolutePos(element) { 
	if ( arguments.length != 1 || element == null ) { 
		return null; 
	} 
	var elmt = element; 
	var offsetTop = elmt.offsetTop; 
	var offsetLeft = elmt.offsetLeft; 
	var offsetWidth = elmt.offsetWidth; 
	var offsetHeight = elmt.offsetHeight; 
	while( elmt = elmt.offsetParent ) { 
		// add this judge 
		if ( elmt.style.position == 'absolute' 
//		    || elmt.style.position == 'relative'  
		    || ( elmt.style.overflow != 'visible' && elmt.style.overflow != '' ) ) 
		{ 
			break; 
		}  
		offsetTop += elmt.offsetTop; 
		offsetLeft += elmt.offsetLeft; 
	} 
	return {top:offsetTop, left:offsetLeft, right:offsetWidth+offsetLeft, bottom:offsetHeight+offsetTop }; 
} 


