<div class="bor-9fc">
	<div class="mode-head">
	    <span class="comment-count" style="float:right"><a href="{APP_URL}{url('comment/comment/index', 'contentid=' . $contentid)}" title="点击查看全部评论">已有<em class="comments">0</em>人参与</a></span>
		<h2 class="cor-c00">网友评论</h2>
	</div>
	<div class="padd-20 padd-b-0 ie6">
		<!-- 注意： 这里只显示5条评论 	 -->
		<dl class="mode-comment" id="commentWrapper">
			
		</dl>
		<div class="view-comment" style="display:none;"><a href="{APP_URL}{url('comment/comment/index', 'contentid=' . $contentid)}" title="点击查看全部评论">查看全部评论</a></div>
	</div>
	<div class="post-comment-area padd-lr-20">
		<form id="commentForm" action="{APP_URL}?app=comment&controller=comment&action=add" method="post">
			<input name="contentid" type="hidden" value="{$contentid}"/>
			<div class="hr-h10"></div>
			<textarea name="content"  rows="10" cols="58" class="textarea textarea-w600"></textarea>
			<div class="user-writeinfo">
				<span class="f-r"><input type="submit" value="发表评论" class="post-btn" /></span>
				<span class="f-l">用户名： <b id="nickname"></b></span>
				<span class="f-l mar-l-10 padd-3" id="anonymous"><label><input type="checkbox"  name="anonymous" value="1" title="选中后，你将以匿名方式发布留言，不会显示你的用户名"/> 匿名发表</label></span>
				<span class="f-l mar-l-10 padd-3" id="quickLogin"><a href="javascript:;">快速登录</a></span>
				<span class="f-l mar-l-10 padd-3" id="quickLogout" style="display:none;"><a href="javascript:;">退出</a></span>
			</div>
			<div style="display: none;" class="fast-login" id="fastLoginDiv" value="0">
				<table cellpadding="0" cellspacing="0" border="0">
					<tr>
						<th><label class="cor-ccc">用户名：</label></th>
						<td><input type="text" class="input-text bor-gray"  name="username" id="loginUsername" value="" /></td>
					</tr>
					<tr>
						<th><label>密码：</label></th>
						<td><input type="password" class="input-text bor-gray" name="password" id="loginPassword" /></td>
					</tr>
					<tr>
						<th></th>
						<td><input type="button" class="fast-login-btn" onclick="login(); return false;" value="登录" /></td>
					</tr>
				</table>
			</div>
            <div class="hr-h10"></div>
		</form>
	</div>
</div>
<script type="text/javascript">
<?php $commentSetting = setting('comment');?>
var islogin = {$commentSetting['islogin']}+0;
var loginUrl = "{APP_URL}{url('member/index/login')}";
var username = "{$commentSetting['defaultname']}";
var defaultname = "{$commentSetting['defaultname']}";

var commentTpl = "<dt><span class='user-info'>{location}网友 [{nickname}]： </span><span class='post-time'>{date}</span></dt><dd><p>{content}</p><div class='hr-dotted hr-h10'></div></dd>";
$(function(){
	$("#commentForm input[name='contentid']").val(contentid);
	getCommemt(contentid,$('#commentWrapper'),function(json) {
		$('.comments').html(json.total);
		if(json.total >0 ) $('.view-comment').show();
	});
	//评论提交
	$('#commentForm').submit(function() {
		if ($('textarea[name=content]', '#commentForm').val() == '') {
			alert('评论内容不能为空');
			return false;
		}
		if (islogin == 1 && !$.cookie(COOKIE_PRE+'auth')) {
			alert('请登录后再发表评论');
			return false;
		}
		return true;
	});
	if ($.cookie(COOKIE_PRE+'auth')) {
		//已登录
		username = $.cookie(COOKIE_PRE+'username');
		if(username == null) username = defaultname;
		$('#nickname').html(username);
		$('#quickLogout').show();
		$('#quickLogin').hide();
	} else {
		if(islogin == 1) {
			//需要登录才能评论
			$('#nickname').html('请登录后再发表评论');
			$('#anonymous').hide();
		} else {
			//不需要登录
			$('#anonymous').hide();
			$('#nickname').html(defaultname);
		}
	}
	
	$('#quickLogin > a').click(function() {
		var _t = $(this);
		var _d  = $('#fastLoginDiv');
		var _lp = _t.offset().left;
		var _tp = _t.offset().top;
		var _v = _d.attr('value');
			_d.css({'position':'absolute','left':_lp+50+'px','top':_tp-50+'px'})
			
		if(_v == 0) {
			_d.fadeIn();
			_d.attr('value',"1");
			_t.html('关闭登录');
		} else {
			_d.fadeOut();
			_d.attr('value',"0");
			_t.html('快速登录');
			$('#loginUsername').focus();
		}
	});
	$('#quickLogout > a').click(function() {
		$.getJSON(APP_URL+'?app=member&controller=index&action=ajaxlogout&jsoncallback=?', function(json){
			if(json.state) {
				if(islogin == 1) {
					//需要登录
					$('#nickname').html('请登录后再发表评论');
				} else {
					$('#nickname').html(defaultname);
				}
				$('#anonymous,#quickLogout').hide();
				$('#quickLogin').show();
				username = '';
			} else {
				alert('退出失败');
			}
		});
	});
});

//取评论函数
function getCommemt(contentid,wrapper,callback) {
	if(!contentid) return ;
	$.getJSON(APP_URL+'?app=comment&controller=comment&action=get&contentid='+contentid+'&jsoncallback=?', function(json){
		if(json.total) {
			for (var i=0,item;item=json.data[i++];) {
                var html = commentTpl;
				for (var key in item) {
					html = html.replace(new RegExp('{'+key+'}',"gm"), item[key]);
				}
				wrapper.append(html);
			}
		}
		typeof callback == 'function' && callback(json);
	});
}

function login() {
	var username = $('#loginUsername').val();
	var password = $('#loginPassword').val();
	if ( username == '') {
		alert('用户名不能为空');
		return false;
	}
	if ( password == '') {
		alert('密码不能为空');
		return false;
	}

	$.getJSON(APP_URL+'?app=member&controller=index&action=ajaxlogin&username='+encodeURIComponent(username)+'&password='+encodeURIComponent(password)+'&jsoncallback=?', function(json){
		if(json.state) {
			username = json.username;
			$('#nickname').html(username);
			$('#fastLoginDiv,#quickLogin').hide();
			$('#quickLogout,#anonymous').show();
		} else {
			alert(json.error);
		}
	});
}
</script>