var app_bottom = function (app,friend,message) {
	this.currentTab = null;
	
	this.appTab		= null;
	this.friendTab	= null;
	this.messageTab	= null;
	
	this.friendTem	= null;
	this.messageTem = null;
	
	this.init(app,friend,message);
}

app_bottom.prototype = {
	init 		: function (app,friend,message) {
		this.appTab		= app;
		this.friendTab	= friend;
		this.messageTab	= message;
		var handle = this;
		getObj(app).onclick = function () {
			handle.showAPP();
		};
		getObj(friend).onclick = function () {
			handle.showFriend();
		};
		getObj(message).onclick = function () {
			handle.showMessage();
		};
	},
	
	showAPP		: function () {
		this.tabChange(this.appTab);
		this.clickArttribute();
		var thisobj = getObj(this.appTab);
		var parent	= thisobj.parentNode;
		this.changeFirstNodeDisplay(parent,'span');
		var list	= elementBind('div','','startbar-menuleft');
		list.innerHTML	= getObj('pwd_app_hidden').innerHTML;
		parent.appendChild(list);
	},
	
	changeFirstNodeDisplay	: function (parent,tag) {
		var firstTag	= this.gerFirstTagNode(parent,tag);
		if (typeof(firstTag)=='object') {
			firstTag.style.display = '';
		}
	},
	
	showMessage : function () {
		this.tabChange(this.messageTab);
		this.clickArttribute();
		var thisobj = getObj(this.messageTab);
		var parent	= thisobj.parentNode;
		this.changeFirstNodeDisplay(parent,'span');
		var list;
		if (this.messageTem != null) {
			list = this.messageTem;
			parent.appendChild(list);
		} else {
			this.loadingView(parent);
			var handle = this;
			list = elementBind('div','','startbar-menuright');
			setTimeout(function(){ajax.send('pw_ajax.php?action=pwb_message','u='+winduid,function(){
				var rText = ajax.request.responseText.split('\t');
				if (rText[0] == 'success') {
					var h2 = elementBind('h2','','h cc','cursor: pointer;');
					h2.innerHTML= '<span class="b fl"><a href="message.php?type=newmsg">收件箱</a></span><span class="fr closeicon"><img src="images/wind/index/cate_open.gif" /></span>';
					h2.onclick = function () {
						handle.hiddenTab();
					}
					list.appendChild(h2);
					
					var div	= elementBind('div','','listTable');
					list.appendChild(div);
					var ul = elementBind('ul','newmessagebox','p10');
					div.appendChild(ul);
					if (rText[1] != '') {
						var msgs = JSONParse(rText[1]);
						if (msgs.length > 5) {
							getObj('newmessagebox').className = 'mes-height';
						}
						if (typeof(msgs)=='object') {
							for (var i in msgs) {
								if (typeof(msgs[i])=='object') {
									handle.creatMess(msgs[i],ul);
								}
							}
						}
					} else {
						var li = elementBind('li');
						li.innerHTML = '<a href="message.php" target="_blank">您没有未读消息</a>';
						ul.appendChild(li);
					}
					handle.messageTem = list;
					delElement('load_temp');
					parent.appendChild(list);
				} else {
					ajax.guide();
				}
			});},300);
		}
	},
	
	creatMess	: function (obj,element) {
		if (typeof(obj)!='object') {
			return false;
		}
		var mid = obj['mid'];
		var title	= char_cv(obj['title']);
		var from	= char_cv(obj['from']);
		var fromuid	= char_cv(obj['fromuid']);
		if (obj['type'] == 'public') {
			var messagetype = 'public';
		} else if (obj['type'] == 'rebox' && fromuid == 0){
			var messagetype = 'system';
		} else {
			var messagetype = 'personal';
		}
		var li	= elementBind('li','msg_'+mid);
		li.innerHTML = '<span class="fr"><a href="u.php?uid='+fromuid+'">'+from+'</a></span><a style="cursor:pointer;" onclick="pw_bottom.readMessage(\''+messagetype+'\',\''+mid+'\')">'+char_cv(title)+'</a>';
		element.appendChild(li);
	},
	
	showFriend	: function () {
		this.tabChange(this.friendTab);
		this.clickArttribute();
		var thisobj = getObj(this.friendTab);
		var parent	= thisobj.parentNode;
		this.changeFirstNodeDisplay(parent,'span');
		this.loadingView(parent);
		var list;
		if (this.friendTemp != null) {
			list = this.friendTemp;
			parent.appendChild(list);
		} else {
			this.loadingView(parent);
			var handle = this;
			list = elementBind('div','','startbar-menuright');
			setTimeout(function(){ajax.send('pw_ajax.php?action=pwb_friend','u='+winduid,function(){
				var rText = ajax.request.responseText.split('\t');
				if (rText[0] == 'success') {
					var h2 = elementBind('h2','','h cc','cursor: pointer;');
					h2.innerHTML= '<span class="b fl">在线好友</span><span class="fr closeicon"><img src="images/wind/index/cate_open.gif" /></span>';
					h2.onclick = function () {
						handle.hiddenTab();
					}
					list.appendChild(h2);
					var div	= elementBind('div','','u-postlist-s gray');
					list.appendChild(div);
					var ul = elementBind('ul');
					div.appendChild(ul);
					if (rText[1] != '') {
						var friends = JSONParse(rText[1]);
						if (typeof(friends)=='object') {
							for (var i in friends) {
								if (typeof(friends[i])=='object') {
									handle.creatFriend(friends[i],ul);
								}
							}
						}
					} else {
						var li = elementBind('li');
						li.innerHTML = '<div class="p10">没有好友在线上 / <a href="mode.php?m=o&q=friend">查看所有好友</a></div>';
						ul.appendChild(li);
					}
					handle.friendTemp = list;
					delElement('load_temp');
					parent.appendChild(list);
				} else {
					ajax.guide();
				}
			});},300);
			
		}
		delElement('load_temp');
	},
	
	creatFriend	: function (obj,element) {
		if (typeof(obj)!='object') {
			return false;
		}
		var uid = obj['uid'];
		var face	= obj['face'];
		var username= obj['username'];
		var li = elementBind('li');
		li.innerHTML = '<a class="fr pwb_mess" onclick="sendmsg(\'pw_ajax.php?action=msg&touid='+uid+'\')">发消息</a><a href="mode.php?m=o&q=user&u='+uid+'"><img src="'+char_cv(face)+'" class="portrait-s"/></a>&nbsp;<a href="mode.php?m=o&q=user&u='+uid+'">'+char_cv(username)+'</a>';
		element.appendChild(li);
	},
	
	hiddenTab	: function () {
		if (this.currentTab != null) {
			var handle = this;
			thistab = getObj(this.currentTab);
			if (this.currentTab == this.appTab) {
				thistab.onclick = function () {
					handle.showAPP();
				}
			} else if (this.currentTab == this.friendTab) {
				thistab.onclick = function () {
					handle.showFriend();
				}
			} else if (this.currentTab == this.messageTab) {
				thistab.onclick = function () {
					handle.showMessage();
				}
			}
			var parent = thistab.parentNode;
			var firstTag	= this.gerFirstTagNode(parent,'span');
			if (typeof(firstTag)=='object') {
				firstTag.style.display = 'none';
			}
			var container = this.getContainer(parent);
			if (typeof(container) == 'object') {
				delElement(container);
			}
			this.currentTab = null;
			this.resetClick();
		}
	},
	
	getContainer	: function (parent) {
		parent = objCheck(parent);
		var leftContainer = this.getElementByClassName(parent,'startbar-menuleft');
		if (typeof(leftContainer) == 'object') {
			return leftContainer;
		}
		var rightContainer	= this.getElementByClassName(parent,'startbar-menuright');
		if (typeof(rightContainer) == 'object') {
			return rightContainer;
		}
		return false;
	},
	getElementByClassName	: function (obj,name) {
		obj	= objCheck(obj);
		var elems = document.getElementsByTagName('div');
		for ( var i = 0; i<elems.length; i++ ) {
			if ( elems[i].className == name ){
				return elems[i];
			}
		}
		return false;
	},
	gerFirstTagNode	: function (obj,tag) {
		obj = objCheck(obj);
		var elems = obj.getElementsByTagName(tag);
		if (elems.length>0) {
			return elems[0]; 
		}
		return false;
	},
	loadingView	: function (parent) {
		var temp	= elementBind('div','load_temp','startbar-menuright popout');
		temp.innerHTML = '<img src="'+imgpath+'/loading.gif" align="absmiddle" /> 正在加载数据...';
		parent.appendChild(temp);
	},
	
	tabChange	: function (id) {
		this.hiddenTab();
		this.currentTab = id;
		var handle = this;
		getObj(id).onclick = function(){
			handle.hiddenTab();
		};
	},
	
	clickArttribute	: function () {
		if (this.currentTab == null) {
			return false;
		}
		var handle	= this;
		var thisparent = getObj(this.currentTab).parentNode;

		document.onclick = function (e) {
			var o = is_ie ? window.event.srcElement : e.target;
			while (o) {
				if (o.tagName && o.tagName.toLocaleLowerCase() == 'body') {
					break;
				}
				if (o == thisparent) {
					return true;
				}
				o = o.parentNode;
			}
			handle.hiddenTab();
		}
	},
	resetClick	: function () {
		document.onclick	= '';
	},
	
	readMessage : function (messagetype,mid) {
		var msgid = 'msg_'+mid;
		var removeObj = getObj(msgid);
		removeObj.parentNode.removeChild(removeObj);
		if(getObj('newmessagebox').childNodes.length == 0){
			getObj('newmessagebox').innerHTML = '<li><a href="message.php" target="_blank">您没有未读消息</a></li>';
			getObj('td_msg').className = 'icon-meg';
		}
		var msgnum = parseInt(getObj('msgnum').innerHTML);
		getObj('msgnum').innerHTML = msgnum - 1;
		getObj('msgnum_show').innerHTML = msgnum - 1;
		sendmsg('pw_ajax.php?action=readmessage&type='+messagetype+'&mid='+mid);
	},
	
	showToolBar : function(type){
		if (IsElement('startbar')) {
			thistoolbox = getObj('startbar');
			var date = new Date();
			if (type == 'close') {
				thistoolbox.style.display = 'none';
				getObj('startbar-close').style.display='none';
				getObj('startbar-open').style.display='';
				date.setTime(date.getTime()+86400000);
				document.cookie="toolbarhide=1;expires=" + date.toGMTString() + " path=/";
			} else if(type == 'open') {
				thistoolbox.style.display = '';
				getObj('startbar-close').style.display='';
				getObj('startbar-open').style.display='none';
				document.cookie="toolbarhide=1;expires=" + date.toGMTString() + " path=/";
			}
		}
	}
}