var online_current_detialID=0;
var online_current_userID=0;
var online_interval_ID=0;
var online_timeout_ID=0;
var online_detial_timeout_ID=0;
var online_lastmsg_time=0;
var online_temp_i=1,ol_temp_interval=0,online_detail_hid_interval=0,online_temp_title=document.title; //for shining

//window.attachEvent('onload',function(){alert(document.referrer)});
function createxmlhttpforonline()
{	var xmlhttp=false;
	try	{
  		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} 
	catch (e) {
  		try {
   			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		} 
		catch (e) {
   			xmlhttp = false;
 		}
 	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  		xmlhttp = new XMLHttpRequest();
				if (xmlhttp.overrideMimeType) {//設置MiME類別
			xmlhttp.overrideMimeType('text/xml');
		}
	}	
	return xmlhttp;	
}

function online_detail_onscroll(e){
	var tooltip_max_width=600;
	oCanvas = document.getElementsByTagName(
	(document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY"
	)[0];
	var x=0,y=0;
	x = window.event ? event.clientX + oCanvas.scrollLeft : e.pageX;
	y = window.event ? event.clientY + oCanvas.scrollTop : e.pageY;
	w_width = window.innerWidth ? window.innerWidth + window.pageXOffset : oCanvas.clientWidth + oCanvas.scrollLeft;
	w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : oCanvas.clientHeight + oCanvas.scrollTop;
	var obj=online_elm("wbc_online_detial")
	obj.style.width = "300px";
	t_width = window.event ? obj.clientWidth : obj.offsetWidth;
	t_height = window.event ? obj.clientHeight : obj.offsetHeight;

	if (t_width > tooltip_max_width){
		obj.style.width = tooltip_max_width + "px";
		t_width = window.event ? obj.clientWidth :obj.offsetWidth;
	}
	t_extra_width = 7; // CSS padding + borderWidth;
	t_extra_height = 5; // CSS padding + borderWidth;

	obj.style.left = x + 8 + "px";
	obj.style.top = y + 8 + "px";

	while (x + t_width + t_extra_width > (w_width-10)){
		--x;
		obj.style.left = x + "px";
		t_width = window.event ? obj.clientWidth : obj.offsetWidth;
	}

	while (y + t_height + t_extra_height > w_height){
		--y;
		obj.style.top = y + "px";
		t_height = window.event ? obj.clientHeight : obj.offsetHeight;
	}
}

function online_getdata(url,obj1,obj2,fb)
{
		if(window.location.toString().split("wbc_online/").length>1){url=url.split("wbc_online/")[1]}
		url=url+'&rnd='+ Math.random()
		var xmlhttp=createxmlhttpforonline();
		if(!xmlhttp)
		{
			alert("你的瀏覽器不支持XMLHTTP！！");
			return;
		}
		if(obj1!=''){online_elm(obj1).innerHTML="數據載入中..."}
		xmlhttp.onreadystatechange=requestdata;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
		function requestdata(){
				if(xmlhttp.readyState==4)
				{

					if(xmlhttp.status==200)
					{
						if(obj2!='')online_elm(obj2).innerHTML=xmlhttp.responseText;
						if(fb!=null){eval(fb)}
					}else{
						if(obj2!='')online_elm(obj2).innerHTML="ErrorCode:"+xmlhttp.status+"<br />伺服器傳回:<br />"+xmlhttp.responseText;
					}
				}
			
		}
}

function wbc_online_setCookie(name,value)
{
    var Days = 30;
    var exp  = new Date();    //new Date("December 31, 9998");
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function wbc_online_getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)) return unescape(arr[2]);
        else return null;
}

