/**
 * PHPWind PAGE JS
 * @Copyright Copyright 2011, phpwind.com
 * @Descript: 前台-喜欢组件
 * @Author	: linhao87@gmail.com
 * @Depend	: core.js、jquery.js(1.7 or later), global.js, jquery.form
 * $Id$
 */
;
(function () {

	var html_like = "<div class=\"p_favthr_tip J_pop_like\" style=\"display:none;\"><span>喜欢成功，请到个人中心“</span><a href=\"/like-mylike-run\" target=\"_blank\">我的喜欢</a><span>”查看</span></div>"
	var likeUtil = {
		close : function(){
			//隐藏输入
			$('div.J_pop_like').slideUp(function(){
			$(this).remove();	
			});
		},
		plus : function(elem, avatar){
			//加1
			var c = Number(elem.text());
			elem.slideUp('fast', function(){
				$(this).text(c+1).slideDown(function(){
					$(this).parent().show();
				});
			});
			
			//主楼显示最近喜欢
			if(avatar) {
				var read_like_list = $('#J_read_like_list');
				read_like_list.show().find('.J_read_like_tit').after('<a class="J_user_card_show" data-uid="'+ GV.U_ID +'" href="'+ GV.U_CENTER +'"><img height="50" width="50" src="'+ GV.U_AVATAR +'" class="J_avatar" data-type="small"><span>'+ GV.U_NAME +'</span></a>');
				Wind.Util.avatarError(read_like_list.find('img.J_avatar'));
			}
		}
	};
	var lock = false;
	window.likePlus = function(elem) {
		$(elem).on('click', function (e) {
				e.preventDefault();

				if(!GV.U_ID) {
					//未登录
					Wind.Util.quickLogin();
					return;
				}

				if(lock) {
					return;
				}
				lock = true;
				
				var $this = $(this),
					fid = $this.data('fid'),
					pid = $this.data('pid'),
					tid = $this.data('tid'),
					$wrap = $(html_like.replace('_FID', fid).replace('_PID', pid).replace('_TID', tid)),
					role = $this.data('role'); //区分喜欢按钮
				
				likeUtil.close();
				var url;
				
				Wind.Util.ajaxMaskShow();
				$.post(this.href, {
					typeid: $this.data('typeid'),
					fromid: $this.data('fromid')
				},function (data) {
					lock = false;
					Wind.Util.ajaxMaskRemove();
					if (data.state === 'success') {
					
						if(role == 'main') {
							//喜欢主楼
							likeUtil.plus($this.children('.J_like_count'), true);
						}else if(role == 'hot'){
							//热门喜欢
							likeUtil.plus($this.children('.J_like_count'), false);
						}else{
							//喜欢楼层
							likeUtil.plus($this.parent().find('a.J_like_user_btn'), false);
						}
					
						$wrap.appendTo($('.p_favthread')).slideDown();
						
						if(data.data > 3) {
							$('#J_like_m').hide();
						}
						
						timer = setTimeout(function(){
							likeUtil.close();
						}, 3000);
						
						var like_enter = $('#J_like_enter');
						

						
						$('#J_like_close').on('click', function (e) {
							e.preventDefault();
							likeUtil.close();
						});
						
					}else if(data.state === 'fail'){
						likeUtil.close();
						Wind.Util.resultTip({
							msg : data.message,
							error : true
						});
					}
					
				}, 'json');
				
		});
	}

})();
