﻿/*20091205 Design By Marcho,ryeen.com*/	
	var faceUrl="images/gif/" //头像路径
	var faceLeft=25 //定义距离输入框的相对左边距
	var faceNums=39  //定义头像数量
	
	
	if($.browser.msie) {
	}
	else if($.browser.safari)
	{
	}
	else if($.browser.mozilla)
	{
	}
	else if($.browser.opera)
	{
	} 



function uf_attachFunction(){
	_win=$('#HtmlEditor')[0].contentWindow; // 我们用 _win 变量代替 iframe window 
	_doc=_win.document;                        // 用 _doc 变量代替 iframe的document 
	_doc.designMode = 'On';	
	_doc.onkeydown = function()
	{
		var SendEditor = document.frames.HtmlEditor
		var e = SendEditor.event;
		KeyboardSendType();
		$("#faceContent").hide();
		$("#FontContent").hide();
		if (gSendMode == "Enter" && e.keyCode==13) 
		{

			SendMsg();
			return false;
		}
		if(gSendMode == "CltrEnter" && e.ctrlKey && e.keyCode==13)
		{
			SendMsg();
			return false;
		}
	}
	if(userInfo.fixUser != 0)
	{
		EnterCheck(String(userInfo.PolicyType),'' , String(userInfo.SubGroupID) , userInfo.m_LasrUserName);
	}
}





//窗口改变时自动调整位置
$(window).resize(function(){
GInitHtmledter(textId,faceLeft);
});

var gSendMode = "Enter";



var g_IsType = 0;

function KeyboardSendType()
{
	if(userInfo.CsID == 0)
	{
		return ;
	}
	
	if(!userInfo.loaded)
	{
	    AddHtmlDiv(LanageInfo.sSendRequestNoLoad , "AlertInfo");
		return;
	}
	
	
	
	////
	if(g_IsType == 0)
    {
	  //
       setTimeout("SnedType()" , 1000);
    } 
    g_IsType = 1;
}

function SnedType()
{
	//
	var Msg = _doc.body.innerHTML;
	var Message = html2ubb(Msg);
	Message = ubb2html(Message);
	if(Message.length > 500)
	{
		Message = Message.substring(0,500);
	}   
	if(Message != "")
	{
	    SendP2PCmd('RequestTypeKey' ,  Message);
    } 	
	g_IsType = 0;

}





