var facetitle = ["大笑","微笑","亲亲","抱抱","色色","好失望哟","好困哦","害羞","酷呆了","晕倒","眨眼","鬼脸","小声点",
"吃惊","翻白眼","干杯","困惑","啥？","睡觉","再见了","眼泪哗哗地","你好讨厌","我吐","怒骂","闭嘴","打你",
"真的生气了","超级棒","不咋地","魅力四射","心都碎了","爱","吻","玫瑰凋谢了","玫瑰盛开了","生日蛋糕","礼物","苹果","西瓜",
"咖啡","足球","握手","星星","精灵","小丑","大怒","生病了","小可爱","小心非典","嘴馋了","警察","抓狂了",
"不爽","汗","思考中","见钱眼开","呲牙咧嘴","晕头转向","好好爱你哦","猪头","便便","月亮","音乐","饭","真衰",
"偷笑","下雨","猫猫","狗狗","骷髅","书呆子","太阳","邮件","帅帅男孩","妩媚女孩","药丸","鄙视","烧香"]


//浏览器类型
var e_isOpera = false;
var e_isIE = false;
var e_isFF = false;
//工具按钮开关
var e_face = true;
var e_iframeno = 0;
var e_toolbarbg = "#ffffff";

var g_gid = 0;
var g_inputchange = 0;

//回调函数
var e_onkeypress = null;

function webEditor(id,editorArea,width,height)
{
	//初始化变量
	this.iframeno = e_iframeno++;
	this.id = id;
	this.editorArea = editorArea;
	this.width = width;
	this.height = height;
	//组件
	this.iframe = null;
	this.textArea = null;
	//信号量
	this.range = null;
	this.seltype = null;
	this.openPanel = '';
}

function gEditor_SetInputTime()
{
	var now = new Date();
	g_inputtime = now.getTime();	
}

webEditor.prototype.$ = function(s)
{
	if(document.getElementById)
	{
		return eval('document.getElementById("' + s + '")');
	}
	else
	{
		return eval('document.all.' + s);
	}
}

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);
		htmlstr = '<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="" onfocus="javascript:'+this.id+'.iframeOnfocus();" onblur="javascript:'+this.id+'.iframeOnblur();"></iframe></div></div>';
	}
	this.editorArea.innerHTML = htmlstr;
}

webEditor.prototype.iframeOnfocus = function()
{
	this.$(this.id+"_eid").childNodes[0].className = "it2";
	g_inputchange = setInterval(gEditor_SetInputTime, 1000);
}

webEditor.prototype.iframeOnblur = function()
{
	if(!this.range)
	{
		this.$(this.id+"_eid").childNodes[0].className = "it1";
	}
	if(g_inputchange)
	{
		clearInterval(g_inputchange);
	}
}

webEditor.prototype.genObj = function()
{
	if(e_isOpera)
	{
		this.textArea = this.$(this.id+'_aid');
	}
	else
	{
		this.toolbar = this.$(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 "<html><head><style type=\"text/css\">body {background: #ffffff; margin:1px; padding:1px; font-size:14px; overflow:auto;word-wrap: break-word; font-family: Arial, '宋体';} p {padding: 0px; margin: 0px; } </style></head><body>"+str+"</body></html>";
}

webEditor.prototype.setKeypressHandler = function (keyHandler)
{
	var frameobj = this.iframe;
	var doc = frameobj.document;
	if (doc && keyHandler)
	{
		if (doc.addEventListener)
		{
			doc.addEventListener(
				'keypress',
				keyHandler,
				false
			);
		}
		else if (doc.attachEvent)
		{
			doc.attachEvent(
				'onkeypress',
				function () { keyHandler(frameobj.event); }
			);
		}
		else
		{
			doc.onkeypress = keyHandler;
		}
	}
}

webEditor.prototype.init = function(str)
{
	this.getUA();
	this.drawEditor();
	this.genObj();
	if(e_isOpera)
	{
		this.textArea.value = str;
		return;
	}
	str = this.setText(str);
	this.iframe.document.open();
	this.iframe.document.write(str); 
	this.iframe.document.close(); 
	this.iframe.document.designMode = "On";
	this.iframe.document.contentEditable = true;
	this.setKeypressHandler(e_onkeypress);
}

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;//copyClipBoard(inner);	
	tdiv.style.display="block";	
	tdiv.style.position="absolute";	
	//tdiv.style.float="left";
	var btn_ele = this.id+"_"+name+'_icon';
	var btn = ((btn_ele=='')?this.$(this.id+'_eid'):this.$(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";//alert("top:"+tdiv.style.top+"left:"+tdiv.style.left)
	document.body.appendChild(tdiv);
	tdiv.focus();
}

webEditor.prototype.removePanel = function(panelid)
{
	if(this.$(panelid))
	{
		document.body.removeChild(this.$(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);
}

webEditor.prototype.insertHTML = function(html)
{
	if (e_isIE)
	{
		if (!this.range)
		{
			this.selectBegin();
		}
		if(this.range && this.seltype != "Control")
		{
			this.range.pasteHTML(html);
			this.range.select();
		}
		this.range = null;
	}
	else
	{
		this.iframe.document.execCommand("insertHTML", false, html);
		this.iframe.focus();
	}
}

webEditor.prototype.facePanel = function(id)
{
	var pContent = '';

	for(i=0;i<4;i++)
	{
		for(j=1;j<=8;j++)
		{
			var tt = (i*8+j).toString();
			var t='http://main.a8-img.com/static/2011/images/face/'+tt+'.gif';
			pContent += '<li><a href="javascript:;" onclick="javascript:'+this.id+'.insFace(\''+t+'\');"><img src="'+t+'" title="'+facetitle[i*8+j-1]+'" width="19" height="19" /></a></li>';
		}
	}
	$('#'+id).html(pContent);
}

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]+"' border=0>";
	}
	
	this.insertHTML(str);
	
	this.removePanel(this.id +'_face');
}

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 }; 
} 



