$(function(){
	$('#ajax-note').ajaxStart(function(){
		$(this).html('开始载入...').fadeIn('fast');
	});
	
	$('#ajax-note').ajaxError(function(){
		$(this).html('载入错误！');
	});
	
	$('#ajax-note').ajaxSend(function(){
		$(this).addClass('ajax-loading').html('正在发送请求...');
	});
	
	$('#ajax-note').ajaxComplete(function(){
		$(this).removeClass('ajax-loading');
	});
	
});

function deletePost()
{
	var a = confirm('您确定要删除此文章吗？');
	if (!a) return false;
	var tthis = $(this);
	var ptr = tthis.parents('tr');
	var subject = ptr.children('td.post-subject');
	$.ajax({
		url: tthis.attr('href'),
		type: 'post',
		dataType: 'json',
		cache: false,
		success: function(data) {
			if (data.result == 1) 
				ptr.fadeOut(1000, function(){$(this).remove()});
			$('#ajax-note').html(data.message);
		}
	});
	return false;
}

function checkboxClick()
{
	var tthis = $(this);
	var td = tthis.parent();
	if (tthis.attr('checked'))
		td.removeClass('bg-checkbox').addClass('bg-checked');
	else
		td.removeClass('bg-checked').addClass('bg-checkbox');
}

function deletePosts()
{
	var a = confirm('您确定要删除这些文章吗？');
	if (!a) return false;
	var chk = $(':checkbox:checked');
	var chkdata = chk.serialize();
	
	$.ajax({
		url: BU + '/admin/post/delete',
		type: 'post',
		dataType: 'json',
		cache: false,
		data: chkdata,
		success: function(data) {
			if (data.result == 1) {
				chk.parents('tr').fadeOut(2000);
			}
			$('#ajax-note').html(data.message);
		}
	});
	return false;
}


function trMouseOver()
{
	$(this).addClass('bg-dark');
}

function trMouseOut()
{
	$(this).removeClass('bg-dark');
}

function selectAll()
{
	$(':checkbox').attr('checked', 'checked');
}

function selectInverse()
{
	var chk = $(':checkbox');
	chk.each(function(){
		var tthis = $(this);
		var ischecked = tthis.attr('checked');
		if (ischecked)
			tthis.removeAttr('checked');
		else
			tthis.attr('checked', 'checked');
	});
}

/****************** category ******************/
function deleteCategory()
{
	var a = confirm('您确定要隐藏此分类吗？');
	if (!a) return false;
	var tthis = $(this);
	var ptr = tthis.parents('tr');
	var subject = ptr.children('td.txt-name');
	$.ajax({
		url: tthis.attr('href'),
		type: 'post',
		dataType: 'json',
		cache: false,
		success: function(data) {
			if (data.result == 1) 
				ptr.fadeOut(1000, function(){$(this).remove()});
			$('#ajax-note').html(data.message);
		}
	});
	return false;
}

function deleteUser()
{
	var a = confirm('您确定要删除此用户吗？');
	if (!a) return false;
	var tthis = $(this);
	var ptr = tthis.parents('tr');
	var subject = ptr.children('td.user-name');
	$.ajax({
		url: tthis.attr('href'),
		type: 'post',
		dataType: 'json',
		cache: false,
		success: function(data) {
			if (data.result == 1) 
				ptr.fadeOut(1000, function(){$(this).remove()});
			$('#ajax-note').html(data.message);
		}
	});
	return false;
}


function changeState()
{
	var tthis = $(this);
	$.ajax({
		url: tthis.attr('href'),
		type: 'post',
		cache: false,
		dataType: 'json',
		success: function(data) {
			if (data.result == 1 || data.result == 0) {
				var img = RESBU + 'admin/images/state' + data.result + '.gif';
				tthis.children('img').attr('src', img);
			}
			$('#ajax-note').html(data.message)
		}
	});
	return false;
}

function deleteComment()
{
	var a = confirm('您确定要隐藏此评论吗？');
	if (!a) return false;
	var tthis = $(this);
	var ptr = tthis.parents('tr');
	var subject = ptr.children('td.expand-collapse');
	$.ajax({
		url: tthis.attr('href'),
		type: 'post',
		dataType: 'json',
		cache: false,
		success: function(data) {
			if (data.result == 1) {
				ptr.next('tr').fadeOut(500, function(){$(this).remove();});
				ptr.fadeOut(500, function(){$(this).remove();});
			}
			$('#ajax-note').html(data.message);
		}
	});
	return false;
}