if (typeof(TopPanel)!='function'){
	document.write("<script type=\"text/javascript\" src=\"http://js.tiexue.net/com/tiexue/TopPanel.js\"></script>");
}

var FriendConfig = {
	GoodFriendUrl:"http://i.tiexue.net/js/InfoBar.aspx?FunName=GetMyFriend&num=10",	//取好友列表的链接地址
	AddDelFriendUrl:"http://i.tiexue.net/AddDeleteFriend.aspx?UserID=",	//添加删除好友
	AddMyFriendUrl:"http://i.tiexue.net/AddFriendByName.aspx",//添加好友
	JudgeFriendUrl:"http://i.tiexue.net/JudgeFriend.aspx?UserID=",//判断是否为好友
	OverTime:10,	//10分钟数据超时失效
	RefreshDataTime:10	//同步好友数据
}
var FriendInfo = null;	//好友信息管理实例

GoodFriend = function(){
	this.IsGetting = false;	//正在获取数据
	this.CacheData = "";	//好友数据
	this.CallBack = null;

	this.PanelReady = false;

	//启动最新好友定时获取功能
	this.oIntervalCache=null;
	this.GetMyFriendsFromCookie();
	//this.InitPanel();

}

//定时器调用函数，更新网页数据
GetMyFriendsFromCookie = function(){
	if (FriendInfo!=null){
		FriendInfo.GetMyFriendsFromCookie();
	}
}
//服务器端的反馈信息,同时设置数据的失效时间
Tx_GetMyFriendFun = function(value){
	if (FriendInfo==null) return;

	FriendInfo.CacheData = value;
	FriendInfo.IsGetting=false;
	//if (value=="")return;
	//更新缓冲Cookie值
	temp = value;
	if (temp==""){
		temp="null";
	}
	//取当前时间
	var curdate = new Date();
	curdate.setTime(curdate.getTime()+FriendConfig.OverTime*60000);
    temp = temp + "|" + curdate.getFullYear() + "-" + (curdate.getMonth()+1) + "-" + curdate.getDate() + " " + curdate.getHours() + ":" + curdate.getMinutes() + ":" + curdate.getSeconds();
	BaseCookie.SetCookie("TxFriends",escape(temp),3600*24);
	//更新数据失效时间
	//BaseCookie.SetCookie("FDataAvail","1",FriendConfig.OverTime*60);//数据有效
	if (FriendInfo.CallBack!=null){
		if (typeof FriendInfo.CallBack == 'function'){
			FriendInfo.CallBack(value);
		}
	}
	document.getElementById("TxFriendJs").outerHTML="";
}
GoodFriend.prototype = {
	RunTimer:function(AutoDetect,callBackFun){
		this.CallBack = callBackFun;
		if (AutoDetect){
			if (this.oIntervalCache==null){
				this.oIntervalCache=window.setInterval("GetMyFriendsFromCookie()",FriendConfig.RefreshDataTime * 1000);
			}
		}else{
			if (this.oIntervalCache!=null){
				window.clearInterval(this.oIntervalCache);
				this.oIntervalCache=null;
			}
		}
	},
	InitPanel:function(){
		if (this.PanelReady) return;
		obj = document.getElementById("TxMyFriendDlg");
		if (obj==null){	//对象未定义！
			var msgObj=document.createElement("div");
			msgObj.setAttribute("id","TxMyFriendDlg");
			msgObj.innerHTML = "<div class='hd'></div><div class='bd'></div>";
			document.body.appendChild(msgObj);
		}
		if (YAHOO.example.container==null) YAHOO.namespace("example.container");

		// Instantiate the Dialog
		YAHOO.example.container.TxMyFriendDlg = new YAHOO.widget.Dialog("TxMyFriendDlg", 
																	{ width : "250px",height:"100%",
																	  fixedcenter : true,
																	  visible : false,
																	  constraintoviewport : true
																	 });
		//YAHOO.example.container.TxMyFriendDlg.setHeader("<div class='tl'></div><span>添加好友</span><div class='tr'></div>");													 
		YAHOO.example.container.TxMyFriendDlg.render();
		
		this.PanelReady = true;
	},
	
	SendSubmit:function(){
		var obj = document.getElementById("TxAddUserName");
		var UserName = obj.value;
		if (UserName==""){
			TxAlert('好友名不能为空');
			return;
		}
		this.AddMyFriend(UserName);
	},
	
	OpenAddDlg:function(UserName,CallBack){
		if (!this.PanelReady) return;
		this.CallBack = CallBack;
		var SendHtmlData = '<table cellSpacing="0" cellPadding="0" width="97%" height="100%" border="0" align="center">';
		SendHtmlData += '<tr><td colspan="3"></td></tr>';
		SendHtmlData += '<tr>';
		//SendHtmlData += '	<td noWrap></td>';
		SendHtmlData += '	<td colspan="2" noWrap>添加登录名：<input id="TxAddUserName" class="inp" style="WIDTH:110px;HEIGHT: 15px" maxLength="80" NAME="TxAddUserName"></td>';
		SendHtmlData += '</tr>';
		//SendHtmlData += '<tr><td colspan="3"><input type="checkbox" value="on" ID="ToSendedCheck" NAME="ToSendedCheck">将发送短信保存到“已发送的”中&nbsp;</td></tr>';
		SendHtmlData += '<tr>';
		SendHtmlData += '	<td vAlign="middle" align="center" colSpan="3" height="40px">';
		SendHtmlData += '		<INPUT id="AddFriendBt" type="button" value="添加" onClick="FriendInfo.SendSubmit();">&nbsp;';
		SendHtmlData += '		<INPUT id="CancelAddBt" type="button" value="取消" onClick="YAHOO.example.container.TxMyFriendDlg.hide()">&nbsp;';
		SendHtmlData += '	</td>';
		SendHtmlData += '</tr>';
		SendHtmlData += '</table>';

		with(YAHOO.example.container.TxMyFriendDlg){
			setHeader("<div class='tl'></div><font size ='2'>添加好友</font><div class='tr'></div>");
			setBody(SendHtmlData);
			show();
		}
	},

	AddMyFriend:function(UserName){
		var SendCallback =
		{
			success:function(o){
			    TxProcessHide();
				if(o.responseText !== undefined){
					YAHOO.example.container.TxMyFriendDlg.hide();
					if (o.status==200){
						var temp = o.responseText;
						if (FriendInfo.CallBack!=null)
							FriendInfo.CallBack(temp);
						else
							TxAlert(temp);
					}
				}
			},
			failure:function(o){
			    TxProcessHide();
				if(o.responseText !== undefined){
					YAHOO.example.container.TxMyFriendDlg.hide()
					TxAlert(o.statusText);
				}
			}
		};
		var sUrl = "agent.aspx?desturl=" + encodeURIComponent(FriendConfig.AddMyFriendUrl);
		var postData = "UserName=" + encodeURIComponent(UserName);
		var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, SendCallback,postData);
		TxProcessShow("正在加好友,请等待...",true);
	},


	AddDelMyFriend:function(UserID,CallBack,UserName){	
	    var SendJudgeCallback =
	    {
	       
	    	success:function(o){
	    	    TxProcessHide();
	    		if(o.responseText != undefined){
	    			if(o.status == 200){
	    				var returnText = o.responseText;
	    				if(returnText != ""){
	    				    var temp = returnText.split('|');
	    				    var msg = "";
	    				    var isFailure = false;
	    				    if(temp[1] == 1){//是好友
	    					    msg = "已经是好友，确定删除好友"+ UserName +"？";
	    				    }
	    				    else if(temp[1] == 0){//不是好友
	    					    msg = "确定添加"+ UserName +"为好友？";
	    				    }
	    				    else{//错误格式
	    					    msg = temp[2];
	    					    isFailure = true;
	    				    }
	    				}
	    				else{
	    				    msg = "服务器错误,请重试!";
	    				    isFailure = true;
	    				}
	    				if(!isFailure){
	    				
	    					function ClickOK(){
	    					
							    var SendCallback =
							    {
								    success:function(o){
								        TxProcessHide();
									    if(o.responseText !== undefined){
										    if (o.status==200){
											    var temp = o.responseText;
											    if (CallBack!=null)
												    CallBack(temp,UserID);
											    else
												    TxAlert(temp);
										    }
									    }
								    },
								    failure:function(o){
								        TxProcessHide();
									    if(o.responseText !== undefined){
										    TxAlert(o.statusText);
									    }
								    }
							    };
							    
							    var sUrl = "agent.aspx?desturl=" + encodeURIComponent(FriendConfig.AddDelFriendUrl + UserID + "&rd=" + Math.random());
							    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, SendCallback);	
							    TxProcessShow("正在操作,请等待...",true); 
						    }
	    					TxConfirm(msg,ClickOK);
						}
						else{
							TxAlert(msg);						
						}
	    			}
	    		}	    		
	    	},
			failure:function(o){
			    TxProcessHide();
				if(o.responseText !== undefined){
					TxAlert(o.statusText);
				}
			}
	    };
	    var jUrl = "agent.aspx?desturl=" +  encodeURIComponent(FriendConfig.JudgeFriendUrl + UserID + "&rd=" + Math.random())
	    var request = YAHOO.util.Connect.asyncRequest('GET', jUrl, SendJudgeCallback);
	    TxProcessShow("正在操作,请等待...",true); 
	},

	//从服务器端获取新短信数据
	GetMyFriendsFromServer:function(){
		var TxTB_UserID = BaseCookie.TxBBS_CurrentUserID();
		if (TxTB_UserID==0) return;

		this.IsGetting=true;
		var objJs = document.getElementById("TxFriendJs");
		if (objJs==null){
			var IsFinish = true;
			if(document.all){
				if (document.readyState!="complete") IsFinish=false;
			}
			if (!IsFinish) return;
			objJs = document.createElement("script");
			objJs.setAttribute("id","TxFriendJs");
			objJs.setAttribute("language","javascript");
			try{document.body.appendChild(objJs);}catch(e){return;}
		}
		
		var Url = FriendConfig.GoodFriendUrl;
		if (Url.lastIndexOf("?")!=-1){
			Url += "&";
		}else{
			Url += "?";
		}
		Url += "tmp=" + Math.round(Math.random()*10000);
		objJs.src = Url;
	},
	//从缓冲区中取判断是否有新短信
	GetMyFriendsFromCookie:function(){
		//读取Cookie
		var temp = BaseCookie.GetCookie("TxFriends");
		//如果Cookie获取为空值,立即从服务器段获取
		if (temp==null){
			this.GetMyFriendsFromServer();
			return this.CacheData;
		}
		var arr = unescape(temp).split("|");

        this.CacheData = arr[0];
		//判断好友数据是否过期 FDataAvail=1:有效,=0:正在获取,=null:数据过期
		if (arr.length==2){
		    SaveDate = arr[1];
		    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
	        var r = SaveDate.match(reg); 
	        if(r!=null){
		        var dt = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
		        var curdate = new Date();
		        if (curdate>dt){
		            this.GetMyFriendsFromServer();
		        }
	        }
	    }
	    
		return this.CacheData;
	},
	ClearCookie:function(){
		BaseCookie.ClearCookie("TxFriends");
		this.CacheData="";
	}

}
var cswmTI="";
var RollPlace=0;//外部滚动后的滚动Top位置(xy修改好友弹出窗位置bug)
function ClearPanel(){
	var obj = document.getElementById("PopupPanel");
	if (obj!=null)obj.outerHTML = "";
}
function HideMenu(){cswmTI=setTimeout("ClearPanel()",700);}
function StopHideMenu(){clearTimeout(cswmTI);}
//鼠标移动到头像上出现右侧菜单按钮,好友列表页
function SetFriendFaceMenu(isShow,obj){
    if(isShow){
    	YAHOO.util.Dom.removeClass(obj,"area2_hy_4_hide");
    	YAHOO.util.Dom.addClass(obj,"area2_hy_4");
    	YAHOO.util.Dom.setStyle(obj,"display","");
    }
    else{
    	YAHOO.util.Dom.removeClass(obj,"area2_hy_4");
    	YAHOO.util.Dom.addClass(obj,"area2_hy_4_hide");
    }
}

