<script>
$(document).on('click', '.js-haya-favorite-show-users', function() {
	var show_users = $(".haya-favorite-show-users");
	var tid = $(this).attr("data-tid");
	if (show_users.hasClass("d-none")) {
		show_users.fadeIn().removeClass("d-none");
		if (!show_users.hasClass("favorite-users")) {
			show_users.addClass("favorite-users");
			
			var url = '<?php echo url("thread-favorite");?>';
			$.xpost(url, {'tid': tid}, function(code, msg) {
				if (code == 1) {
					show_users.find(".haya-favorite-users").html(msg);
				} else {
					show_users.removeClass("favorite-users");
					haya_favorite_msg = msg || "<?php echo lang('haya_favorite_load_error_tip'); ?>";
					show_users.find(".haya-favorite-users").html(haya_favorite_msg);
				}
			});
		}
	} else {
		show_users.fadeOut().addClass("d-none");
	}
});

<?php if ($uid) { ?>
$(document).on('click', '.js-haya-favorite-add', function() {
	$(this).attr("disabled", "disabled");

	var thiz = $(this);
	var tid = thiz.attr("data-tid");
	var url = '<?php echo url("my-favorite-create");?>';
	$.xpost(url, {'tid': tid}, function(code, msg){
		if (code == 1) {
			thiz.removeClass('js-haya-favorite-add')
				.addClass('js-haya-favorite-del')				
				.attr('title', '<?php echo lang('haya_favorite_delete_favorite'); ?>');		
		
			thiz.find(".icon").removeClass('icon-star-o')
				.addClass('icon-star')
				.attr("aria-label", '<?php echo lang('haya_favorite_delete_favorite'); ?>');
				
			thiz.find(".haya-favorite-btn")
				.text('<?php echo lang('haya_favorite_delete'); ?>');
				
			$(".haya-favorite-show-users").find(".haya-favorite-users").html(msg.users);
			$(".haya-favorite-user-count").text(msg.count);			
		} else {
			$.alert(msg);
		}
		
		thiz.removeAttr("disabled");
	});
});

$(document).on('click', '.js-haya-favorite-del', function() {
	$(this).attr("disabled", "disabled");

	var thiz = $(this);
	var tid = thiz.attr("data-tid");
	var url = '<?php echo url("my-favorite-delete");?>';
	$.xpost(url, {'tid': tid},function(code,msg){
		if (code == 1) {				
			thiz.removeClass('js-haya-favorite-del')
				.addClass('js-haya-favorite-add')
				.attr('title', '<?php echo lang('haya_favorite_add'); ?>');		
		
			thiz.find(".icon").removeClass('icon-star')
				.addClass('icon-star-o')
				.attr("aria-label", '<?php echo lang('haya_favorite_add'); ?>');
				
			thiz.find(".haya-favorite-btn")
				.text('<?php echo lang('haya_favorite_add'); ?>');
		
			$(".haya-favorite-show-users").find(".haya-favorite-users").html(msg.users);
			$(".haya-favorite-user-count").text(msg.count);				
		} else {
			$.alert(msg);
		}
		
		thiz.removeAttr("disabled");
	});
});
<?php } else { ?>
$(document).on('click', '.js-haya-favorite-tip', function() {
	$.confirm('<?php echo lang('haya_favorite_add_favorite_confirm_title'); ?>', function() {
		window.location = "<?php echo url('user-login'); ?>";
	}, {'body': '<?php echo lang('haya_favorite_login_favorite_tip'); ?>'});		
});
<?php } ?>
</script>
