var XMLHttp = {
    _objPool: [],
    _getInstance: function (){
        for (var i = 0; i < this._objPool.length; i ++){
            if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4){
                return this._objPool[i];
            }
        }
        // IE5中不支持push方法
        this._objPool[this._objPool.length] = this._createObj();
        return this._objPool[this._objPool.length - 1];
    },
    _createObj: function (){
        if (window.XMLHttpRequest){
            var objXMLHttp = new XMLHttpRequest();
            if (objXMLHttp.overrideMimeType) {//设置MiME类别
                objXMLHttp.overrideMimeType("text/xml");
            }
        }else{
            var MSXML = ['Msxml2.XMLHTTP.7.0','MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
            for(var n = 0; n < MSXML.length; n++){
                try{
                    var objXMLHttp = new ActiveXObject(MSXML[n]);
                    break;
                }catch(e){
                }
            }
        }
        // mozilla某些版本没有readyState属性
        if (objXMLHttp.readyState == null){
            objXMLHttp.readyState = 0;
            objXMLHttp.addEventListener("load", function (){
                objXMLHttp.readyState = 4;
                if (typeof objXMLHttp.onreadystatechange == "function"){
                    objXMLHttp.onreadystatechange();
                }
            },  false);
        }

        return objXMLHttp;
    },
    // 发送请求(方法[post,get], 地址, 数据, 回调函数)
    //synchronous 同步，默认为false，即默认为异步
    sendReq: function (method, url, data, callback,NoCache,synchronous){
        var objXMLHttp = this._getInstance();
        if(!synchronous){asynchronous=true;}else{asynchronous=false;}
        with(objXMLHttp){
            try
            {
                if(NoCache==true){
                    // 加随机数防止缓存
                    if (url.indexOf("?") > 0)
                    {
                        url += "&randnum=" + Math.random();
                    }
                    else
                    {
                        url += "?randnum=" + Math.random();
                    }
                }
                open(method, url, asynchronous);
                // 设定请求编码方式
                //setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                onreadystatechange = function ()
                {
                    if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
                    {
                        callback(objXMLHttp);
                    }
                }
            }
            catch(e)
            {
                //alert(e);
            }
        }
    }
};
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}
function removeEvent(obj, evType, fn, useCapture){
	if (obj.removeEventListener){
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.detachEvent){
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be removed");
	}
}
function myaddEvt(_elem, _evtName, _fn, _useCapture){
   if(!_elem) return '';
   if (typeof _elem.addEventListener != 'undefined')
   {
	  if (_evtName === 'mouseenter')
		 { _elem.addEventListener('mouseover', mouseEnter(_fn), _useCapture); }
	  else if (_evtName === 'mouseleave')
		 { _elem.addEventListener('mouseout', mouseEnter(_fn), _useCapture); }
	  else
		 { _elem.addEventListener(_evtName, _fn, _useCapture); }
   }
   else if (typeof _elem.attachEvent != 'undefined')
   {
	  _elem.attachEvent('on' + _evtName, _fn);
   }
   else
   {
	  _elem['on' + _evtName] = _fn;
   }
}
function mouseEnter(_fn){
   return function(_evt)
   {
	  var relTarget = _evt.relatedTarget;
	  if (this === relTarget || isAChildOf(this, relTarget))
		 { return; }

	  _fn.call(this, _evt);
   }
}
function isAChildOf(_parent, _child){
   if (_parent === _child) { return false; }
	  while (_child && _child !== _parent)
   { _child = _child.parentNode; }

   return _child === _parent;
}
////////////////////////////////////////ticker//////////////////////////////////
function Cookie(tran){
  this.tran=tran;
  this.setValue=function(name,value,ms,path,domain,secure){
    var str=new String();
    var nextTime=new Date();
	nextTime.setTime(nextTime.getTime()+ms);
    var val=this.tran==true?escape(value):value;
    str=name+"="+val;
    if(ms){
      str+=";expires="+nextTime.toGMTString();}
    if(path){
      str+=";path="+path;}
    if(domain){
      str+=";domain="+domain;}
    if(secure){
      str+=";secure";}
    document.cookie=str;
  };
  this.getValue=function(name){
    var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
    if(tmp=rs){
      return this.tran==true?unescape(tmp[2]):(tmp[2]);}
    return null;
  }
}
var cookie = new Cookie();
var title = document.title;
var imWindowId = new Array();
var chatWindowId = new Array();
var titleMsg = false;
var isTitleEmpty = false;
var iIntervalID;
var iTitleFlashID;
var intervalSentMsg = 1000*10;
var maxTickerNum = 5;
var currentTickerNum = 0;
var currMsgs = new Array();
var currUsers = new Array();
function initTicker(){
	if(!islogin || _show==false){
		return;
	}
	clearInterval(iIntervalID);
	var closeAll = '';
	if(_locale == "zh") closeAll = "关闭所有";
	else closeAll = "close all notifications";
	jQuery('<div class="ticker_right" id="jGrowl_popup"><div class="closeall" id="jGrowlcloseAll"><a href="#" onclick="jGrowlCloseAll();return false;">'+closeAll+'</a></div></div>').appendTo('body');
	var param = "action=getmsg&status=1&msgnum="+(maxTickerNum-currentTickerNum);
	XMLHttp.sendReq('GET', "/ticker/index.php?"+param, "" , tickerCallback,true);
	iIntervalID = setInterval("sentMsg()",intervalSentMsg);
	P1_Spawn_Im.rubyHeader();
}
function _getUsersPopInArray(uid){
	for(var i=0;i<currUsers.length;i++){
		if(currUsers[i] == uid) return true;
		else continue;
	}
	return false;
}
function _reBuildUsersPopArray(uid,num){
	jQuery("#jGrowl_"+uid).fadeOut(2000,function(){jQuery(this).remove()});
	var arr = new Array();
	var j = 0;
	for(var i=0;i<currUsers.length;i++){
		if(currUsers[i] == uid) continue;
		else{
			arr[j] = currUsers[i];
			j++;
		}
	}
	currUsers = arr;
	currentTickerNum = currentTickerNum - 1;
	//if(num == 1) sentMsg();
}
function buildTickerMsg(msg){
	if(!msg||msg.length == 0) return;
	var curTime=new Date();
	var curtimestamp = Math.ceil(parseInt(curTime.getTime())/1000);
	for(var i=0;i<msg.length;i++){
		msg[i]["timestamp"] = curtimestamp;
	}
	return msg;
}
function tickerCallback(data){
	eval("var d ="+data.responseText);
	var u = d.user;
	var g = d.group;
	m = buildTickerMsg(d.msg);

	if(m&&m.length>0){
		currMsgs = m;
		showTicker(u,g);
	}else{
		P1_Spawn_Im.userlist = u;
		P1_Spawn_Im.showOnlineUser(P1_Spawn_Im.userlist);
		P1_Spawn_Im.grouplist = g
		P1_Spawn_Im.showGroupList(P1_Spawn_Im.grouplist);
	}
}
function showTicker(u,g){
	if(!currMsgs||currMsgs.length == 0){
		clearTitleFlashInterval();
		return;
	}
	titleMsg = false;
	var len = currMsgs.length >= maxTickerNum ? maxTickerNum : currMsgs.length;
	for(var i=0;i<len;i++){
		var info = _getMsgMessage(currMsgs[i]);
		if(info["ticker_type"]==1){
			titleMsg = true;
			if(_getUsersPopInArray(info['f_uid'])){
					continue;
			}else{
				currUsers.push(info['f_uid']);
				currentTickerNum++;
			}
			var html = '<div class="newsticker100326" id="jGrowl_'+info['f_uid']+'"><a href="/'+info['f_uname']+'">'+info['img']+'</a><div class="ticker_l"><a href="/'+info['f_uname']+'"><span class="'+info['sex']+'">'+info['f_realname']+'</span></a></div><div class="ticker_r" onclick="popupIMWindow('+info['f_uid']+');clearTickerHtml('+info['f_uid']+','+info['ticker_type']+');return false;">'+info['message']+'</div><div class="close" onclick="_reBuildUsersPopArray('+info['f_uid']+',1);">×</div></div>';
			//jQuery.jGrowl(html,{sticky:true,closer:false,close:function(e,m,o){_reBuildUsersPopArray(m);}});
		}else if(info["ticker_type"]==2){
			var html = '<div class="newsticker100326" id="jGrowl_'+info['f_uid']+'"><a href="/'+info['f_uname']+'">'+info['img']+'<div class="ticker_l"><span class="'+info['sex']+'">'+info['f_realname']+'</span></div><div class="ticker_r">'+info['message']+'</div></a><div class="close" onclick="_reBuildUsersPopArray('+info['f_uid']+',1);">×</div></div>';
			//$.jGrowl(html,{life: 3000});
		}else if(info["ticker_type"]==3){
			if(_getUsersPopInArray(info['f_uid'])){
					continue;
			}else{
				currUsers.push(info['f_uid']);
				currentTickerNum++;
			}
			var html = '<div class="newsticker100326" id="jGrowl_'+info['f_uid']+'"><a href="/group/index.php?group_id='+info['f_uid']+'">'+info['img']+'</a><div class="ticker_g" onclick="popupChatWindow('+info['f_uid']+');clearTickerHtml('+info['f_uid']+','+info['ticker_type']+');return false;">'+info['message']+'</div><div class="close" onclick="_reBuildUsersPopArray('+info['f_uid']+',1);">×</div></div>';
			//jQuery.jGrowl(html,{sticky:true,closer:false,close:function(e,m,o){_reBuildUsersPopArray(m);}});
		}
		jGrowl(html,info['f_uid'],info['ticker_type']);
	}
	if(jQuery("#jGrowl_popup").find(".newsticker100326").length > 1) jQuery("#jGrowlcloseAll").css("display","block");

	P1_Spawn_Im.userlist = u;
	P1_Spawn_Im.showOnlineUser(P1_Spawn_Im.userlist);
	P1_Spawn_Im.grouplist = g
	P1_Spawn_Im.showGroupList(P1_Spawn_Im.grouplist);
}
function jGrowl(html,uid,type){
	if(navigator.userAgent.indexOf('MSIE 6.0') > -1){
		var top = document.documentElement.scrollTop || document.body.scrollTop;
		top += 46;
		jQuery("#jGrowl_popup").css("top",top + "px");
	}
	jQuery(html).prependTo(jQuery("#jGrowl_popup")).animate({"opacity":"0.8"});
	//jQuery(html).prependTo(jQuery("#jGrowl_popup")).css("display","none").fadeIn(2000);
	if(type == 2){
		setTimeout(function(){jQuery("#jGrowl_"+uid).fadeOut("slow",function(){jQuery(this).remove();checkCloseAll();});},4000);
	}else{
		
	}
	checkCloseAll();
}
function jGrowlCloseAll(){
	jQuery("#jGrowl_popup").find(".newsticker100326").each(function(){
		var id = jQuery(this).attr("id");
		var tempuid = parseInt(id.slice(7,id.length));
		_reBuildUsersPopArray(tempuid,2);
	}).remove();
	jQuery("#jGrowlcloseAll").css("display","none");
}
function sentMsg(){
	if(!islogin || _show==false){
		return;
	}
	var param = "action=getmsg&status=1&msgnum="+(maxTickerNum-currentTickerNum);
	XMLHttp.sendReq('GET', "/ticker/index.php?"+param, "" , tickerCallback,true);
}
function _getMsgMessage(msg){
	if(msg == undefined) return false;
	if(msg['ticker_type']==1){
		msg['message'] = "sent you a message.";
		if(msg['f_uimg'] && msg['f_uimg'] != "")
			msg['img'] = '<img src="/user/image/'+msg['f_uid']+'/thumb/'+msg['f_uimg']+'" width="24" height="30" />';
		else
			msg['img'] = '<img src="/images/gfx/avatar40x50.jpg" width="24" height="30"/>';
	}else if(msg['ticker_type']==2){
		msg['message'] = "signed in.";
		if(msg['f_uimg'] && msg['f_uimg'] != "")
			msg['img'] = '<img src="/user/image/'+msg['f_uid']+'/thumb/'+msg['f_uimg']+'" width="24" height="30" />';
		else
			msg['img'] = '<img src="/images/gfx/avatar40x50.jpg" width="24" height="30" />';
	}else if(msg['ticker_type']==3){
		msg['message'] = "New message : "+msg['f_uname'];
		msg['img'] = "<img src=/img/ticker/newticker_online_group.gif />";
	}
	msg['sex'] = msg['f_usex'] == 1 ? "female" : "male";
	return msg;
}
function clearTickerHtml(fid_a,type_a){
	if(currentTickerNum > 0)
		currentTickerNum--;
	
	_reBuildUsersPopArray(fid_a,1);
	titleMsg = false;
	document.title = title;
	var param = "action=clearmsg&fid_a="+fid_a+"&type_a="+type_a;
	XMLHttp.sendReq('GET', "/ticker/?"+param, "" , tickerCallback, true);
	checkCloseAll();
}
function checkCloseAll(){
	if(jQuery("#jGrowl_popup").find(".newsticker100326").length > 1){
		jQuery("#jGrowlcloseAll").css("display","block");
	}else{
		jQuery("#jGrowlcloseAll").css("display","none");
	}
}
function popupChatWindow(groupid){
        var windowWidth, windowHeight, windowLeft, windowTop;
        var strUrl = "/group/chatroom_act.php?group_id="+groupid;
        if(typeof window.screenX == "number" && typeof window.innerWidth == "number"){
                //windowWidth = window.innerWidth * .68;
                //windowHeight = window.innerHeight * .68;
                windowLeft = window.screenX + window.innerWidth * .25;
                windowTop = window.screenY + window.innerHeight * .2;
        }else if(typeof window.screenTop == "number" && typeof document.documentElement.offsetHeight == "number"){
                //windowWidth = document.documentElement.offsetWidth * .68;
                //windowHeight = document.documentElement.offsetHeight * .68;
                windowLeft = window.screenLeft + document.documentElement.offsetWidth * .25;
                windowTop = window.screenTop - 50 + + document.documentElement.offsetHeight * .2;
        }else{  
                //windowWidth = 500;
                //windowHeight = 250;
                windowLeft = 60;
                windowTop = 40;
        }       
        windowWidth= 520;
        windowHeight = 540;
        if (chatWindowId[groupid] == null || chatWindowId[groupid].closed){
                chatWindowId[groupid] = window.open(strUrl, groupid, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth + ",height=" + windowHeight + ",menubar=no,toolbar=no,location=yes,resizable=no,scrollbars=no,status=no");
                chatWindowId[groupid].focus();
        }else{  
                chatWindowId[groupid].focus();
        }       
}
function popupIMWindow(toid){
	var windowWidth, windowHeight, windowLeft, windowTop;
	var strUrl = "/im/im_msg.php?ttag=ticker&user="+toid;
	if(typeof window.screenX == "number" && typeof window.innerWidth == "number"){
		//windowWidth = window.innerWidth * .68;
		//windowHeight = window.innerHeight * .68;
		windowLeft = window.screenX + window.innerWidth * .25;
		windowTop = window.screenY + window.innerHeight * .2;
	}else if(typeof window.screenTop == "number" && typeof document.documentElement.offsetHeight == "number"){
		//windowWidth = document.documentElement.offsetWidth * .68;
		//windowHeight = document.documentElement.offsetHeight * .68;
		windowLeft = window.screenLeft + document.documentElement.offsetWidth * .25;

		windowTop = window.screenTop - 50 + + document.documentElement.offsetHeight * .2;
	}else{
		//windowWidth = 500;
		//windowHeight = 250;
		windowLeft = 60;
		windowTop = 40;
	}
	windowWidth= 520;
	windowHeight = 540;
	if (imWindowId[toid] == null || imWindowId[toid].closed){
		imWindowId[toid] = window.open(strUrl, toid, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth + ",height=" + windowHeight + ",menubar=no,toolbar=no,location=yes,resizable=no,scrollbars=no,status=no");
		imWindowId[toid].focus();
	}else{
		imWindowId[toid].focus();
	}
}
function setTitleFlashInterval(){
	clearInterval(iTitleFlashID);
	iTitleFlashID = setInterval("titleFlash()", 500);
}
function clearTitleFlashInterval(){
	titleMsg = false;
	clearInterval(iTitleFlashID);
	iTitleFlashID="";
	document.title = title;
}
function titleFlash(){
	if(titleMsg){
		if(isTitleEmpty){
			document.title = "【New Message】"+title;
			isTitleEmpty = false;
		}else{document.title = "【　　　　　 】"+title;
			isTitleEmpty = true;
		}
	}
}
function ticker_set(group_id, btn){
	var msg = "";
	var action = 0;
	if (btn.innerHTML == "block")
	{
		action = 1;
		msg = "unblock";
	}
	else
	{
		action = 0;
		msg = "block";
	}
	XMLHttp.sendReq('GET', "/group/ticker_set.php?action="+action+"&group_id="+group_id+"&t="+Math.random(), "" , "",true);
	btn.innerHTML=msg;
}
function showTab(i){
	if (i == 0){
		document.getElementById("user_list").style.display="block";
		document.getElementById("user_tab").className="over";
		document.getElementById("group_list").style.display="none";
		document.getElementById("group_tab").className="";
	}else{
		document.getElementById("user_list").style.display="none";
		document.getElementById("user_tab").className="";
		document.getElementById("group_list").style.display="block";
		document.getElementById("group_tab").className="over";
	}
}
function headSearch(type){
	var q = document.getElementById("headsearch").value;
	if(q == "搜索P1会员" || q == "Search people on P1") q = '';
	if(q == "") return false;
	if(type == 1) window.location = '/browse/search.php?q='+q+"&search=1&c=1,2,3,4,5,6";
	else if(type == 2) window.location='/'+q;
	return true;
}

