var ProjectComment = {};


ProjectComment.loginTimer = 0;
ProjectComment.isLogin = 1;
ProjectComment.notLogin = 1;
ProjectComment.projectid =null;

ProjectComment.init = function(){
	
	ProjectComment.ShowFormDiv = _gObj('show_say');
	ProjectComment.replayForm = _gObj('replay_div');
	ProjectComment.commentDisplayDiv = _gObj('comment_display');
	ProjectComment.disPlayComment('?p=GetComment&projectid='+gProjectId);
	ProjectComment.projectid = gProjectId;
	ProjectComment.CheckUserLogin();
}

ProjectComment.CheckUserLogin = function(){
	
	if(ProjectComment.loginTimer) clearTimeout(ProjectComment.loginTimer);
	ProjectComment.loginTimer = setTimeout(function (){ProjectComment.CheckUserLogin();}, 1000);
	if (ProjectComment.isLoginFun() == false) {
		if(ProjectComment.notLogin) {
			ProjectComment.ShowLoginFrom(1);
			ProjectComment.notLogin = 0;
			ProjectComment.isLogin = 1;
		}
		return false;
	} else if(ProjectComment.isLogin) {
		ProjectComment.isLogin = 0;
		ProjectComment.notLogin = 1;
		ProjectComment.ShowSayForm();
	}
	//alert(ProjectComment.isLogin+","+ProjectComment.notLogin);
	
	return true;
};

ProjectComment.isLoginFun =function(){
	var ck = getCookie('3SEYU_SESS[main]');
	//alert(ck);
	if(ck == null || ck == "") return false;
	else return true;
};

ProjectComment.ShowSayForm = function (){
	
	//ProjectComment.ShowFormDiv = "";
	var album = new InstQuery("/template/share/project.detail.say.html?"+ getTimeStamp(), null, ProjectComment.onShowSayForm);
	album.execute();
};
ProjectComment.onShowSayForm = function(oParams, oReq){
	//alert(oReq.responseText);
	ProjectComment.ShowFormDiv.innerHTML=oReq.responseText;
	//初始化评论输入框
    new_say = new HtmlEditor('new_say','myeditor','faceDiv');
    new_say.setFaceDiv();
	var f = $("#ProjectComment-Say-form");
	f.onsubmit = function (){
	    f.comment.value = new_say.getHtml();
		if(f.comment.value == "")
		{
			alert("评论不能为空,或有非法字符");
			f.comment.focus();
			return false;
		}
		ProjectComment.SubmitComment(f);
		return true;
	}
	
};



ProjectComment.ShowLoginFrom = function(){
	var lghtml = new InstQuery("/template/share/project.detail.login.html?"+ getTimeStamp(), null, ProjectComment.onShowLoginFrom);
	lghtml.execute();
};

ProjectComment.onShowLoginFrom = function(oParams, oReq){
	ProjectComment.ShowFormDiv.innerHTML=oReq.responseText;
	var f = $("#ProjectComment-login-form");
	var v = new Validator(f);
	v.requireNames([
		"email",
		"password"
		,"vcode"
	]);
	v.setRules({
		email: {format: "email", message: "请输入正确格式的邮件地址"},
		password: {format: "pass6-20", message: "密码请输入6-20位非空字符"}
		,vcode: {format: "digit4", message: "验证码请输入4位数字"}
	});
	v.init();
};



ProjectComment.doLogin = function () {
	var f = _gObj("ProjectComment-login-form");
	
	var params = {
		vcode_type: f.vcode_type.value,
		email:f.email.value,
		password:f.password.value,
		vcode:f.vcode.value
	};
	var is = new InstSubmit("/ajax.login.php", params, null);
	is.resComplete = function(oReq){
		if(oReq.responseText == ""){
			this.onFailure(oReq);
		}
		else{
			//window.alert(oReq.responseText);
			//callbackCheckCoupon(oReq.responseText, bSubmit);
			switch(oReq.responseText)
			{
				case 'params_missed' : alert("参数丢失,请确认参数已经输入正确");break;
				case 'vcode_incorrect' : alert("验证码错误,请重新输入");break;
				case 'login_failed' : alert("登录失败");break;
				case 'email_not_existed' : alert("邮件地址错误");break;
				case 'password_incorrect' : alert("密码错误");break;
				case 'USERS_FORBIDDEN' : alert("用户已经被屏蔽");break;
				case 'USERS_ACTIVATE' : alert("您尚处于待认证状态，请查收邮件并进行邮箱认证");break;
				case 'unfine'	:alert("错误不明.请和客服人员联系");break;
				default : 	//gUserId = parseInt(oReq.responseText);
							break;
			}
			
		}
	};
	is.onLoading = function(oReq){
	};
	is.execute();

};

ProjectComment.SubmitComment = function (obj){
	var params = {
		projectid: ProjectComment.projectid ,
		comment:obj.comment.value
	};
	obj.comment.value = "";
	new_say.clearHtml();
	
	var is = new InstSubmit("?p=DoComment", params, null);
	is.resComplete = function(oReq){
		if(oReq.responseText == ""){
			this.onFailure(oReq);
		}
		else{
			//window.alert(oReq.responseText);
			switch(oReq.responseText)
			{
				case 'params_missed' 	: alert("参数丢失,请确认参数已经输入正确");break;
				case 'login_failed' 	: alert("登录失败");break;
				case 'unfine'			: alert("错误不明.请和客服人员联系");break;
				case 'not_project'		: alert('没有查询到相应的印品');break;
				case 'not_share'		: alert('此印品没有公开');break;
				default : 
							ProjectComment.disPlayComment('?p=GetComment&projectid='+gProjectId);
						 	ProjectComment.setCommentsCount(1);	
							break;
			}
			
		}
	};
	is.onLoading = function(oReq){
	};
	is.execute();

};

