function ToolBar() {
	this.toolBarId = "float_toolbar";
	this.messageId = "float_message";
	this.handle;//控制main状态
	this.mainObj;
	this.fansId = "float_fans";
	this.alertId = "float_alert";
	this.alertListener;
	this.newMessageBrick = 0;
	this.newMessageBrickHandler;
	this.broadCastId = "float_broadcast";
	
	//显示工具条 
	//Integer pid, String username
	this.main = function (thisObj, pid, username) {
		if (this.handle != null)
			this.clearHandle();
		this.mainObj = thisObj;
		if (!pid) pid = 0;
		if (!username) username = "";
		var url = "/float/toolbar/index.php?pid=" + pid + "&username=" + username + "&r=" + Math.random();
		var request = new Request(
						function()
						{
							if (request.xmlhttp.readyState == 4)
							{
								var divId = (new ToolBar).toolBarId;
								var div = document.getElementById(divId);
								if (!div)
								{
									div = document.createElement("DIV");
									div.id = divId;
									document.body.appendChild(div);
								}
								div.innerHTML = request.xmlhttp.responseText;
								(new ToolBar).show(div, lib.x(thisObj) + thisObj.offsetWidth + 2, lib.y(thisObj) - 8);
							}
						}		
					);
		request.get(url);
	}
	
	//消失工具条
	this.remove = function (thisObj, delay) {
		if (!delay) delay = 300;
		this.handle = window.setTimeout("(new ToolBar).close((new ToolBar).toolBarId)", delay);
	}
	
	//清除handle
	this.clearHandle = function() {
		window.clearInterval(this.handle);
	}
	
	//传纸条 
	//Integer pid, String username
	//typeId: 0 - 直接打开  1 - 通过菜单打开
	this.message = function (thisObj, pid, username, typeId) {
		this.remove(thisObj, 1);
		if (!this.isLogin("给人传纸条是要登录的哦！")) 
			return false;
		if (!pid) pid = 0;
		if (!username) username = "";
		if (!typeId) typeId = 0;
		var url = "/float/message/index.php?pid=" + pid + "&username=" + username + "&r=" + Math.random();
		var mainObj = this.mainObj;
		var request = new Request(
						function()
						{
							if (request.xmlhttp.readyState == 4)
							{
								var divId = (new ToolBar).messageId;
								var div = document.getElementById(divId);
								if (!div)
								{
									div = document.createElement("DIV");
									div.id = divId;
									document.body.appendChild(div);
								}
								div.innerHTML = request.xmlhttp.responseText;
								switch (typeId) {
									case 0:
										(new ToolBar).show(div, lib.x(thisObj) + 15, lib.y(thisObj)+15);
										break;
									case 1:
										(new ToolBar).show(div, lib.x(mainObj), lib.y(mainObj)+15);
								}
							}
						}		
					);
		request.get(url);
	}	
	
	//关闭message
	this.closeMessage = function() {
		alert("您的消息已经成功发送给对方");
		var div = window.parent.document.getElementById(this.messageId);
		div.parentNode.removeChild(div);
	}
	
	//给某人发短信
	this.sendSmsMessage = function(username) {
		if (this.isLogin("给人发短信是要登录的哦！"))
		 	window.open("http://message.wangyou.com/sms/?username=" + username, "", "width=418,height=350,scrollbars=no,toolbar=no");
	}
	
	//粉丝
	this.fans = function (thisObj, pid, username) {
		if (!this.isLogin("加入粉丝团是要登录的哦！"))
			return false;
		if (!pid) pid = 0;
		if (!username) username = "";
		var request = new Request(
					function()
					{
						if (request.xmlhttp.readyState == 4)
						{
							if (request.xmlhttp.responseText == "100") {
								(new ToolBar).showLoginForm("加入粉丝团是要登录的哦");
							}
							else {
								var divId = (new ToolBar).fansId;
								var div = document.getElementById(divId);
								if (!div)
								{
									div = document.createElement("DIV");
									div.id = divId;
									document.body.appendChild(div);
								}
								//alert(request.xmlhttp.responseText);
								div.innerHTML = request.xmlhttp.responseText;
								(new ToolBar).show(div, lib.x(thisObj) + 15, lib.y(thisObj)+15);
								
							}
						}
					}
				);
		request.get("/float/fans/index.php?pid=" + pid + "&username=" + username + "&r=" + Math.random());
	}
	
	//发送粉丝
	this.sendFans = function (thisObj, pid, username) {
		if (!this.isLogin("加入粉丝团是要登录的哦！"))
			return false;
		if (!pid) pid = 0;
		if (!username) username = "";
		var msg = thisObj.msg.value.trim();
		if (msg == "") {
			alert("不想对你的偶像说点什么吗？");
			thisObj.msg.value = "";
			thisObj.msg.focus();
			return false;
		}
		var request = new Request(
					function()
					{
						if (request.xmlhttp.readyState == 4)
						{
							var response = request.xmlhttp.responseText;
							switch (response)
							{
								case "100":
									(new ToolBar).showLoginForm("加入粉丝团是要登录的哦");
									break;
								case "1":
									alert("您不能加入自己的粉丝团");
									break;
								case "2":
									alert("您要加入的粉丝团尚未建立");
									break;
								case "3":
									alert("您已经加入了该粉丝团,不必重复加入");
									break;
								case "4":
									alert("请输入您的粉丝宣言");
									break;
								case "5":
									alert("恭喜您已经成功加入！");
									break;
								case "6":
									alert("您还没有拥有自己的地盘，不能使用此功能");
									break;
								default:
									alert(response);
							}
							(new ToolBar).close((new ToolBar).fansId);
						}
					}
				);


		request.get("/float/fans/index.php?q=send&pid=" + pid + "&username=" + username + "&msg=" + this.urlEncode(msg) + "&r=" + Math.random());
	}
	
	//显示小提示
	this.alert = function (thisObj) {
		return false;
		if (cookie.get("toolbar_closed") == "1") {
			return;
		}
		var request = new Request(
				function () {
					if (request.xmlhttp.readyState == 4) {
						var response = request.xmlhttp.responseText;
						var divId = (new ToolBar).alertId;
						var div = document.getElementById(divId);
						if (!div) {
							div = document.createElement("DIV");
							div.id = divId;
							document.body.appendChild(div);
						}
						div.innerHTML = response;
						(new ToolBar).alertPosition();
						(new ToolBar).alertShow(thisObj, 1);
					}
				}
			);
		request.get("/float/alert/index.php?" + Math.random());
		if (this.alertListener != null) {
			listener.remove(window, "scroll", this.alertListener);
			listener.remove(window, "resize", this.alertListener);
		}
		this.alertListener = function(){
			(new ToolBar).alertPosition(550);
			$ToolBar.broadCastPosition();
		};
		listener.add(window, "scroll", this.alertListener);
		listener.add(window, "resize", this.alertListener);
		listener.add(window, "load", function () {
			//新消息变色
			$ToolBar.newMessageBrickHandler = window.setInterval("changeNewMessageColor()", 400);
			
			//查看系统消息
			(new ToolBar()).broadCastLookup();
			//window.setInterval("(new ToolBar()).broadCastLookup()", 5000);
		});
	}
	
	
	//查询广播
	this.broadCastLookup = function () {
		return false;
		if ($ToolBar.chatClosed) {
			return;
		}
		var divId = $ToolBar.broadCastId;
		var div = document.getElementById(divId);
		if (div != null && div.style.display == "") {
			return;
		}
		var request = new Request(
			function () {
				if (request.xmlhttp.readyState == 4) {					
					var response = request.xmlhttp.responseText;
					if (response == "") {
						window.setTimeout("(new ToolBar()).broadCastLookup()", 15000);
						return;
					}
					
					if (!div) {
						div = document.createElement("DIV");
						div.id = divId;
						document.body.appendChild(div);
					}
					
					div.innerHTML = response;
					
					$ToolBar.broadCastPosition();
					
					window.setTimeout("(new ToolBar()).broadCastLookup()", 6000);
				}
			}
		);
		request.get("/float/broadcast/index.php?action=lookup&" + Math.random());
	};
	
	//广播定位
	var broadCastLoaded = false;
	this.broadCastPosition = function () {
		var divId = $ToolBar.broadCastId;
		var div = document.getElementById(divId);
		if (div == null) {
			return;
		}
		var body = lib.getBody();
		var x = 4;
		var bodyHeight = lib.intval(body.scrollTop) + lib.intval($ToolBar.getBodyHeight());
		var y = bodyHeight - 150;
		
		if (broadCastLoaded) {
			$ToolBar.show(div, x, y);
		}
		else {
			broadCastLoaded = true;
			this.fade(divId, x, bodyHeight, y); 
		}
	};

	this.fade = function (divId, x, y, min) {
		var div = document.getElementById(divId);
		$ToolBar.show(div, x, y);
		if (y > min) {
			window.setTimeout("$ToolBar.fade('" + divId + "'," + x + "," + (y - 10) + "," + min + ")", 150);
		}
	};

	
	//跳到聊天室
	this.chatClosed = false;
	this.goChat = function (username, id) {
		//if (typeof(id) != "undefined") {
			//删除消息
			//var request = new Request (function () {
			//	if (request.xmlhttp.readyState == 4) {
					//alert(request.xmlhttp.responseText);
			//	}
		//	});
		//	request.get("/float/chat/index.php?username=" + username + "&id=" + id);
	//	}
		
		
		//push用户到聊天室
		//var wyOpenWinId = cookie.get("wyClientOpen");
		//if(wyOpenWinId == null || wyOpenWinId == "0") {
		//	window.open("http://chat.wangyou.com/dingdong.html?user="+username, "newWyClient");
		//}
	//	else {
		//	if(username !=null && username != "") {
		//		if(location.href.indexOf("wangyou.com")>0) {
		//			var domain = "wangyou.com";
		//		}
		//		else {
		//			var domain = "192.168.0.42";
		//		}
		//		cookie.set("wyClientUser",username, 3600, "/",domain);
		//	}		
		//}
		
		//删除层
		//if (typeof(id) != "undefined") {
		//	this.close(this.broadCastId);
	//	}
		
		return false;
	};
	
	//拒绝聊天
	this.refuseChat = function (username, id) {
		//this.chatClosed = true;
		
		//var request = new Request (function () {
		//	if (request.xmlhttp.readyState == 4) {
				//alert(request.xmlhttp.responseText);
		//	}
		//});
		//request.get("/float/chat/index.php?username=" + username + "&id=" + id);
		
		//删除层
		//this.close(this.broadCastId);
	};
	
	//问别人要照片
	this.requestFace = function (username) {
		//判断有无请求过
		if(location.href.indexOf("wangyou.com")>0) {
			var domain = "wangyou.com";
		}
		else {
			var domain = "192.168.0.42";
		}
		var list = cookie.get("requestface");
		if (list == null) {
			cookie.set("requestface", username, 3600, "/", domain);
		}
		else {
			//判断里面有没有
			var lists = list.split("\,");
			for (var i=0; i<lists.length; i++) {
				if (lists[i] == username) {
					alert("要过就不要再要了吧！");
					return;
				}
			}
			lists.shift();
			lists.push(username);
			cookie.set("requestface", lists.join(","), 3600, "/", domain);
		}
		
		//发送请求
		var request = new Request (function () {
			if (request.xmlhttp.readyState == 4) {
				if (request.xmlhttp.responseText == "1") {
					alert("地主已经收到了您的请求！");
				}
			}
		});
		request.get("/float/face/index.php?myUser=" + username);
	};
	
	//提示框位置
	this.alertPosition = function (y) {
		if (!y) y = 550;
		var div = document.getElementById(this.alertId);
		if (typeof(div) == "object") {
			var body = lib.getBody();
			var x = 4;
			var y = lib.intval(body.scrollTop) + lib.intval(this.getBodyHeight()) - y;
			this.show(div, x, y);
		}
	}
	
	//提示框选项卡
	this.alertShow = function (thisObj, order) {
		var total = 3;
		for (var i=1; i<= total; i++)
		{
			if (i != order)
			{
				var $display = "none";
				var $img = "http://img2.wangyou.com/float/o_c.gif";
			}
			else
			{
				var $display = "block";
				var $img = "http://img2.wangyou.com/float/o.gif";
			}
			try {
				document.getElementById("float_alert_tip" + i).style.display = $display;
				document.getElementById("float_alert_img" + i).src = $img;
				//执行动作
				this.alertAction(document.getElementById("float_alert_tip" + i), order);
			} catch (e) {
				
			}		
		}
	}
	
	//执行动作
	this.alertAction = function(obj, order) {
		return false;
		var url;
		var r = Math.random();
		obj.innerHTML = "<p style='color:gray'>数据加载中...</p>";
		switch (order)
		{
			case 1:
				url = "/float/alert/index.php?q=visitWho&r=" + r;
				break;
			case 2:
				url = "/float/alert/index.php?q=whoVisitor&r=" + r;
				break;
			case 3:
				url = "/float/alert/index.php?q=message&r=" + r;
				break;
		}
		var request = new Request(
					function ()
					{
						if (request.xmlhttp.readyState == 4)
						{
							var response = request.xmlhttp.responseText;
							obj.innerHTML = response;
						}
					}
				);
		if (url != null)
		{
			request.get(url);
		}		
	}
	
	//最小化
	this.alertMinimize = function (thisObj) {
		document.getElementById("float_alert_min").style.display = "none";
		document.getElementById("float_alert_max").style.display = "";
		var div = document.getElementById(this.alertId);
		document.getElementById("float_alert_body").style.display = "none";
		if (typeof(div) == "object") {			
			this.alertPosition(55);
			if (this.alertListener != null) {
				listener.remove(window, "scroll", this.alertListener);
				listener.remove(window, "resize", this.alertListener);
			}
			this.alertListener = function(){(new ToolBar).alertPosition(55)};
			listener.add(window, "scroll", this.alertListener);
			listener.add(window, "resize", this.alertListener);
		}
	}
	
	//最大化
	this.alertMaximize = function (thisObj)
	{
		document.getElementById("float_alert_min").style.display = "";
		document.getElementById("float_alert_max").style.display = "none";
		document.getElementById("float_alert_body").style.display = "";
		this.alertPosition();
		this.alertShow(null, 1);
		if (this.alertListener != null) {
			listener.remove(window, "scroll", this.alertListener);
			listener.remove(window, "resize", this.alertListener);
		}
		this.alertListener = function(){(new ToolBar).alertPosition(550)};
		listener.add(window, "scroll", this.alertListener);
		listener.add(window, "resize", this.alertListener);
	}	

	//关闭
	this.alertClose = function () {
		if (window.confirm("亲爱的用户：\n若您选择“关闭”地盘小提示，24\n小时内小提示将不会在地盘内显示\n同意关闭请点确定，否则请点取消")) {
			document.body.removeChild(document.getElementById(this.alertId));
			cookie.set("toolbar_closed", 1, 3600 * 24, "/");
		}
	};
	
	//检查发送消息的表单
	this.checkMessageForm = function(theForm)
	{
		var subject = theForm.subject.value.trim();
		var body = theForm.body.value.trim();
		if (subject == "")
		{
			alert("请输入您要发送的主题");
			theForm.subject.value = "";
			theForm.subject.focus();
			return false;
		}
		if (body == "")
		{
			alert("请输入您要发送的内容");
			theForm.body.value = "";
			theForm.body.focus();
			return false;
		}
		return true;		
	}
	
	//网友广播
	
	
	this.show = function (div, x, y) {
		x = lib.intval(x);
		y = lib.intval(y);
		try {
			div.style.cssText = "z-index:9;margin:0px;padding:0px;position:absolute;left:" + x + "px" + ";top:" + y + "px";
		} catch (e) {
			
		}
	}
	
	this.close = function (divId)
	{
		var div = document.getElementById(divId);
		if (div)
		{
			div.parentNode.removeChild(div);
		}
	}
	
	this.isLogin = function(message) {
		var md = cookie.get("wangyoulogin");
		if (md == null || md.length != 32) {
			this.showLoginForm(message);
			return false;
		}
		return true;
	}
	
	this.showLoginForm = function (message) {
		QuickLoginForm.width = 216;
		QuickLoginForm.height = 106;
		QuickLoginForm.url = "/quick_login/index.php";
		QuickLoginForm.id = "message-login-form";
		QuickLoginForm.msg = message;
		QuickLoginForm.show();
	};
	
	this.getBodyWidth = function() {
	  	if (window.innerWidth)
   			return window.innerWidth;
		if (document.body&&document.body.clientWidth)
   			return document.body.clientWidth;
  		if (document.documentElement&&document.documentElement.clientWidth)
   			return document.documentElement.clientWidth;
  		
	};
	
	this.getBodyHeight = function() {		
   		var height1 = 100000, height2 = 100000, height3 = 100000;
   		if (document.documentElement && document.documentElement.clientHeight) 
   			height1 =  lib.intval(document.documentElement.clientHeight);
  		if (window.innerHeight)
   			height2 = lib.intval(window.innerHeight);
   		if (document.body && document.body.clientHeight)
   			height3 =lib.intval(document.body.clientHeight);
   		return Math.min(height1, height2, height3);
	};

	this.urlEncode = function (s) {
		s = s.replace(/%/g, "%25");
		s = s.replace(/&/g, "%26");
		s = s.replace(/\?/g, "%3F");		
		s = s.replace(/\//g, "%2F");
		s = s.replace(/:/g, "%3A");
		s = s.replace(/=/g, "%3D");
		s = s.replace(/\+/g, "%2B");
		s = s.replace(/\s+/g, "+");
		return s;
	};
}

$ToolBar = new ToolBar();


function changeNewMessageColor () {
	var obj = document.getElementById("new_message_num");
	if (obj == null) {
		window.clearInterval($ToolBar.newMessageBrickHandler);
		return;
	}
	var num = parseInt(obj.innerHTML.toString());
	if (num == 0) {
		window.clearInterval($ToolBar.newMessageBrickHandler);
		return;
	}
	if ($ToolBar.newMessageBrick == 0) {
		obj.className = "f_num";
		obj.style.fontWeight = "bold";
		$ToolBar.newMessageBrick = 1;
	}
	else {
		obj.className = "";
		obj.style.fontWeight = "bold";
		$ToolBar.newMessageBrick = 0;
	}
}