﻿//全局表情映射表 - 前台没用了，后台才用这个转码
//var g_face_index_map = Array(
//	14,  1,  2,  3,  4,  5,  6,  7,  8,  9,
//	10, 11, 12, 13, 0, 80, 81, 82, 83, 84, 
//	45, 85, 44, 43, 40, 41, 86, 42, 87, 57, 
//	88, 52, 89, 63, 64, 65, 66, 67, 53, 69, 
//	90, 91, 74, 92, 93, 75, 76, 77, 94, 95, 
//	15, 16, 17, 18, 19, 26, 27, 28, 29, 46, 
//	47, 48, 49, 60, 61, 62, 68, 70, 71, 72, 
//	73, 78, 59, 20, 21, 22, 23, 24, 25, 30, 
//	31, 32, 33, 34, 35, 36, 37, 38, 39, 50, 
//	51, 54, 55, 56, 58, 79);

//系统表情快捷键列表
//var g_face_hotkey_list = Array("wx","pz","se","fd","dy","ll","hx","bz","shui","dk","gg","fn","tp","cy","jy","ng","kuk","feid","zk","tu","tx","ka","baiy","am","jie","kun","jk","lh","hanx","db","fendou","zhm","yiw","xu","yun","zhem","shuai","kl","qiao","zj","shan","fad","aiq","tiao","zhao","mm","zt","maom","xg","yb","qianc","dp","bei","dg","shd","zhd","dao","zq","yy","bb","kf","fan","yw","mg","dx","wen","xin","xs","hy","lw","dh","sj","yj","ds","ty","yl","qiang","ruo","ws","shl","dd","mn","hl","mamao","qz","fw","oh","bj","qsh","xig","xy","duoy","xr","xixing","nv","nan");
var g_face_hotkey_list = Array("[A001]","[A002]","[A003]","[A004]","[A005]","[A006]","[A007]","[A008]","[A009]","[A010]","[A011]","[A012]","[A013]","[A014]","[A015]","[A016]","[A017]","[A018]","[A019]","[A020]","[A021]","[A022]","[A023]","[A024]","[A025]","[A026]","[A027]","[A028]","[A029]","[A030]","[A031]","[A032]","[A033]","[A034]","[A035]","[A036]","[A037]","[A038]","[A039]","[A040]","[A041]","[A042]","[A043]","[A044]","[A045]","[A046]","[A047]","[A048]","[A049]","[A050]","[A051]","[A052]","[A053]","[A054]","[A055]","[A056]","[A057]","[A058]","[A059]","[A060]","[A061]","[A062]","[A063]","[A064]","[A065]","[A066]","[A067]","[A068]","[A069]","[A070]","[A071]","[A072]","[A073]","[A074]","[A075]","[A076]","[A077]","[A078]","[A079]","[A080]","[A081]","[A082]","[A083]","[A084]","[A085]","[A086]","[A087]","[A088]","[A089]","[A090]","[A091]","[A092]","[A093]","[A094]","[A095]","[A096]");
var g_face_title = Array("微笑", "撇嘴", "色", "发呆", "得意", "流泪", "害羞", "闭嘴", "睡", "大哭", "尴尬", "发怒", "调皮", "呲牙", "惊讶", "难过", "酷", "非典", "抓狂", "吐", "偷笑", "可爱", "白眼", "傲慢", "饥饿", "困", "惊恐", "流汗", "憨笑", "大兵", "奋斗", "咒骂", "疑问", "嘘...", "晕", "折磨", "衰", "骷髅", "敲打", "再见", "闪人", "发抖", "爱情", "跳", "找", "美眉", "猪头", "猫咪", "小狗", "拥抱",  "钱", "灯泡", "酒杯", "蛋糕", "闪电", "炸弹", "刀", "足球", "音乐", "便便",  "咖啡", "饭", "药丸", "玫瑰", "凋谢", "吻", "爱心", "心碎", "会议", "礼物",  "电话", "时间", "邮件", "电视", "太阳", "月亮", "强", "弱", "握手", "胜利",  "多多", "美女", "汉良", "毛毛", "Q仔", "飞吻", "怄火", "白酒", "汽水", "西瓜", "下雨", "多云", "雪人", "星星", "女", "男");