//弹出窗口
OnClickEvent = function(thisObj,uID,uName){
//  added by kongweijia at 2008-02-25 11:31
    if(document.all){//是否为IE。ff不支持document.readyState.wangzhe modify at 2008-2-27
        if(document.readyState != "complete") return;
    }
	
    var userID = uID;
    var userName = uName;
    var obj = document.getElementById("PopupPanel");
    if(obj == null){
        obj=document.createElement("div");
		obj.className = "menu";
		obj.id = "PopupPanel";
	    obj.onclick = function(){this.outerHTML = "";}
	    // geyongjun and lichongxin modify at 2008-3-20
	    obj.onmouseout = function(){HideMenu();}
	    obj.onmouseover = function(){StopHideMenu();}
	    obj.onblur = function(){this.outerHTML = "";}
		document.body.appendChild(obj);
    }
    var temp = '<A class="menuItem" href="javascript:FriendInfo.AddDelMyFriend(' + userID + ',AddDelFriendCallBack,\''+ userName +'\')">删除好友</A>';
	temp += "<A class='menuItem' href='javascript:DlgInfo.CreateInfo(\"" + userName + "\")'>发送短信</A>";
	temp += '<A class="menuItem" href="http://'+ userID + '.i.tiexue.net" target="_blank">个人中心</A>';
	temp += '<A class="menuItem" href="http://'+ userID + '.i.tiexue.net/blog/" target="_blank">博客</A>';
	obj.innerHTML = temp;
	//设置显示的位置
	var top = getIEPosY(thisObj);
	var left = getIEPosX(thisObj);
	obj.style.left = left-80 + "px";
	obj.style.top = top+98-RollPlace + "px";

	obj.style.visibility="visible";
	HideMenu();
}