//将表情标题及内容写入BODY
function InitHtmledter() {	
	msg="<style>#faceContent{width:364px; position:absolute;border:1px solid #aaa;border-bottom-style:none;display:none;z-index:9999; text-align:center;padding:3px;padding-bottom:6px;background:#fff;} #faceContent a img{float:left;cursor:pointer;margin:1px 1px; border:#cacaca 1px solid}  #faceContent a:hover img{border:1px solid #f51d69}   #faceTitle{height:22px; width:36px; position:absolute; background:url("+faceUrl+"first.gif) no-repeat center center #fff;border:1px solid #aaa;border-bottom:none;'}</style>"
	msg=msg+"<div id='faceTitle' style='cursor:pointer'></div>"
	msg=msg+""
	msg=msg+"<div id='faceContent'></div>"
	msg=msg+"<div id='FontTitle' style='cursor:pointer'></div>"
	
	msg=msg+ '<DIV id=SelectSnedMode style="BORDER-RIGHT: #838383 1px solid; PADDING-RIGHT: 1px; BORDER-TOP: #838383 1px solid; DISPLAY:none; PADDING-LEFT: 1px; BACKGROUND: #ffffff; LEFT: 26px; PADDING-BOTTOM: 1px; BORDER-LEFT: #838383 1px solid; WIDTH: 133px; PADDING-TOP: 1px; BORDER-BOTTOM: #838383 1px solid; POSITION: absolute; TOP: 35px; "><div style="font-size:12px; :" class="selectSendMode" id="sendEnter" onmousemove="this.style.backgroundColor=\'#e5e5e5\'" onmouseout="this.style.backgroundColor=\'#ffffff\'" onclick="SetSendMode(\'Enter\')">Enter 发送</div><div style="font-size:12px" class="NoselectSendMode" id="sendCltrEnter" onmousemove="this.style.backgroundColor=\'#e5e5e5\'" onmouseout="this.style.backgroundColor=\'#ffffff\'" onclick="SetSendMode(\'CltrEnter\')">Cltr+Enter 发送</div></DIV>';
	
	
	msg += "	<div id=\"FontContent\" style=\"z-index: 1; left: 16px; DISPLAY:none; width: 185px;";
	msg += "    position: absolute; top: 245px; height: 23px; border:1px solid #aaa;border-bottom-style:none;\">";
	msg += "    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
	msg += "        <tr>";
	msg += "            <td width=\"20%\" height= 20px>";
	msg += "                <select id=\"select1\"  name=\"select1\" onchange=\"changeFont(this);\">";
	msg += "                    <option selected=\"selected\" value=\"宋体\">字体</option>";
	msg += "                    <option value=\"宋体\">宋体</option>";
	msg += "                    <option value=\"黑体\">黑体</option>";
	msg += "                    <option value=\"幼圆\">幼圆</option>";
	msg += "                    <option value=\"楷体\">楷体</option>";
	msg += "                    <option value=\"隶书\">隶书</option>";
	msg += "                    <option value=\"仿宋\">仿宋</option>";
	msg += "                    <option value=\"华文彩云\">彩云</option>";
	msg += "                    <option value=\"华文新魏\">新魏</option>";
	msg += "                    <option value=\"华文行楷\">行楷</option>";
	msg += "                </select>";
	msg += "            </td>";
	msg += "            <td width=\"20%\"  height= 20px>";
	msg += "                <select id=\"select2\"  name=\"select2\" onchange=\"changeSize(this);\">";
	msg += "                    <option selected=\"selected\" value=\"3\">字号</option>";
	msg += "                    <option value=\"10\">10</option>";
	msg += "                    <option value=\"12\">12</option>";
	msg += "                    <option value=\"14\">14</option>";
	msg += "                    <option value=\"16\">16</option>";
	msg += "                    <option value=\"18\">18</option>";
	msg += "                    <option value=\"20\">20</option>";
	msg += "                    <option value=\"24\">24</option>";
	msg += "                    <option value=\"36\">36</option>";
	msg += "                </select>";
	msg += "            </td>";
	msg += "            <td width=\"20%\"  height= 20px>";
	msg += "                <select id=\"select3\"  name=\"select3\" onchange=\"changeColors(this);\">";
	msg += "                    <option selected=\"selected\">颜色</option>";
	msg += "                    <option style=\"background-color: #000000\" value=\"#000000\">黑色</option>";
	msg += "                    <option style=\"background-color: #888888\" value=\"#888888\">灰色</option>";
	msg += "                    <option style=\"background-color: #ff0000\" value=\"#ff0000\">红色</option>";
	msg += "                    <option style=\"background-color: #cc3366\" value=\"#cc3366\">暗红</option>";
	msg += "                    <option style=\"background-color: #ff00ff\" value=\"#ff00ff\">紫红</option>";
	msg += "                    <option style=\"background-color: #ee9966\" value=\"#ee9966\">橘黄</option>";
	msg += "                    <option style=\"background-color: #aa00cc\" value=\"#aa00cc\">紫色</option>";
	msg += "                    <option style=\"background-color: #8800ff\" value=\"#8800ff\">蓝紫</option>";
	msg += "                    <option style=\"background-color: #ffff00\" value=\"#ffff00\">黄色</option>";
	msg += "                    <option style=\"background-color: #ccaa00\" value=\"#ccaa00\">土黄</option>";
	msg += "                    <option style=\"background-color: #ff8800\" value=\"#ff8800\">金黄</option>";
	msg += "                    <option style=\"background-color: #0000ff\" value=\"#0000ff\">蓝色</option>";
	msg += "                    <option style=\"background-color: #6699ee\" value=\"#6699ee\">天蓝</option>";
	msg += "                    <option style=\"background-color: #0088ff\" value=\"#0088ff\">海蓝</option>";
	msg += "                    <option style=\"background-color: #000088\" value=\"#000088\">深蓝</option>";
	msg += "                    <option style=\"background-color: #00ff00\" value=\"#00ff00\">绿色</option>";
	msg += "                    <option style=\"background-color: #888800\" value=\"#888800\">黄绿</option>";
	msg += "                    <option style=\"background-color: #008888\" value=\"#008888\">蓝绿</option>";
	msg += "                </select></td>";
	msg += "        </tr>";
	msg += "    </table>";
	msg += "</div>";
	
	
	$("body").append(msg);
	SendInit();
}

function editerInit()
{
	_doc.body.onclick =GetPos;
	_doc.body.onselect =GetPos;
	_doc.body.onkeyup =GetPos;
}



//将表情循环插入页面中
function IF() {	
	$("#faceContent").html("");
	for(var i=1;i<=faceNums;i++){
		str=faceUrl+"F_"+i+".gif";
		$("#faceContent").append("<a href='javascript:InitImg(\""+ str +"\")'><img src="+str+" fn=[@F_"+i+"@] /></a>");
	}
}

//控制表情区位置
function GInitHtmledter(obj,L) {	
	var O=$("#"+obj).offset();
	var top=O.top;
	var left=O.left;
	$("#faceTitle").css("top",top-25+"px");
	$("#faceTitle").css("left",left+L+"px");
	
	$("#FontTitle").css("top",top-25+"px");
	$("#FontTitle").css("left",left+70+"px");
	
	$("#faceContent").css("top",top-120+"px");
	$("#faceContent").css("left",left+L+"px");
	
	$("#FontContent").css("top",top-50+"px");
	$("#FontContent").css("left",left+50+"px");
}

//show客服正在输入中

