// 显示异常
function prvcShowException(recommend_status)
{
	recommend_status.html(prvcrecommendpost.exception);
	recommend_status.css('color', 'red');
	recommend_status.fadeIn('slow');
}

// 请求Ajax
function prvcSendRequest(recommend, recommend_status, blog_id, post_id)
{
	recommend.unbind('click');
	recommend.fadeOut('slow', function() {
		recommend.html(prvcrecommendpost.dealing);
		recommend.fadeIn('slow', function() {
			jQuery.ajax({
				type: "POST",
				url: ajaxurl,
				data: "action=recommend-post&recommend=true&blog_id=" + blog_id + "&post_id=" + post_id,
				success: function(msg) {
					temp = msg.split('|');
					result = temp[0];
					total = temp[1];
					recommend.fadeOut('slow', function() {
						if (result == 'success') {
							//日志推荐成功
							recommend.html(prvcrecommendpost.recommended + '(' + total + ')');
							recommend.removeClass('recommend');
							recommend.fadeIn('slow');
							
//							recommend.click(function() {
//								prvcShowException(recommend_status);
//							});
						} else if(result == 'recommended') {
							//日志已经推荐过
							recommend.html(prvcrecommendpost.recommended + '(' + total + ')');
							recommend.removeClass('recommend');
							recommend.fadeIn('slow');
							
							//prvcShowException(recommend_status);
						} else {
							//用户未登录
							location.href = prvcrecommendpost.login_url;
						}
					});
				}
			});
		});
	});
}

// 产品首页请求Ajax
function site_recommend_request_data(recommend, recommend_num, blog_id, post_id)
{
	recommend.unbind('click');
	recommend_num.fadeOut('slow', function() {
		
		recommend_num.html('<img src="' + prvcrecommendpost.loading + '" />');
		recommend_num.fadeIn('slow', function() {
			jQuery.ajax({
				type: "POST",
				url: ajaxurl,
				data: "action=recommend-post&recommend=true&blog_id=" + blog_id + "&post_id=" + post_id,
				success: function(msg) {
					temp = msg.split('|');
					result = temp[0];
					total = temp[1];
					recommend_num.fadeOut('slow', function() {
						if (result == 'success') {
							//日志推荐成功
							recommend_num.html(total);
							recommend_num.fadeIn('slow');
							recommend.html(prvcrecommendpost.recommended);
							recommend.removeClass('site-recommend');
//							recommend.click(function() {
//								prvcShowException(recommend_status);
//							});
						} else if(result == 'recommended') {
							//日志已经推荐过
							recommend_num.html(total);
							recommend_num.fadeIn('slow');
							recommend.html(prvcrecommendpost.recommended);
							recommend.removeClass('site-recommend');
							//prvcShowException(recommend_status);
						} else {
							//用户未登录
							location.href = prvcrecommendpost.login_url;
						}
					});
					
				}
			});
		});
	});
}

// 绑定事件
jQuery(document).ready(function() {
	jQuery('.recommend').each(function() {
		jQuery(this).click(function() {
			recommend = jQuery(this);
			temp = recommend.attr('id').split('_');
			blog_id = temp[1];
			post_id = temp[2];
			recommend_status = jQuery('#recommend_status_' + blog_id + '_' + post_id);
			prvcSendRequest(recommend, recommend_status, blog_id, post_id);
		});
	});
	
	//产品首页推荐处理
	jQuery('.site-recommend').livequery('click',
		function() {
			recommend = jQuery(this);
			temp = recommend.attr('id').split('-');
			blog_id = temp[1];
			post_id = temp[2];
			recommend_num = jQuery('#recommend-num-' + blog_id + '-' + post_id);
			site_recommend_request_data(recommend, recommend_num, blog_id, post_id);
		}
	);
});