uid=0;

function comment_show(data)
{
	var commentshow = "";
	var tshow = "";
	for (i=0;i<data.length ;i++ )
	{
		tshow = ' <dl><dt><!--<em>1</em>--><span>'+data[i].dtime+'</span><br /><strong><a href="/en/u/author.php?uid='+data[i].uid+'">'+data[i].nicker+'</a></strong> '+g_message['comment']['post']+'：</dt><dd><span><img src="'+data[i].photo+'" width="28" height="28" onerror="show_img(this,\'small_userphoto\');"/> </span><p>'+data[i].comment+'<br /></p></dd>           </dl>'; 
		commentshow += tshow;
		
	}	
	jQuery("#commentshow").html(commentshow);
}


function comment_rank(data)
{

	var rankshow = "";
	var tshow = "";
	for (i=0;i<data.length ;i++ )
	{
		tshow = '<li><a href="/en/b/'+data[i].book_id+'.html">'+data[i].title.substring(0,18)+'</a></li>';
		rankshow += tshow;
		
	}	
	jQuery("#rand_content").html(rankshow);
}

jQuery.noConflict();
(function($) {
	$.Comment = {
		setup : function ()
		{
			$.getJSON("/interface/ustate.php?action=check", function(json){
				if (json.result=='1')
				{
					$("#cnicker").css("display","");
					$("#cnicker").html(g_message['common']['hello'] + "：" + json.nicker);
					$("#cusername").css("display","none");
					//$("#cpassword").css("display","none");
					$("#cregister").css("display","none");
					$("#nav_ustate").html(g_message['common']['hello'] + "：" + json.nicker + "&nbsp;<a href='/en/logout.php'>"+g_message['common']['signout']+"</a>");
					uid=json.uid;
					return;
				}
				else
				{
					return;
				}
			});
		},

		
		comment : function ()
		{
			var book_id = $("#comm_book_id").val();
			var comment = $("#comment").val();
			var anonymous = $("#anonymous").attr("checked");
			comment = $.trim(comment);
			if (book_id == "")
			{
				alert("invalid book！");
				return false;
			}
			if (uid == 0 && book_id!='55091170' && anonymous != true)
			{
				var user = $("#comm_user").val();
				var pass = $("#comm_pass").val();
				user = $.trim(user);
				pass = $.trim(pass);
				if (user == "" || user == "Email")
				{
					alert(g_message['comment']['want_username']);
					$("#comm_user").focus();
					return false;
				}
				if (pass == "" || pass == "Password")
				{
					alert(g_message['comment']['want_password']);
					$("#comm_pass").focus();
					return false;
				}
			}
			if (comment == "")
			{
				
				if (book_id=='55091170')
				{
					alert(g_message['feedback']['want_comment']);
				}else
				{
					alert(g_message['comment']['want_comment']);
				}
				$("#comment").focus();
				return false;
			}
			
			$.post(
				"/interface/comment.php",
				{
					"username":user,
					"password":pass,
					"comment":comment,
					"book_id":book_id,
					"anonymous":anonymous,
					"action":'post'
				},
				function(response){
					if (response.result=='1')
					{
						if (uid==0 && response.uid != '0')
						{
							$("#cnicker").css("display","");
							$("#cnicker").html(response.nicker);
							$("#cusername").css("display","none");
							//$("#cpassword").css("display","none");
							$("#cregister").css("display","none");
							$("#nav_ustate").html(g_message['common']['hello'] + "," + response.nicker);
							uid=response.uid;
						}
						$("#comment").val("");
						if (book_id=='55091170')
						{
							$("#feedbackstatus").html(g_message['feedback']['publish_succ']);
						}else
						{
							alert(g_message['comment']['publish_succ']);
						}
						return true;
					}
					else
					{
						alert(response.msg);
						return false;
					}
					
				},
				"json"
			); 
		
			return false;
		},

		feedback : function ()
		{
			var book_id = $("#feedback_book_id").val();
			var comment = $("#fd_comment").val();
			comment = $.trim(comment);
			if (book_id == "")
			{
				book_id = 55091170;
			}
			if (uid == 0)
			{
				var user = "";
				var pass = "";
			}
			if (comment == "")
			{
				
				alert(g_message['feedback']['want_comment']);
				$("#fd_comment").focus();
				return false;
			}
			comment = $("#feedback_type").val() + "：" + comment + "<br>" + window.location.href;
			
			$.post(
				"/interface/comment.php",
				{
					"username":user,
					"password":pass,
					"comment":comment,
					"book_id":book_id,
					"action":'post'
				},
				function(response){
					if (response.result=='1')
					{
						$("#fd_comment").val("");
						$("#feedbackstatus").html(g_message['feedback']['publish_succ']);
						return true;
					}
					else
					{
						alert(response.msg);
						return false;
					}
					
				},
				"json"
			); 
		
			return false;
		},

		
		
		//显示留言函数
		comment_show : function (book_id, num)
		{
			var commentshow = "";

			$.getJSON("/interface/comment.php?callback=comment_show&action=list&num="+num+"&book_id="+book_id, function(json){
				return;			
			});
		},

		comment_rank: function(dwhere, num)
		{
			switch (dwhere)
			{
				case 'day':
					$("#dayrank").attr("class", "tab2");
					$("#weekrank").attr("class", "tab1");
					$("#monthrank").attr("class", "tab1");
					break;
				case 'week':
					$("#dayrank").attr("class", "tab1");
					$("#weekrank").attr("class", "tab2");
					$("#monthrank").attr("class", "tab1");
					break;
				default:
					$("#dayrank").attr("class", "tab1");
					$("#weekrank").attr("class", "tab1");
					$("#monthrank").attr("class", "tab2");
					break;
			
			}
			$.getJSON("/interface/comment.php?callback=comment_rank&action=rank&num="+num+"&dwhere="+dwhere, function(json){
				return;			
			});
		}
	}
})(jQuery);