//最小/大的face字符串的长度
var fa_hkmaxlen = 0;
var fa_hkminlen = 0;
var g_face_hotkey_map = new Array();

//系统表情窗口
var g_sysFaceWin = null;

//构造系统表情快捷键map
function fg_init_face_map()
{
	var i;
	fa_hkmaxlen = g_face_hotkey_list[0].length;
	fa_hkminlen = fa_hkmaxlen;
	
	for(i = 0; i < 	g_face_hotkey_list.length; i++)
	{
		if(g_face_hotkey_list[i].length > fa_hkmaxlen)
		{
			fa_hkmaxlen = g_face_hotkey_list[i].length;
		}
		if(g_face_hotkey_list[i].length < fa_hkminlen)
		{
			fa_hkminlen = g_face_hotkey_list[i].length;
		}
		
		g_face_hotkey_map[g_face_hotkey_list[i]] = i;
	}
}

//表情被点击时的处理函数，请设置这个回调函数
//var g_on_system_face_click;
function fa_add_system_face()
{
	var el = event.srcElement;
	if(el && el.hkindex >= 0 && g_sysFaceWin)
	{
		if(typeof(g_on_system_face_click) == "function")
		{
			//g_on_system_face_click(g_sysFaceWin.curcid, "/" + g_face_hotkey_list[el.hkindex], el.hkindex);
			g_on_system_face_click(g_sysFaceWin.curcid, g_face_hotkey_list[el.hkindex], el.hkindex);
		}
		else if(typeof(document.g_on_system_face_click) == "function")
		{
			//g_on_system_face_click(g_sysFaceWin.curcid, "/" + g_face_hotkey_list[el.hkindex], el.hkindex);
			document.g_on_system_face_click(g_sysFaceWin.curcid, g_face_hotkey_list[el.hkindex], el.hkindex);
		}
	}
	
	return true;
}

//获得系统表情的html表
function fa_get_system_face_window(cid)
{
	if(g_sysFaceWin)
	{
		if(cid)
		{
			g_sysFaceWin.curcid = cid;
		}
		else
		{
			g_sysFaceWin.curcid = 0;
		}
	}
	else
	{
		g_sysFaceWin = g_vs.createWindow("faceWin", null, null, -1, 0, 0, "393px" , "185px", false, "", null, false, false);
		if(g_sysFaceWin)
		{
			g_sysFaceWin.client.style.padding = "1 1 1 1";
			g_sysFaceWin.client.style.overflow = "hidden";
			g_vs.imgFactory.changeImg(g_sysFaceWin.client, "/img/system-face/all.gif", true);
			g_sysFaceWin.client.style.backgroundRepeat = "no-repeat";
			g_sysFaceWin.client.onselectstart = fa_onfacecellselectstart;
			var tb = document.createElement('<table class=emNoSel width="391px" height="183px" border="0" cellpadding="0" cellspacing="1"></table>');
			if(tb)
			{
				//alert(tb);
				var i,j;
				var row,cell;
				var idx = 0;
				for(i = 0; i < 7; i++)	
				{
					row = tb.insertRow();
					for(j = 0; j < 15 && row; j++)
					{
						cell = row.insertCell();
						if(cell)
						{
							cell.width = "25px";
							cell.style.lineHeight = "23px";
							//cell.innerHTML = "&nbsp;";
							cell.align = "center";
							cell.style.border="1px solid #FFFFFF";
							cell.onclick =  fa_add_system_face;
							if(g_isFF)
							{
								cell.className = "emNoSel";
							}
							if(idx < g_face_hotkey_list.length)
							{
								cell.innerHTML = "&nbsp;";
								cell.hkindex = idx;
								cell.style.cursor = VS_CUR_HAND;
								//cell.title = g_face_title[idx] + " /" + g_face_hotkey_list[idx];
								cell.title = g_face_title[idx] +" "+ g_face_hotkey_list[idx];
								cell.onmouseover = fa_onfacemouseover;
								cell.onmouseout = fa_onfacemouseout;
							}
							else
							{
								//cell.style.cursor = "arrow";
							}
							idx++;
						}
					}
				}
			}
			if(cid)
			{
				g_sysFaceWin.curcid = cid;
			}
			else
			{
				g_sysFaceWin.curcid = 0;
			}
			g_sysFaceWin.div.style.overflow='hidden';
			g_sysFaceWin.div.style.padding='0 0 0 0';
			g_sysFaceWin.client.appendChild(tb);
			g_sysFaceWin.client.insertAdjacentHTML("beforeEnd", "<span style='display:none;'>.</span>");
		}
	}
	
	
	return g_sysFaceWin;
}

