﻿
window.onload = getMsgBox;
window.onresize = resizeDiv;
window.onerror = function(){}

var D=new Function('obj','return document.getElementById(obj);')
function htmlbody(){
return (
   document.documentElement.clientHeight<=document.body.clientHeight
   &&document.documentElement.clientHeight!=0
)
?document.documentElement:document.body;
}
//浏览器滚动条位置
function scrollLeft(){return (!window.pageYOffset)?htmlbody().scrollLeft:window.pageXOffset;}
function scrollTop(){return (!window.innerHeight)?htmlbody().scrollTop:window.pageYOffset;}

//实际应距左距离
function getleft(strobjs,strLeftType,strleft){
var temp_getleft = 0;
if (strLeftType=="left"){
   temp_getleft = scrollLeft()*1 + strleft*1;
}else if (strLeftType=="mid"){
   (strleft*1<0)
   ?temp_getleft = scrollLeft()*1 + strleft*1
   + htmlbody().clientWidth*1/2 - strobjs.offsetWidth*1
   :temp_getleft = (scrollLeft()*1+strleft*1 + htmlbody().clientWidth*1/2);
}else if (strLeftType=="right"){
	temp_getleft
	= scrollLeft()*1 + htmlbody().clientWidth*1
	- strleft*1 - strobjs.offsetWidth*1;
}
return temp_getleft;
}

function moveTips(strobj,theTop,theLeft,theLeftType) {
var old,nowobj = D(strobj);
var nowleft = nowobj.style.left.replace("px","")*1;//返回在改变窗口大小或移动横滚动条前的距左部距离（数值）
var temp_left = getleft(nowobj,theLeftType,theLeft);//实际应距左距离
var re_theTop = theTop;
if (temp_left!=nowleft){//横向递增
   (Math.abs(temp_left-nowleft)>3&&Math.abs(temp_left-nowleft)<600)
   ?((temp_left>nowleft)?nowleft += Math.abs(temp_left-nowleft)/5
   :nowleft -= Math.abs(temp_left-nowleft)/5)
   :nowleft = temp_left;
   nowobj.style.left = nowleft + "px";
}
if (!openweb){old = re_theTop+40;var openweb;}/*这是默认高度*/;
var pos,tt=50;
pos = scrollTop()*1-nowobj.offsetTop*1+re_theTop*1;
pos = nowobj.offsetTop+pos/10;//纵向开始递增
if (pos < re_theTop) pos = re_theTop;
if (pos != old) {nowobj.style.top = pos+"px";tt=5;}
old = pos;
setTimeout("moveTips('"+strobj+"','"+theTop+"','"+theLeft+"','"+theLeftType+"')",tt);
}

