/**
 * 初始化这个msg.js,需要几个条件:
 * 		A. 需要在 head 标签中导入
 *			<link href="<%=basePath %>v3_view/common/css/msg.css" rel="stylesheet" type="text/css"/>
 *		    <script src="<%=basePath %>v3_view/common/js/mssage.js" type="text/javascript"></script>
 *			
 *		B. 需要一个变量
 *			var currentUserId = ?;
 * 			isMySpace();
 *			var isMyfriend = false; //是否是当前用户的朋友
 *		C. 使用说明:
 *			例1:		var message = new Message();
 *					message.showMsg(); 
 */
var Message = Class.create({
	//初始化入口
	initialize : function(containerId){
		this.containerId = (!!containerId || "personalTab");
		this.currentUserId = self.currentUserId;
		this.mySpace = isMySpace();
	},
	//发消息
	postMsg : function(formId,containerId){
		//首先检查是否允许留言
		if(!this.checkAllowMessage()) return;
		
		this.containerId = containerId;
		//
		if($F("lMsgContent") == "")
		{
			$("lMsgContent").focus();
			return -1;
		}
		 
		
		var currentUserId = $F("currentUserId");
		var url = constants.url() + "/user/postMsg.action";
		var waveIdv = $F("waveId");
		var params = Object.toQueryString({
			lMsgContent : $F("lMsgContent"),
			replyToId : $F("replyToId"),
			currentUserId : currentUserId,
			msgType : $F("msgType"),
			waveId : waveIdv
		});
		$("userReplyDescription").update("");
		$(containerId).update("loading...");
		
		
		var callback = function(request){
			//console.log(request.responseText);
			//console.log("containerId = " + containerId + ",this.containerId = " + this.containerId);
			$(this.containerId).update(request.responseText);
			var resp = request.responseText;
			resp = resp.unescapeHTML();
			if(resp == 'success')
			{
				$("lMsgContent").update('');
			}
			else
			{
			//alert(s)
			    //$("lMsgContent").value=tempContent;
				$("lMsgContent").update(lMsgContent);
				
			}
		
			$("replyToId").value = -1;
			$("waveId").value = -1;
			this.getMsg({msgType:$F("msgType"),waveId:waveIdv});
		}
		
		gCommonAsynRequest(url,'',params,callback.bindAsEventListener(this));
		
	},
	//显示消息列表
	showMsg : function(param){
		this.containerId = "personalTab";
		var url = constants.url() + "/user/showMsg.action";
		this.handleMsg(url,param);
	},
	//取出消息列表
	getMsg : function(param){
		var url = constants.url() + "/user/getMsg.action";
		this.handleMsg(url,param);
	},
	//处理消息
	handleMsg : function(url,param){
		$(this.containerId).update("loading...");
		
		var params = "currentUserId=" + this.currentUserId;
		if(param){
			params += "&" + Object.toQueryString(param);
		}
		var callback = function(response){
			if(param.waveId){
				$("waveId").value = param.waveId;
			}
			Message.msgType = (param.msgType || 1);
			if(!!Message.msgType && Message.msgType != 1){
				$("lMsgTitle").update("该语音文件的留言");
			}
			//console.log("0." + params.toString());
			//console.log("1." + Message.msgType);
			//console.log("2." + param.waveId);
			//console.log("3." + param.msgType);
			this.initializePaginate.apply(this,[Message.msgType,param.waveId]);
		}
		gCommonAsynRequest(url,this.containerId,params,callback.bindAsEventListener(this));
	},
	//删除留言
	deleteMsg : function(messageId){
		if(this.mySpace){
			
			var url = constants.url() + "/user/deleteMsg.action";
			var params = {messageId : messageId};
			var waveIdv = $F("waveId");
			//console.log(Object.toQueryString({msgType : (Message.msgType || 1),waveId : waveIdv}));
			this.containerId = "lShowMsgPanel";
			var callback = function(response){
				this.getMsg({msgType : (Message.msgType || 1),waveId : waveIdv});
			}
			gCommonAsynRequest(url,this.containerId,params,callback.bindAsEventListener(this));
			
		}
		return false;
	},
	//点击回复之后需要做的事情
	clickReplyButton : function(replyToId,myUsername,currentUsername){
		$("replyToId").value = replyToId;
		//得稍等片刻才能让对象获得焦点
		setTimeout(function(){
			$("lMsgContent").focus();
			$("userReplyDescription").update(myUsername + " 回复 " + currentUsername + ":");
		}, 300);
	},
	//初始化分页
	initializePaginate : function(){
		var array = $A(arguments);
		array.unshift(this);
		//console.log("totalpage = " + $F("msgTotalPage"));
		var msgPaginate = new VoxPaginate($F("msgTotalPage"), $F("msgCurrentPage"), 5,array);
		msgPaginate.addPageChangeCallback(function(totalPageCount, currentPageNumber, attachArray){
			attachArray[0].gotoWhichPage(currentPageNumber,attachArray);
		});
	},
	//分页的回调方法
	gotoWhichPage : function(currentPage,array){
		var params = {msgCurrentPage:currentPage,msgType:array[1],waveId:array[2]};
		array[0].getMsg(params);
	},
	//检查是否允许留言
	checkAllowMessage : function(){
		
		if(!this.mySpace && !isMyfriend){
			if(messagePrivacy == "仅好友" ){
				alert("对不起，对方不允许非好友留言。");
				return false;
			}else if(messagePrivacy == "任何人"){
				return true;
			}
		}else if(!this.mySpace && isMyfriend){
			return true;
		}
		
		return true;
	}
});