ProjectComment.ReComment = function(pcmid){
	
	if(ProjectComment.isLoginFun() == false)
	{
		alert('您还没有登陆呢,请先登陆后再回复');
		if(_gObj('login_email'))
		{
			_gObj('login_email').focus();
		}
		return false;
	}
	var replay = document.getElementsByName('replay[]');
	for(var i =0 ;i<replay.length;i++)
	{
		if(replay[i].value == pcmid)
		{
			if(_gObj('comment_'+pcmid).innerHTML !="")
			{	
				//alert('ok');
				_gObj('comment_'+pcmid).innerHTML='';
				_gObj('comment_'+pcmid).style.display = 'none';
				if(reply_say !=null)reply_say.clearFaceDiv();
				reply_say = null;
			}else
			{
		          var lghtml = new InstQuery('/template/share/comment.replay.html', null,ProjectComment.onLondReplayForm);
		          lghtml.setParams(pcmid);
		          lghtml.execute();
			}
		}else
		{
			//alert(replay[i].value);
			_gObj('comment_'+replay[i].value).innerHTML ='';
			_gObj('comment_'+replay[i].value).style.display = 'none';
			if(reply_say !=null)reply_say.clearFaceDiv();
			reply_say = null;
		}
	}
	
	return false;
		
};
ProjectComment.onLondReplayForm = function(pcmid, oReq)
{
    _gObj('comment_'+pcmid).style.display = 'block';
    _gObj('comment_'+pcmid).innerHTML=oReq.responseText;
    reply_say = new HtmlEditor('reply_say','reply_inputer','face_div_reply');
    if(reply_say !=null)
    {
        reply_say.setFaceDiv();
        reply_say.SetSayFocus();
    }
     
};


ProjectComment.doReComment = function(obj){
	//alert(obj.recomment.value);
	if(ProjectComment.isLoginFun() == false)
	{
		alert('您还没有登陆呢,请先登陆后再回复');
		return false;
	}
	obj.recomment.value = reply_say.getHtml();
	if(obj.recomment.value == "")
	{
		alert("回复不能为空");
		obj.recomment.focus();
		return false;
	}
	var parant = obj.parentNode;
	var params = {
		projectid: ProjectComment.projectid ,
		pcmid : parant.getAttribute('pcmid'),
		comment:obj.recomment.value
	};
	
	obj.recomment.value ="";
	reply_say.clearHtml();
	if(reply_say !=null)reply_say.clearFaceDiv();
	reply_say = null;
	var is = new InstSubmit("?p=DoReComment", params, null);
	is.resComplete = function(oReq){
		if(oReq.responseText == ""){
			this.onFailure(oReq);
		}
		else{
			//window.alert(oReq.responseText);
			switch(oReq.responseText)
			{
				case 'params_missed' 	: alert("参数丢失,请确认参数已经输入正确");break;
				case 'login_failed' 	: alert("登录失败");break;
				case 'unfine'			: alert("错误不明.请和客服人员联系");break;
				case 'not_project'		: alert('没有查询到相应的印品');break;
				case 'not_share'		: alert('此印品没有公开');break;
				case '-1'				: alert("此评论已经被删除");
				default : ProjectComment.disPlayComment('?p=GetComment&projectid='+gProjectId+"&pageno="+_gObj('curr_page_no').value);	
						  if(oReq.responseText != '-1')ProjectComment.setCommentsCount(1);
						  break;
			}
			
		}
	};
	is.onLoading = function(oReq){
	};
	is.execute();
	return false;
};

ProjectComment.disPlayComment = function(url)
{
	var lghtml = new InstQuery(url, null, ProjectComment.ondisPlayComment);
	lghtml.execute();
};

ProjectComment.ondisPlayComment = function(oParams, oReq){
	ProjectComment.commentDisplayDiv.innerHTML=oReq.responseText;
};
ProjectComment.getPager = function (uri,pageNo)
{
	var url = uri+"="+pageNo;
	//alert(url);
	ProjectComment.disPlayComment(url);
};

ProjectComment.doDelete = function (pcmid){
	if(confirm('真的要删除吗?'))
	{
		var params = {
			projectid: ProjectComment.projectid ,
			'pcmid' : pcmid
		};
		
		var is = new InstSubmit("?p=DoDelComment", params, null);
		is.resComplete = function(oReq){
			if(oReq.responseText == ""){
				this.onFailure(oReq);
			}
			else{
				//window.alert(oReq.responseText);
				switch(oReq.responseText)
				{
					case 'params_missed' : alert("参数丢失,请确认参数已经输入正确");break;
					case 'login_failed' : alert("登录失败");break;
					case 'unfine'	:alert("错误不明.请和客服人员联系");break;
					case 'not_project'	: alert('没有查询到相应的印品');break;
					case 'not_project_user' : alert('您没有权限删除此评论');break;
					case 'del_error'		: alert('删除失败,请稍候再试!');break;
					default : 
								ProjectComment.disPlayComment('?p=GetComment&projectid='+gProjectId+"&pageno="+_gObj('curr_page_no').value);
								ProjectComment.setCommentsCount(-(parseInt(oReq.responseText,10)));
							break;
				}
				
			}
		};
		is.onLoading = function(oReq){
		};
		is.execute();
		return false;
	}
	return false;
};

ProjectComment.setCommentsCount = function (count){
	_gObj('commments_count').innerHTML= parseInt(_gObj('commments_count').innerHTML,10) + count;
};