function getMsgBox(){
	var ajax = InitAjax();


	var url = "chat_msg_box_handler.phtml?math="+Math.random();
	ajax.open("GET", url, false);
	ajax.send(null); 
	if(ajax.status == 200){
		var xmlDoc=ajax.responseXML&&ajax.responseXML.documentElement;
		if(!xmlDoc) return;
		var items = xmlDoc.getElementsByTagName('item');
		for(var i = 0; i<items.length; i++){
			var item = items[i];
			var uid = getNodeText(item.childNodes[0]);
			var sender = getNodeText(item.childNodes[1]);
			var num = getNodeText(item.childNodes[2]);
			var chatUrl="http://www.kawoka.com/chat/OnlineChat.phtml?uid="+uid;
			addMsgTr(chatUrl,sender,num);
		}
		getMsg();
	}
}
function InitAjax()
{
	var ajax=false;
	try        /*IE*/
	{
	ajax=new ActiveXObject("Msxml1.2XMLHTTP");
	}
	catch(e)
	{
	try        /*IE*/
	{
		ajax=new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (E)
	{
		ajax=false;
	}
	}
	if(!ajax && typeof XMLHttpRequest!='undefined')    /*非IE*/
	{
	ajax=new XMLHttpRequest();
	}
	return ajax;
}
//短信提示使用(asilas添加)
var divTop,divLeft,divWidth,divHeight,docHeight,docWidth,objTimer,i = 0;

function getMsg()
{
  var trs = document.getElementById('msgboxTb').getElementsByTagName('tr');
  if(trs.length==0){
	closeDiv();
	return;
  }
  try{
  divTop = parseInt(document.getElementById("eMeng").style.top,10)
  divLeft = parseInt(document.getElementById("eMeng").style.left,10)
  divHeight = parseInt(document.getElementById("eMeng").offsetHeight,10)
  divWidth = parseInt(document.getElementById("eMeng").offsetWidth,10)
  docWidth = document.documentElement.clientWidth;
  docHeight = document.documentElement.clientHeight;
  document.getElementById("eMeng").style.top = parseInt(document.body.scrollTop,10) + docHeight + 10;// divHeight
  document.getElementById("eMeng").style.left = parseInt(document.body.scrollLeft,10) + docWidth - divWidth
  //document.getElementById("eMeng").style.top = parseInt(document.body.scrollTop,10)  + 10;// divHeight
  //document.getElementById("eMeng").style.left = parseInt(document.body.scrollLeft,10) - divWidth
  document.getElementById("eMeng").style.visibility="visible"
  //objTimer = window.setTimeout("moveDiv()",10)
  }
  catch(e){}
}

function resizeDiv()
{
  var trs = document.getElementById('msgboxTb').getElementsByTagName('tr');
  if(trs.length==0){
	closeDiv();
	return;
  }	
  i+=1
  if(i>500) closeDiv()
  try{
  divHeight = parseInt(document.getElementById("eMeng").offsetHeight,10)
  divWidth = parseInt(document.getElementById("eMeng").offsetWidth,10)
  docWidth = document.documentElement.clientWidth;
  docHeight = document.documentElement.clientHeight;
  document.getElementById("eMeng").style.top = docHeight - divHeight + parseInt(document.body.scrollTop,10)
  document.getElementById("eMeng").style.left = docWidth - divWidth + parseInt(document.body.scrollLeft,10)
  }
  catch(e){}
}

function moveDiv()
{
  
  try
  {
  if(parseInt(document.getElementById("eMeng").style.top,10) <= (docHeight - divHeight + parseInt(document.body.scrollTop,10)))
  {
  window.clearInterval(objTimer)
  objTimer = window.setTimeout("resizeDiv()",1)
  }
  divTop = parseInt(document.getElementById("eMeng").style.top,10)
  document.getElementById("eMeng").style.top = divTop - 1
  }
  catch(e){}
}
function closeDiv()
{
  document.getElementById('eMeng').style.visibility='hidden';
  if(objTimer) window.clearInterval(objTimer)
}

function removeMsgTr(link,url){
	var tr = link.parentNode.parentNode;
	var tb = tr.parentNode;
	tb.removeChild(tr);
	var trs = document.getElementById('msgboxTb').getElementsByTagName('tr');
	if(trs.length==0){
		closeDiv();
	}
	link.target = "_blank";
	link.href=url;	
}

function addMsgTr(chatUrl,sender,num){
	var str="<a onclick=\"removeMsgTr(this,'"+chatUrl+"');\"  href=\"#\" style=\"color: #999999\">";
	str += "<span style=\"color: #999999\">"+sender+"</span>&nbsp;("+num+")</a>";	
	var tb = document.getElementById('msgboxTb');
	var   newRow   =   tb.insertRow(tb.rows.length);   
	var   newTd   =   newRow.insertCell(0);   
	newTd.innerHTML = str;
}

//得到节点的Text值
function getNodeText(obj)
{
	if(!obj)
	{
	return "";
	}
	if(obj.text)
	{
	return obj.text;
	}
	if(obj.textContent)
	{
	return obj.textContent;
	}
	if(obj.firstChild)
	{
	obj=obj.firstChild;    
	}
	if(obj.nodeValue)
	{
	return obj.nodeValue;
	}
	if(obj.data)
	{
	return obj.data;
	}
	return "";
}
function getNodeAttribute(node,name)
{
	if(!node || !name)
	{
	return false;
	}
	return getAttribute(name,node.attributes);
}
//得到某个属性
function getAttribute(name,list)
{
	if(!list)
	{
	return false;
	}
	for(var i=0;i<list.length;i++)
	{
	if(list[i].nodeName.toLowerCase()==name.toLowerCase())
	{
		return list[i];
	}
	}
	return false;    
}
			

	