function AddDelFriendCallBack(value,uId){
	if (value.indexOf('成功删除')==0 && uId != null && uId != "" && document.getElementById("Friend"+uId) != null && document.getElementById("Friend"+uId) != undefined){
	    document.getElementById("Friend"+uId).outerHTML = "";
	    //更新Cookie
	    FriendInfo.ClearCookie();
		FriendInfo.GetMyFriendsFromServer();
	}
	TxAlert(value);
}

// 得到IE中各元素真正的位移量，即使这个元素在一个表格中
function getIEPosX(elt) { return getIEPos(elt,"Left"); }
function getIEPosY(elt) { return getIEPos(elt,"Top"); }
function getIEPos(elt,which) {
 iPos = 0
 while (elt!=null) {
  iPos += elt["offset" + which]
  elt = elt.offsetParent
 }
 return iPos
}

//个人中心页弹出窗口
OnClickEventHome = function(thisObj,uID,uName){
//  added by kongweijia at 2008-02-25 11:31
    if(document.all){//是否为IE。ff不支持document.readyState.wangzhe modify at 2008-2-27
       if(document.readyState != "complete") return;
    }
    var userID = uID;
    var userName = uName;
    var obj = document.getElementById("PopupPanel");
    if(obj == null){
        obj=document.createElement("div");
		obj.className = "menu";
		obj.id = "PopupPanel";
		obj.onclick = function(){this.outerHTML = "";}
		// lichongxin modify at 2008-3-20
		obj.onmouseout = function(){HideMenu();}
	    obj.onmouseover = function(){StopHideMenu();}
	    obj.onblur = function(){this.outerHTML = "";}
		document.body.appendChild(obj);
    }
    var temp = '<A class="menuItem" style="white-space:nowrap;" href="javascript:void(0)" onclick="FriendInfo.AddDelMyFriend(' + userID + ',AddDelFriendCallBack,\''+ userName +'\')">添加(删除)好友</A>';
	temp += "<A class='menuItem' href='javascript:void(0)' onclick='DlgInfo.CreateInfo(\"" + userName + "\")'>发送短信</A>";
	temp += '<A class="menuItem" href="http://'+ userID + '.i.tiexue.net" target="_blank">个人中心</A>';
	temp += '<A class="menuItem" href="http://'+ userID + '.i.tiexue.net/blog/" target="_blank">博客</A>';
	obj.innerHTML = temp;
	
	//设置显示的位置
	var top = getIEPosY(thisObj);
	var left = getIEPosX(thisObj);
	obj.style.left = left-60 + "px";
	obj.style.top = top+50 + "px";
	obj.style.visibility="visible";
	HideMenu();
}

//个人中心页回调
function HomeAddDelFriendCallBack(value,uId){
	TxAlert(value);
}