function fa_onfacemouseover()
{
	var cell = event.srcElement;
	if(cell)
	{
		cell.style.border="1px solid #0000FF";
	}
}

function fa_onfacemouseout()
{
	var cell = event.srcElement;
	if(cell)
	{
		cell.style.border="1px solid #FFFFFF";		
	}	
}

function fa_onfacecellselectstart()
{
	return false;
}

function fa_changeImgStyle1()
{
	this.style.border="1px solid #626262";
}

function fa_changeImgStyle2()
{
	this.style.border="1px solid #DFE6F6";
}

//准备图片，把消息体中的\24\25等特殊字符开头的内容进行格式化，转化为html的元素
//function fc_format_message(str)
function fc_format_message(str,isHistory, funonmsgimgload, isSimpleText)
{
	if(fa_hkmaxlen == 0)
	{
		alert("您还没有调用fg_init_face_map呢");
		return str;
	}
	
	var len = str.length;
	var i, j;
	var c = 0, c1=0, c5=0;
	var index = -1;	
	var result = '';
	//*********** stevenmi(2007-7-27) **********
	var guid;		
	var startpos=0;
	var endpos;
	//*************  end *********************
	
	//循环检查每个字符 		
	for(i = 0; i < len; i++)
	{
		// such as : [A008]
		if(len-i >= 5)
		{				
			c = str.charAt(i);
			c1 = str.charAt(i+1);
			c5 = str.charAt(i+5);			
		}
		else
		{
			c = str.charAt(i);
			c1=0;
			c5=0;
		}
		
		//表情
		//if('/' == c)
		if(('[' == c)&&('A'==c1)&&(']'==c5))
		{
			//alert("fa_hkminlen = " + fa_hkminlen + " fa_hkmaxlen = " + fa_hkmaxlen);
			//alert(len - i - 1);			
			for(j = fa_hkminlen; j <= fa_hkmaxlen && j <= len - i; j++)
			{
				//alert(str.substr(i+1, j).toLowerCase());
				//index = g_face_hotkey_map[str.substr(i+1, j).toLowerCase()];				
				index = g_face_hotkey_map[str.substr(i, j)];				
				if("undefined" != ""+index)
				{
					break;
				}
				else
				{
					index = -1;
				}						
			}
			
			//找到，正确表情，构造消息
			if(-1 != index)
			{
				if(isSimpleText == true)
				{
					result += '[表情]';
				}
				else
				{
					//img = g_vs.imgFactory.newImage('img/system-face/'+index+ '.gif', 20, 20);
					//result += img.outerHTML;				
					result += '<img align="absbottom" style="width:20;height:20;" src="/img/system-face/'+index+ '.gif"></img>';
					//alert(img.outerHTML);
				}
				i += j-1;
				index = -1;
			}
			//非正确表情
			else
			{
				result += c;
			}
		}
		// if a picture
		else if(('/' == c) && (str.charAt(i+1) == '{'))
		{
			endpos = str.indexOf(".",i+1);
			if(endpos != -1)
			{				
				//***** /{ADAC603D-584C-4B27-963A-B3B35CFB1C5F}0.jpg   vs  ADAC603D584C4B27963AB3B35CFB1C5F0.jpg
				// QQ->KF     as: " ..}0.jpg"					
				if(endpos-i == 40)
				{
					// get {ADAC603D-584C-4B27-963A-B3B35CFB1C5F}0.jpg  use to be the id
					var fname =str.substring(i+1,endpos+4);
					guid = fname.replace('{',"");
					guid = guid.replace('}',"");
					guid = guid.replace(/\-/g,"");
					
					if(isSimpleText == true)
					{
						result += '[图片]';
					}
					else
					{
						//result += '<img id='+fname +' src="' + KF_FILETRANS_DOWN_URL+'/Down/'+guid+ '"></img>'; 					
						// is not a history message
						if((isHistory ==null)||(isHistory==true))  
						{
						    if(funonmsgimgload != null && typeof(funonmsgimgload) == "string")
						    {
						        result += '<img id='+fname +' src="/img/sendingimg.gif" onload="'+funonmsgimgload+'();"></img>';
						    }
						    else
						    {
							    result += '<img id='+fname +' src="/img/sendingimg.gif"></img>';
							}
						}
						else						  
						{
							//history message
							if(funonmsgimgload != null && typeof(funonmsgimgload) == "string")
						    {
						        result += '<img src="' + KF_FILETRANS_DOWN_URL+'/Down/'+guid+ '" onload="'+funonmsgimgload+'();"></img>';
						    }
						    else
						    {
							    result += '<img src="' + KF_FILETRANS_DOWN_URL+'/Down/'+guid+ '"></img>';
							}
						}
					}
					i = endpos+3;
					//alert("enter format change imag, str=" + str+"qq result=" + result);	
				}
				//******/{73358C2F-A1E3-3C04-8A5E-766C4EDED047}.jpg  vs   L73358C2FA1E33C048A5E766C4EDED047.jpg
				// KF ->QQ   as "...}.jpg"
				else if(endpos-i == 39)
				{
					guid = str.substring(i+2,endpos-1);
					guid = 'L'+guid.replace(/\-/g,"");
					if(funonmsgimgload != null && typeof(funonmsgimgload) == "string")
				    {
				        result += '<img src="' + KF_FILETRANS_DOWN_URL+'/upload/'+guid+'.tmp" onload="'+funonmsgimgload+'();"></img>'; 
				    }
				    else
				    {
					    result += '<img src="' + KF_FILETRANS_DOWN_URL+'/upload/'+guid+'.tmp"></img>'; 
					}
					i = endpos+3;
					//alert("enter format change imag, str=" + str+"kf result=" + result);	
				}
				// not a picture
				else
				{
					result += c;
				}
			}
		}
		//普通字符
		else
		{
			result += c;
		}
	}

	return result;
}

//获得用户头像的image对象，对于未知的用户头像ID,直接传入0即可
function fc_getUserHead(imgId, bigface, offline)
{
	var maxImageId = 84;
	var result = '';
	//var index = imgId / 3;
	//index = (index > maxImageId)?maxImageId:index;
	var index = imgId;
	if(index%3 != 0)
	{
		index /= 3;
		index += 86;
	}
	else
	{
		index /= 3;
		if(index >= 100)
		{
			index = 0;
		}
	}	
	
	if(bigface)
	{
		
		result = "http://img.qq.com/face/l/"+index+"-0.gif";
	}
	else
	{
		if(offline)
		{
			result = "http://img.qq.com/face/s/"+index+"-3.gif";
		}
		else
		{
			result = "http://img.qq.com/face/s/"+index+"-2.gif";
		}
	}
	return result;
}
