if (typeof Follow == 'undefined') {var Follow = {};}
(function($){
	Follow.is_active = '1';
	Follow.topFollow = function(obj){
		var _obj = $(obj);
		var json_data = JSON.parse($(obj).attr("data"));
		_obj = $('<img src="/img/common/loader1.gif" alt="loading" class="loader" />').replaceAll(_obj);
		$.post('/follow/follow.php',{
			is_insert:1,
			follow_user_id:json_data.user_id
		},function(res){
			if (res == "1")
			{
				switch (json_data.state)
				{
				case "followers":
					_obj.replaceWith('<a href="#" class="btn_unfollow_top" data=\'{"user_id":'+json_data.user_id+',"state":"friends"}\'><img src="/img/common/related_3.gif" alt="friends" /></a>');
					break;
				case "no related":
					_obj.replaceWith('<a href="#" class="btn_unfollow_top" data=\'{"user_id":'+json_data.user_id+',"state":"following"}\'><img src="/img/common/related_1.gif" alt="following" /></a>');
					break;
				}
			}else{
				_obj.replaceWith('<span>실패</span>');
				if (res != ''){$.alertTop(res);}
			}
		});
	};

	Follow.topUnFollow = function(obj){
		var _obj = $(obj);
		var json_data = JSON.parse($(obj).attr("data"));
		_obj = $('<img src="/img/common/loader1.gif" alt="loading" class="loader" />').replaceAll(_obj);
		$.post('/follow/unfollow.php',{
			is_insert:1,
			unfollow_user_id:json_data.user_id
		},function(res){
			if (res == "1")
			{
				switch (json_data.state)
				{
				case "following":
					_obj.replaceWith('<a href="#" class="btn_follow_top" data=\'{"user_id":'+json_data.user_id+',"state":"no related"}\'><img src="/img/common/related_4.gif" alt="no related" /></a>');
					break;
				case "friends":
					_obj.replaceWith('<a href="#" class="btn_follow_top" data=\'{"user_id":'+json_data.user_id+',"state":"followers"}\'><img src="/img/common/related_2.gif" alt="followers" /></a>');
					break;
				}
			}else{
				_obj.replaceWith('<span>실패</span>');
				if (res != ''){$.alertTop(res);}
			}
		});
	};



	Follow.setFollow = function(obj, multi, related){
		var _obj = $(obj);
		var follow_user_id = _obj.closest("tr").find("input[type=hidden]").attr("value");
		var user_screen_name = $.trim(_obj.closest("tr").find(".td_bio a.user").text());
		var _obj_parent = _obj.parent();
		_obj_parent.html('<img src="/img/common/loader1.gif" alt="loading" class="loader" />');
		$.post('/follow/follow.php',{
			is_insert:1,
			follow_user_id:follow_user_id,
			force:"1"
		},function(res){
			if (res == "1")
			{
				var status_val = $.trim($(".form_status").val());
				if (status_val.indexOf("@"+user_screen_name+" ") == -1)
				{
					//$(".form_status").val("@" + user_screen_name + " " + status_val + " ");
				}
			}
			if (res != "1")
			{
				_obj_parent.html('실패');
				if (res != ''){$.alertTop(Msg.convert_kr(res));}
			}else if (related == true)
			{
				if(_obj_parent.hasClass("no")){
					_obj_parent.html('<a href="#" class="btn_unfollow"><img src="/img/common/related_1.gif" alt="following" /></a>');
					_obj_parent.removeClass("no");
					_obj_parent.addClass("following");
				}else if(_obj_parent.hasClass("followers")){
					_obj_parent.html('<a href="#" class="btn_unfollow"><img src="/img/common/related_3.gif" alt="friends" /></a>');
					_obj_parent.removeClass("followers");
					_obj_parent.addClass("friends");
				}
				
			}else{
				_obj_parent.html('<span class="isFollowing">Following</span><a href="#" class="btn_unfollow"><img src="/img/common/btn_unfollow.gif" alt="unfollow" /></a>');
			}
			if (multi)
			{
				Follow.multiFollow();
			}
		});
	};
	Follow.setUnFollow = function(obj, related){
		var _obj = $(obj);
		var unfollow_user_id = _obj.closest("tr").find("input[type=hidden]").attr("value");
		var _obj_parent = _obj.parent();
		_obj_parent.html('<img src="/img/common/loader1.gif" alt="loading" class="loader" />');
		$.post('/follow/unfollow.php',{
			is_insert:1,
			unfollow_user_id:unfollow_user_id,
			force:"1"
		},function(res){
			if (res != "1")
			{
				_obj_parent.html('실패');
				if (res != ''){$.alertTop(Msg.convert_kr(res));}
				return false;
			}
			if (related == true)
			{
				if(_obj_parent.hasClass("friends")){
					_obj_parent.html('<a href="#" class="btn_follow"><img src="/img/common/related_2.gif" alt="followers" /></a>');
					_obj_parent.removeClass("friends");
					_obj_parent.addClass("followers");
				}else if(_obj_parent.hasClass("following")){
					_obj_parent.html('<a href="#" class="btn_follow"><img src="/img/common/related_4.gif" alt="no related" /></a>');
					_obj_parent.removeClass("following");
					_obj_parent.addClass("no");
				}
				
			}else{
				_obj_parent.html('<a href="#" class="btn_follow"><img src="/img/common/btn_follow.gif" alt="follow" /></a>');
				//alert(res);
			}
		});
	};
	Follow.allSelect = function(){
		$("td[class=td_num] input[type=hidden]").each(function(){
			this.checked = true;
		});
	};

	Follow.selectFollow = function(){
		multi_user_id = new Array();
		$("td[class=td_num] input[type=hidden]").each(function(){
			if (this.checked)
			{
				multi_user_id.push(this.value);
			}
		});
		multi_cnt = multi_user_id.length;
		Follow.multiFollow();
	};

	Follow.multiFollow = function(){
		var select_cnt = multi_user_id.length;
		if (select_cnt > 0)
		{
			var follow_id = multi_user_id[0];
			$("#follow_progress").html((multi_cnt - select_cnt + 1) + " / " + multi_cnt);
			multi_user_id.shift();
			if ($("input[value="+follow_id+"]").closest("tr").find(".td_follow a.btn_follow").size() == 1)
			{
				this.setFollow($("input[value="+follow_id+"]").closest("tr").find(".td_follow a").get(0), true, true);
			}else{
				Follow.multiFollow();
			}
		}else{
			alert("완료");
		}
	};

	Follow.follow = function(obj){
		var _obj = $(obj);
		var follow_user_id = _obj.data("id");
		var _obj_parent = _obj.parent();
		var _tr = _obj.closest("tr").closest("tr");
		_obj_parent.html('<img src="/img/common/loader1.gif" alt="loading" class="loader" />');
		$.post('/follow/follow.php',{
			is_marketing:1,
			is_insert:1,
			follow_user_id:follow_user_id
		},function(res){
			if (res != "1")
			{
				_obj_parent.html('실패');
				if (res != ''){$.alertTop(res);}
			}else{
				_obj_parent.html('<a href="#" data-id="'+follow_user_id+'" class="btn_unfollow"></a>');
			}
		});
	};

	$("#rt_table .btn_unfollow").live("click", function(){Follow.unfollow(this); return false;});
	$("#rt_table .btn_follow").live("click", function(){Follow.follow(this); return false;});

})(jQuery);