function online_elm(id){return document.getElementById(id);}
function online_msg_show(online_rs){
		clearTimeout(online_timeout_ID);
		online_detail_hid();
		if(online_elm("wbc_online_msg_container").style.display=='block'&&online_elm("online_userID").innerHTML==online_current_userID){
			online_msg_hid()}else{
			online_elm("online_userID").innerHTML=online_current_userID;
			online_elm("wbc_online_msg_container").style.display='block';
			if(online_elm("online_msg_content").style.display=='block')online_elm("online_msg_content").focus();
		}
}
function  online_msg_hid(){
	online_elm("online_msg_content").value="";
	online_elm("wbc_online_msg_container").style.display='none';
}
function online_msg_send(){
	online_elm("online_send_bt").disabled="disabled";
	online_getdata("Plugins/wbc_online/wbc_online_re.asp?act=sendmsg&receiver="+escape(online_elm("online_userID").innerHTML)+"&msg="+escape(online_elm("online_msg_content").value),"online_msg_fb","online_msg_fb","online_after_send()");
}
function oneline_del_msg(id){
	online_getdata("Plugins/wbc_online/wbc_online_re.asp?act=delmsg&id="+id,"","online_msg_fb","wbc_online_load(0)");
}
function online_after_send(){
	online_elm("online_send_bt").disabled="";
	if(online_elm("online_msg_fb").innerHTML.split("完成").length==2){
		online_elm("online_msg_content").value='';
		setTimeout("wbc_online_load(0);online_elm('online_msg_fb').innerHTML='';",2000);
	}else{
		setTimeout("online_elm('online_msg_fb').innerHTML='';",2000);
	}
}
function online_detail_hid(){
		clearTimeout(online_detial_timeout_ID);
		online_current_detialID=0;
		online_elm("wbc_online_detial").style.display='none';
}
function wbc_online_getdetail(id,online_gid,e){
	online_detail_hid();
	if(online_current_detialID!=id){
	online_current_detialID=id;
	online_detail_onscroll(e);
	clearTimeout(online_timeout_ID);
	online_timeout_ID=setTimeout("wbc_online_getdetial_do("+id+","+online_gid+")",200)
	}
}
function wbc_online_msg_shine(){
	online_temp_i=1;
	ol_temp_interval=setInterval('var e=online_elm("wbc_ol_msg_it_"+online_elm("wbc_online_msg_lastID").innerHTML);if(online_temp_i%2==1){e.className="wbc_online_msg_new";document.title="Online插件收到新消息"}else{e.className="commenttop";document.title=online_temp_title;}online_temp_i++;if(online_temp_i>6){clearInterval(ol_temp_interval)}',500)
	if(online_elm("online_msg_content").style.display=='block')online_elm("online_msg_content").focus();
}
function wbc_online_getdetial_do(id,online_gid){
	clearTimeout(online_detail_hid_interval);
	online_elm("wbc_online_detial").style.display='block';
	if(online_elm("wbc_online_detail_"+id)!=null){
		online_elm("online_detail_container").innerHTML=online_elm("wbc_online_detail_"+id).innerHTML;//Use the old data
		clearTimeout(online_detial_timeout_ID);online_detial_timeout_ID=setTimeout('online_detail_hid()',online_detial_timelast*1000);wbc_addlistener()
	}else{
		online_getdata("Plugins/wbc_online/wbc_online_re.asp?act=getdetial&id="+id+"&online_gid="+online_gid,"online_detail_container","online_detail_container","clearTimeout(online_detial_timeout_ID);online_detial_timeout_ID=setTimeout('online_detail_hid()',online_detial_timelast*1000);wbc_addlistener();wbc_online_savedetail("+id+");");
	}
}
function wbc_online_savedetail(id){
	elm("wbc_online_detail_header").innerHTML+='<span class="wbc_temp" id="wbc_online_detail_'+id+'">'+online_elm("online_detail_container").innerHTML+'</span>';
}
function wbc_online_load(online_gid){
	clearInterval(online_interval_ID);
	online_getdata("Plugins/wbc_online/wbc_online_re.asp?act=getlist&online_gid="+online_gid,"","wbc_online","wbc_online_checkmsg();elm('wbc_online_detail_header').innerHTML=''");
	if(wbc_online_interval>0)online_interval_ID=setInterval("wbc_online_load(0)",1000*wbc_online_interval);
}

function wbc_online_checkmsg(){
	if(wbc_online_getCookie("wbc_online_msg_time")!=null){online_lastmsg_time=parseInt(wbc_online_getCookie("wbc_online_msg_time"));}
	if(parseInt(online_elm("wbc_online_msg_time").innerHTML)>online_lastmsg_time){
		window.focus();
		wbc_online_msg_shine();
		online_lastmsg_time=parseInt(online_elm("wbc_online_msg_time").innerHTML);
		wbc_online_setCookie("wbc_online_msg_time",online_lastmsg_time);
	}
}
var isitin=false;
function wbc_addlistener(){
	var o=online_elm("online_detail_container");
	if(o.attachEvent){
		o.attachEvent("onmouseover", function(){clearTimeout(online_detail_hid_interval)});	
		o.attachEvent("onmouseout", online_detail_hid_t);
	}
}

function online_detail_hid_t(e) {
        if (!e) var e = window.event;
        var tg = (window.event) ? e.srcElement : e.target;
        if (tg.nodeName != 'DIV') return;
        var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
        while (reltg != tg && reltg.nodeName != 'BODY')
                reltg= reltg.parentNode
        if (reltg== tg) return;
		online_detail_hid()
}
function online_detail_hid_ts(){
	online_detail_hid_interval=setTimeout("online_detail_hid()",400);
}
setTimeout("wbc_online_load(0);",1)