//控制表情区位置
function ShowTypeMsg() {	
	var O=$("#faceTitle").offset();
	var top=O.top;
	var left=O.left;
	$("#OHH2ATypeMsg").css("top",top-18+"px");
	$("#OHH2ATypeMsg").css("left","24px");
	var showHtml = LanageInfo.sOHH2ATypeMsg;
	$("#OHH2ATypeMsg")[0].style.zIndex = document.getElementsByTagName("div").length +1 ;
	setTimeout("ClertTypeMsg()",4000);
	$("#OHH2ATypeMsg")[0].innerHTML = showHtml;
	$("#OHH2ATypeMsg")[0].style.display = 'block';
	//alert($("#OHH2ATypeMsg")[0].innerHTML);
}

function ShowTypeMsgHtml(Html) {	
	var O=$("#faceTitle").offset();
	var top=O.top;
	var left=O.left;
	$("#OHH2ATypeMsg").css("top",top-18+"px");
	$("#OHH2ATypeMsg").css("left","24px");
	$("#OHH2ATypeMsg")[0].innerHTML = Html;
	//setTimeout("ClertTypeMsg()",10000);

}

function ClertTypeMsg()
{
	$("#OHH2ATypeMsg")[0].innerHTML = "&nbsp;";
}


//替换页面中的表情代码为图片
function RInitHtmledter(){
		rContent=$("#"+replaceId).html();
		rContent = rContent.replace(/\[@/g, "<img src="+faceUrl+"");
		rContent = rContent.replace(/\@]/g, ".gif />"); 
		$("#"+replaceId).html(rContent);
}


//替换页面中的表情代码为图片
function strR(str){
		rContent=str;
		rContent = rContent.replace(/\[@/g, "<img src="+faceUrl+"");
		rContent = rContent.replace(/\@]/g, ".gif />"); 
		return rContent;
		//$("#"+replaceId).html(rContent);
}








function GetPos()
{
	_win.pos = _doc.selection.createRange();
//alert("dafd");
}


function insertHTML( sHtml )
{
	//
	try{
		   _win.focus(); 
		   //_doc.getElementsByTagName("BODY")[0].focus();
		   if(   $.browser.msie ){
					if (_doc.selection.type.toLowerCase() != "none")
					{
						_doc.selection.clear() ;
					}
					_win.pos.pasteHTML( sHtml ) ;
					//alert(_win.pos.select());
		   }else{ 
				  _doc.execCommand( 'InsertHtml' , '' , sHtml );
		   }
		   _win.focus(); 
		   _win.pos.select();
	}
	catch(e)
	{
		_doc.selection.createRange().pasteHTML( sHtml ) ;
		//alert(e);
	}
}

function InitImg(imgurl)
{
	htmlSelect();
	var imgurlSrc = "<img src='"+imgurl+"'>";
	insertHTML(imgurlSrc);
}

function htmlSelect()
{
	try
	{
		_win.focus();
		_win.pos.select();
	}
	catch(e)
	{
	}
}



//表情点击显示效果
function CInitHtmledter(){
	$("#faceTitle").click(
	function(event){
	try{
		_win.pos.select();
	}
	catch(e){};
	$("#faceContent").toggle();
	$("#FontContent").hide();
	//
	//$("#faceContent img").click(function(){})
	event.stopPropagation();
	});
	
	
	
	$("#FontTitle").click(
	function(event){
			_win.focus();
		try
		{
			_win.pos.select()
		}
		catch(e){}
		$("#FontContent").toggle();
		$("#faceContent").hide();
		

			event.stopPropagation();
	});
	
	
	
	$("body").click(
	function(){
		$("#faceContent").hide();
		//$("#FontContent").hide();
	});

}


function SendInit(){
	$("#sendModeButton").click(
	function(event){
		$("#SelectSnedMode").toggle();
		var O=$("#sendModeButton").offset();
		var top=O.top;
		var left=O.left;
		$("#SelectSnedMode").css("top",top-25+"px");
		$("#SelectSnedMode").css("left",left+"px");
		event.stopPropagation();
	});
	$("body").click(
	function(){
		$("#SelectSnedMode").hide();
	});

}


//设置发送模式
function SetSendMode(Mode)
{
	////
	if(Mode == "Enter")
	{
		$("#sendEnter")[0].className = "selectSendMode";
		$("#sendCltrEnter")[0].className = "NoselectSendMode";
		AddHtmlDiv(LanageInfo.sSendModeSelectEnter , "AlertInfo");
	}
	else
	{
		$("#sendEnter")[0].className = "NoselectSendMode";
		$("#sendCltrEnter")[0].className = "selectSendMode";
		AddHtmlDiv(LanageInfo.sSendModeSelectCtrlEnter , "AlertInfo");
	}
	gSendMode = Mode;
}


function changeSize(obj){
	FormatText('fontsize',obj.value);
}
function changeFont(obj){
	FormatText('fontname' ,obj.value);
}
function changeColors(obj){
	FormatText('foreColor',obj.value);
}

function FormatText(command, option){
	try{
		//debugger;
		   _win.focus(); 
			_doc.execCommand(command, true, option);
			_doc.selection.createRange().execCommand(command, true, option);
		   _win.focus(); 
		   //_win.pos.select();
	}
	catch(e)
	{
		_doc.selection.createRange().execCommand(command, true, option);
		//alert(e);
	}
	
	$("#FontContent").hide();
}