var P1_Spawn_Im = {
	lineucnt:false,
	cntlimit:9,
	userlist:[],
	grouplist:[],

	showGroupList:function(){
		var g = this.grouplist;
		var html = '<ul>';
		var g_cnt = g.length;
		var count = g_cnt;
		for(var i=0;i<g_cnt;i++)
		{
			if (g[i].chat_type == "1")
			{
				count--;
				continue;
			}
			html += '<li onmouseover="this.className=\'over\';document.getElementById(\'btn'+g[i].id+'\').style.display=\'block\'" onmouseout="this.className=\'\';document.getElementById(\'btn'+g[i].id+'\').style.display=\'none\'"><span class="tg_name"><a href="/group/index.php?group_id='+g[i].id+'" class="grou_ico"><img src="/img/ticker/im_group_color.gif"/></a><a href="#" onclick="popupChatWindow('+g[i].id+');return false;">'+g[i].name+'</a><label>('+g[i].num+')</label></span><a href="#" onclick="ticker_set('+g[i].id;
			html += ',this);return false;" class="button" id="btn'+g[i].id+'" style="display:none;">';
			if (g[i].tset)
				html += "unblock";
			else
				html += "block";
			html += '</a></li>';
		}
		html += '</ul>';
		document.getElementById("group_list").innerHTML = html;
		if(count <= this.cntlimit){
			document.getElementById("group_list").className = "ticker_online_group";
		}else{
			document.getElementById("group_list").className = "ticker_online_group_h";
		}
		if (count == 0)
			document.getElementById("group_tab").style.display="none";
	},
	
	showOnlineUser:function(){
		var u = this.userlist;
		var u_cnt = u.length;
		var u_cnthtml = '';
		u_cnt = u_cnt != undefined ? u_cnt : 0;
		var html = '';
		var maildis = '';
		if(u_cnt > 0){
			document.getElementById("_userEmpty").style.display = "none";
			document.getElementById("_userHas").style.display = "";
			this.lineucnt = true;
		}else{
			document.getElementById("_userEmpty").style.display = "";
			document.getElementById("_userHas").style.display = "none";
			this.lineucnt = false;
		}
		if(u_cnt == 0){
			document.getElementById("people_number").innerHTML = "<label>"+u_cnt+"</label>";
		}else{
			var u_cnt_str = u_cnt.toString().split("");
			for(var j=0;j<u_cnt_str.length;j++)
			{
				u_cnthtml += "<label>"+u_cnt_str[j]+"</label>"
			}
			document.getElementById("people_number").innerHTML = u_cnthtml;
		}
		
		for(var i=0;i<u_cnt;i++){
			u[i].sex = u[i].sex == 1 ? "female" : "male";
			u[i].head_path_thumb=u[i].head_path_thumb!=undefined?u[i].head_path_thumb:"/img/mypage_act/avatar24x30.jpg";
			maildis = u[i].new_msg_num > 0 ? "" : "none";
			html += '<li class="" onmouseover="this.className=\'changebg\'" onmouseout="this.className=\'\'"><a href="/'+u[i].username+'"><img src="'+u[i].head_path_thumb+'" width="24" height="30" border="0" ></a><a href="#" class="'+u[i].sex+' online_right" onclick="clearTickerHtml('+u[i].userid+');popupIMWindow('+u[i].userid+');"><span>'+u[i].realname+'</span><img src="/img/ticker/plm.gif" class="info_img" border="0" style="display:'+maildis+'" id="p2m'+u[i].userid+'"></a></li>';
		}
		document.getElementById("userlist").innerHTML = html;
		if(u_cnt <= this.cntlimit){
			document.getElementById("select_list").className = "select_list";
		}else{
			document.getElementById("select_list").className = "select_list_h";
		}
	},
	
	HeadshowUser:function(){
		if(this.lineucnt){
			document.getElementById("ticker_friend_select").style.display = "block";
			document.getElementById("select_list").style.display = "block";
			document.getElementById("ticker_friend_select_no").style.display = "none";
		}else{
			document.getElementById("ticker_friend_select").style.display = "block";
			document.getElementById("select_list").style.display = "none";
			document.getElementById("ticker_friend_select_no").style.display = "block";
		}
		document.getElementById("onlinemember").className = "over";
	},
	
	HeadhideUser:function(){
		document.getElementById("onlinemember").className = "";
		document.getElementById("ticker_friend_select").style.display = "none";
		document.getElementById("select_list").style.display = "none";
		document.getElementById("ticker_friend_select_no").style.display = "none";
	},

	rubyHeader:function(){
		var user_login = cookie.getValue("user_login");
		var html = [
			'<div class="ticker_con">',
				'<div class="ticker_friend" id="ticker_friend">',
					'<div class="ticker_friend_l">',
						'<img src="/img/ticker/ticker_friend_img_online.gif" width="20" height="17" border="0" style="display:none;" id="_userHas">',
						'<img src="/img/ticker/ticker_friend_img_default.gif" width="20" height="17" border="0" id="_userEmpty">Chat',
					'</div>',
					'<div class="ticker_friend_r">',
						'<a href="#"  id="onlinemember" onclick="P1_Spawn_Im.HeadshowUser();return false;"><span class="people_number_con">',
							'<span class="people_number" id="people_number">0</span></span>',
						'</a>',
					'</div>',
				' </div>',
				'<div class="ticker_msg_list" id="ticker_msg_list" style="display:none;" onmouseover="showTickerMsgList()" onmouseout="hideTickerMsgList()">',	
				'</div>',
				'<div class="ticker_friend_select" id="ticker_friend_select" style="display:none;">',
					'<div class="ticker_tab">',
						'<ul>',
							'<li id="user_tab" onclick="showTab(0);" class="over">Friends</li>',
							'<li id="group_tab" onclick="showTab(1);">Groups</li>',
						'</ul>',
					'</div>',
					'<div class="clear"></div>',
					'<div class="ticker_online_member" id="user_list">',
						'<div class="select_list" id="select_list">',
							'<ul id="userlist"></ul>',
						'</div>',
						'<div class="select_none" id="ticker_friend_select_no" style="display:none;">',
							'您现在没有好友在线，<br />',
							'<a href="/browse/">看看其他在线会员</a> <img src="/img/ticker/ticker_icon.gif">',
						'</div>',
						'<div class="select_notonline"><a href="/incoming/message.php">查看离线好友</a></div>',
					'</div>',
					'<div class="ticker_online_group" id="group_list" style="display:none;">',
						'<ul>',
						'</ul>',
					'</div>',
				'</div>',
				'<div id="newsticker" class="topsearch">',
						'<input name="q" type="text" class="topsearch_put" value="Search people on P1" id="headsearch" autocomplete="off" onfocus="this.value=\'\';this.className=\'topsearch_puted\';" /><img src="/img/topsearch_but.gif" class="topsearch_but" alt="" onmouseover="this.src=\'/img/topsearch_but_over.gif\';" onmouseout="this.src=\'/img/topsearch_but.gif\'"; onclick="headSearch(1);">',
					'<div class="search_select" id="headsearchresult">',
						'<ul>',
						'</ul>',
						'<div class="clear"></div>',
					'</div>',
				'</div>',
			'</div>',
		].join("");
		document.getElementById("header_broadcast").innerHTML = html;
		
		if(document.getElementById("ticker_friend") != undefined) {
			myaddEvt(document.getElementById("ticker_friend"),"mouseenter", function(){
				P1_Spawn_Im.HeadshowUser();
			});
			myaddEvt(document.getElementById("ticker_friend"),"mouseleave", function(){
				P1_Spawn_Im.HeadhideUser();
			});
			myaddEvt(document.getElementById("ticker_friend_select"),"mouseenter", function(){
				P1_Spawn_Im.HeadshowUser();
			});
			myaddEvt(document.getElementById("ticker_friend_select"),"mouseleave", function(){
				P1_Spawn_Im.HeadhideUser();
			});
		}
		P1_search.init();
	}
};
var P1_search={
	directKey:0,

	init:function(){
		var self = this;
		var obj = jQuery("#headsearch");
		
		self.keyInit();
		if(obj == null) return false;
		obj.autocomplete("/ticker/search.php", {
			width: 188,
			max: 8,
			selectFirst: false,
			scroll: false,
			scrollHeight: 300,
			dataType: "json",
			parse: function(data) {
				return jQuery.map(data, function(row) {
					return {
						data: row,
						value: row.truename,
						result: row.login
					}
				});
			},
			formatItem: function(item) {
				return self.buildData(item);
			}
		}).result(function(e,item) {
			//var str = "<scr"+"ipt>window.location='/"+item.login+"';<\/scr"+"ipt>";
			//jQuery(str).appendTo('body');
			self.directKey = 1;
			headSearch(2);
			return true;
		});
	},
	
	keyInit:function(){
		var self = this;
		jQuery("#headsearch").keyup(function(event){
			if(event.keyCode==13 && self.directKey != 1){
				headSearch(1);
				return true;
			}
		});
	},

	buildData:function(d){
		if (d == "") {
			jQuery(".ac_results").children().css({"display":"none"});
			return false;
		}else{
			jQuery(".ac_results").children().css({"display":"block"});
		}
		var html = '';
		d._sex = d.sex == 1 ? "female" : "male";
		if(d.position){
			html += ['<div onclick="P1_search.setVal(\''+d.login+'\');">',
								'<a href="/'+d.login+'" ><img src="'+d.image+'" alt="" width="24" height="30"></a>',
								'<p><a href="/'+d.login+'" class="'+d._sex+'">'+d.truename+'</a></p>',
								'<p>'+d.position+'</p>',
							'</div>'].join("");
		}else{
			html += ['<div  onclick="P1_search.setVal(\''+d.login+'\');">',
								'<a href="/'+d.login+'" ><img src="'+d.image+'" alt="" width="24" height="30"></a>',
								'<p class="pad"><a href="/'+d.login+'" class="'+d._sex+'">'+d.truename+'</a></p>',
							'</div >'].join("");
		}
		return html;
	},
	
	setVal:function(v){
		//jQuery("#headsearch").val(v);
		//jQuery(".ac_results").children().css({"display":"none"});
		top.location = "/"+v;
		return true;
	}
};

addEvent(window, "focus", initTicker);
addEvent(window, "blur", setTitleFlashInterval);
//addEvent(window, "load", initTicker);
