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);
		var thisobj = getObj(this.appTab);
		var parent	= elementBind('div','','butt-app-current');
		addElement(thisobj,parent);
		var list	= elementBind('div','','app-menuList');
		list.innerHTML	= getObj('pwd_app_hidden').innerHTML;
		parent.appendChild(list);
	},
	
	showMessage : function () {
		this.tabChange(this.messageTab);
		var thisobj = getObj(this.messageTab);
		var parent	= elementBind('div','','message-app-current');
		addElement(thisobj,parent);
		var list;
		if (this.messageTem != null) {
			list = this.messageTem;
		} else {
			var handle = this;
			list = elementBind('div','','app-menuList');
			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','','h2A cc','cursor: pointer;');
					h2.innerHTML= '<span class="b fl">收件箱</span><span class="fr">-</span>';
					h2.onclick = function () {
						handle.hiddenTab();
					}
					list.appendChild(h2);
					
					var div	= elementBind('div','','listTable');
					list.appendChild(div);
					var ul = elementBind('ul','','p10');
					div.appendChild(ul);
					if (rText[1] != '') {
						//eval(rText[1]);
						var msgs = JSON.parse(rText[1]);
						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;
				} else {
					ajax.guide();
				}
			});
		}
		parent.appendChild(list);
	},
	
	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 (fromuid == 0){
			var action = 'readn';
		} else {
			var action = 'read';
		}
		var li	= elementBind('li');
		li.innerHTML = '<span class="fr">'+from+'</span><a href="message.php?action='+action+'&mid='+mid+'" target="_blank">'+char_cv(title)+'</a>';
		element.appendChild(li);
	},
	
	showFriend	: function () {
		this.tabChange(this.friendTab);
		var thisobj = getObj(this.friendTab);
		var parent	= elementBind('div','','friend-app-current');
		addElement(thisobj,parent);

		var list;
		if (this.friendTemp != null) {
			list = this.friendTemp;
		} else {
			var handle = this;
			list = elementBind('div','','app-menuList');
			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','','h2A cc','cursor: pointer;');
					h2.innerHTML= '<span class="b fl">在線好友</span><span class="fr">-</span>';
					h2.onclick = function () {
						handle.hiddenTab();
					}
					list.appendChild(h2);
					var div	= elementBind('div','','u-postlist-s gray1');
					list.appendChild(div);
					var ul = elementBind('ul');
					div.appendChild(ul);
					if (rText[1] != '') {
						var friends = JSON.parse(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 = '沒有好友在線上';
						ul.appendChild(li);
					}
					handle.friendTemp = list;
				} else {
					ajax.guide();
				}
			});
		}
		parent.appendChild(list);
	},
	
	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 upparent = parent.parentNode;
			upparent.replaceChild(thistab,parent);
			this.currentTab = null;
		}
	},
	
	tabChange	: function (id) {
		this.hiddenTab();
		this.currentTab = id;
		var handle = this;
		getObj(id).onclick = function(){
			handle.hiddenTab();
		};
	}
}