var app_bottom = function (app,friend,message,postlist,forumlist) {
	this.currentTab = null;

	this.appTab		= null;
	this.friendTab	= null;
	this.messageTab	= null;
	this.postlistTab= null;
	this.forumlistTab=null;

	this.friendTem	= null;
	this.messageTem = null;

	this.init(app,friend,message,postlist,forumlist);
}

app_bottom.prototype = {
	init 		: function (app,friend,message,postlist,forumlist) {
		this.appTab		= app;
		this.friendTab	= friend;
		this.messageTab	= message;
		this.postlistTab= postlist;
		this.forumlistTab= forumlist;
		var handle = this;
		getObj(app).onclick = function () {
			handle.showAPP();
		};
		getObj(friend).onclick = function () {
			handle.showFriend();
		};
		getObj(message).onclick = function () {
			handle.showMessage();
		};
		getObj(postlist).onclick = function () {
			handle.showPostlist(this);
		};
		getObj(forumlist).onclick = function () {
			handle.showForumlist(this);
		};
	},

	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 = '';
		}
	},

	showForumlist : function (o) {
		this.tabChange(this.forumlistTab);
		this.clickArttribute();
		var thisobj = getObj(this.forumlistTab);
		var parent	= thisobj.parentNode;
		this.changeFirstNodeDisplay(parent,'span');
		pwForumList(false,false,null,o);
		var list	= elementBind('div','pwd_forumlist_hidden','menu menu-thread-bottom','');
		list.innerHTML	= getObj('menu_forumlist').innerHTML;
		document.body.insertBefore(list,document.body.firstChild);
	},

	showPostlist : function (o) {
		this.tabChange(this.postlistTab);
		this.clickArttribute();
		var thisobj = getObj(this.postlistTab);
		var parent	= thisobj.parentNode;
		this.changeFirstNodeDisplay(parent,'span');
		pwForumList(false,true,null,o);
		var list	= elementBind('div','pwd_postlist_hidden','menu menu-post-bottom','');
		list.innerHTML	= getObj('menu_forumlist').innerHTML;
		document.body.insertBefore(list,document.body.firstChild);
	},

	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');
					if (rText[1] != '') {
						var msgs = JSONParse(rText[1]);
						if (msgs.length > 5) {
							ul.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);
					}
					div.appendChild(ul);
					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();
				}
			} else if (this.currentTab == this.postlistTab) {
				thistab.onclick = function () {
					handle.showPostlist(this);
				}
			} else if (this.currentTab == this.forumlistTab) {
				thistab.onclick = function () {
					handle.showForumlist(this);
				}
			}
			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;
		}

		if (IsElement('pwd_postlist_hidden')) {
			return getObj('pwd_postlist_hidden');
		}
		if (IsElement('pwd_forumlist_hidden')) {
			return getObj('pwd_forumlist_hidden');
		}
		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 = '<div class="p10"><img src="'+imgpath+'/loading.gif" align="absmiddle" /> 正在加載數據...</div>';
		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 || o == getObj('pwd_postlist_hidden') || o == getObj('pwd_forumlist_hidden')) {
					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';
			getObj('pwb_message').className = 'app-m fl';
		}
		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=/";
			}
		}
	},

	delShortCut : function(fid){
		ajax.send('pw_ajax.php?action=shortcut&fid='+fid,'',function(){
				var rText = ajax.request.responseText.split('\t');
				if (typeof(rText[1]) != 'undefined' && rText[1] == 'successno'){
					var oShortcut = getObj('shortcut_'+fid);
					oShortcut.parentNode.removeChild(oShortcut);
					var pwd_app = getObj('pwd_app_hidden');
					var lis = document.getElementsByTagName('li');
					for (var i = 0; i < lis.length; i++) {
						if (lis[i].id == 'shortcut_'+fid) {
							lis[i].parentNode.removeChild(lis[i]);
						}
					}
					showDialog('success','刪除書簽成功！');
				} else {
					showDialog('error','刪除書簽出錯！');
				}
			}
		);
	}
}