var item_num = 0;

$.fn.extend({
	//发送评论
	postComment : function(){

		$('#postComment').find('.postCommentPlus').addClass('loading');

		$.post('/ajax/comment/insert', {
			post_id: $('#post_id').val(), 
			post_author: $('#post_author').val(), 
			post_title: $('#post_title').val(),
			comment_content: $('#comment_content').val(),
			comment_type: $('#comment_type').val()
			},
			function(i){
				item_num++;
				var newItem = '<div id="newItem' + item_num + '" class="hide">'+ i +'</div>';
				$('#new_comments').append(newItem);
				$('#newItem' + item_num).effect("highlight", {mode: 'show'}, 600);
				$('#comment_content').val('');
				$('#comment_content').focus();

				//给新添加的条目绑定事件
				$('#newItem' + item_num).hover(
					function(){$(this).find('a.hide').css('visibility', 'visible')},
					function(){$(this).find('a.hide').css('visibility', 'hidden')}
				);
				bind_delete_btn($('#newItem' + item_num).find('a.delete'));
				//把所有的scrollingLink重新绑定一遍
				$('a.scrollingLink').scrollingLink();
				//移除加载图片
				$('#postComment').find('.loading').removeClass('loading');
				//移除“暂无评论”
				$('.no_comment').hide();
			}
		);
	},
	focusPostComment: function(replyTo){
		if (document.selection){
			var currentPosition = document.selection.createRange();
			currentPosition.text = replyTo;
		}else{
			var len = this.val().length;
			var start = this.selectionStart;
			var end = this.selectionEnd;
			this.val(this.val().substring(0,start) + replyTo + this.val().substring(end,len));
		}
	},

	//链接跳转
	scrollingLink : function(){
		this.click(function(){
			var object = $(this);
			var target = $('#comment_' + object.attr('href').replace(/#*/, ''));
			var selfComment = object.parent().parent().parent();
			if(!target.find('a').is('[name="return"]')){
				target.find('.grid_10').append('<p class="tipBox f12 inBlock"><a href="#' + selfComment.attr('id') + '" id="return_comment' + selfComment.attr('id') + '" class="scrollingLink" name="return">↓  返回相关回复...</a></p>');
			}
			$('#return_comment' + selfComment.attr('id') ).click(function(){
				$(this).parent().remove();
				selfComment.smoothScrolling('highlight');
			});

			if((target.html() == null)){
				if(!selfComment.find('p').is('.tipBox')){
					selfComment.find('.grid_10').append('<p class="tipBox inBlock" id="tip_' + selfComment.attr('id') + '">对应的留言已被删除...</p>');
					$('#tip_' + selfComment.attr('id')).autoHide(3000);
				}
			}else{
				target.smoothScrolling('highlight');
			}
		});
	},

	//回复
	feedBackTo: function(){
		this.click(function(){
			var replyTo = $(this).attr('name') + ' ';
			var textarea = $('#comment_content');
			textarea.focus();
			textarea.focusPostComment(replyTo);
			return false;
		});
	},

	//follow
	follow:function(){
		this.click(function(){
			var obj = $(this);
			$.post('/ajax/follow/insert',{
			 	following_id : obj.attr('name')
			 },
			 function(i){
			 	
				if(i == 1){
					obj.after('<a href="#" class="follow" name="' + obj.attr('name') + '"><span class="checked inBlock"></span><strong>已追踪</strong><span class="gray">( 取消 )</span></a>');
					$('a.follow').follow();
					obj.remove();
				}else{
					obj.after('<a href="#" class="linkBtn follow" name="' + obj.attr('name') + '"><span>追踪</span></a>');
					$('a.follow').follow();
					obj.remove();
				}
			});
		});
	}

});

//收藏
function save_fav(){
	$('.fav').removeClass('favSuccess');
	$('.fav').addClass('favLoading');

	$.post("/ajax/fav/topic_fav", { 
		topic_id: $('#post_id').val(),
		fav_type: $('#comment_type').val()
		},
		function(i){
			$('.fav').removeClass('favLoading');

			if(i == 1){
				$('.fav').addClass('favSuccess');
			}
		}
	);
	return false;
}

//绑定删除按钮
function bind_delete_btn(object){
	object.click(function(){
		var comment_id = $(this).parent().find('a.delete').attr('name');
		$(this).parent().find('a.delete').css('visibility', 'hidden');

		$(this).parent().append('<p class="tipBox textCenter">确定删除吗？<br /><a href="#" class="delete_comment_yes green" name="'+ comment_id +'"><strong>是</strong></a> / <a href="#" class="delete_confirm green" onclick="$(this).parent().remove();return false;"><strong>否</strong></a></p>');
		$(this).parent().find('.tipBox').autoHide(10000);

		//正式删除评论
		$(this).parent().find('a.delete_comment_yes').click(function(){
			var comment_block = $(this).parent().parent().parent();
			$(this).parent().remove();
			$.post("/ajax/comment/delete", { 
				comment_id : $(this).attr('name'),
				comment_type : $('#comment_type').val(),
				comment_parent : $('#post_id').val()
				},
				function(i){

				}
			);

			comment_block.effect("highlight", {color: '#fff95b', mode: 'hide'}, 600);
			return false;
		});
		return false;
	});
}

//回复
function feedBackTo(object){
	object.click(function(){
		var replyTo = $(this).attr('name') + ' ';
		var textarea = $('#comment_content');
		textarea.focus();
		textarea.focusPostComment(replyTo);
		return false;
	});
}


 $(document).ready(function() {
	//添加评论
	$('#post_comment_insert').click(function(){
		$().postComment('topic');
		return false;
	});

	//收藏
	$('.fav').click(function(){
		save_fav();
	});

	//显示回复按钮
	$('.comment').hover(
		function(){
			if( ! $(this).find('p').hasClass("tipBox")){
				$(this).find('a.hide').css('visibility', 'visible');
			}
		},
		function(){$(this).find('a.hide').css('visibility', 'hidden')}
	);

	//链接跳转
	$('a.scrollingLink').scrollingLink();

	//为删除按钮绑定事件
	bind_delete_btn($('a.delete'));

	//绑定回复按钮
	$('a.reply').feedBackTo();

	//绑定快速回复
	postHotkey();

	//绑定追踪按钮
	$('a.follow').follow();
});

function postHotkey(){
	$(document).keydown(function(event){
		if((event.ctrlKey) && (event.keyCode==13)) {
			$().postComment('topic');
			return false;
		}
	});
}