
RA.ready.push(function() {

    $('button.follow').click(function() {
        var $button = $(this),
            userId = $button.attr('title'),
            isFollowing = $button.find('.unfollow:visible').size(),
            url = '/home/follow/' + (isFollowing ? 'remove' : 'add'),
            $loading = $('<img class="vab" src="/image/icon/loading_arrows.gif" />');

        $button.hide().after($loading);
        $.ajax({
            type: 'POST',
            url: url,
            data: { user_id: userId },
            success: function(data) {
                if ( $(data).find('form[action*=login]').size() ) {
                    location.href = '/user/login?required';
                }
                else {
                    $loading.remove();
                    if ( $button.hasClass('notoggle') ) {
                        $button.closest('li').slideUp();
                    }
                    else {
                        $button.show().children().hide();
                        $button.find(isFollowing ? '.follow' : '.unfollow').show();
                    }
                }
            }
        });
    });

    $('ul.tags li').click(function() {
        var tag = this.title,
            existsTag = false,
            isActive = ( $(this).hasClass('active') === true );

        $('input.tag').each(function() {
            if ( this.value == tag ) {
                existsTag = true;
                $(this).remove();
                $('ul.tags li[title='+tag+']').each(function() {
                    $(this).removeClass('active');
                });
            }
        });

        if ( !existsTag ) {
            var $copy = $('input.tag[value=]').val(tag).clone()
            $copy.val('').keyup(checkTag);
            $('input.tag').parent().append($copy);
            $('ul.tags li[title='+tag+']').each(function() {
                $(this).addClass('active');
            });
        }
    });

    $('input.tag').keyup(checkTag).trigger('keyup');

    function checkTag() {

        // add empty text box
        var existsEmptyText = false;
        $('input.tag').each(function() {
             if ( this.value == '' ) existsEmptyText = true;
        });
        if ( !existsEmptyText ) {
            var copy = $(this).clone().val('').keyup(checkTag);
            $(this).parent().append(copy);
        }

        // remove extra text box
        var removeCount = $('input.tag[value=]').size() - 1;
        $('input.tag[value=]').each(function() {
            if ( removeCount-- ) $(this).remove();
        });

        // enable tag
        $('ul.tags li[title=' + this.value + ']').each(function() {
            $(this).addClass('active');
        });

        // disable tag
        $('ul.tags li.active').each(function() {
            var tag = this.title,
                existsTag = false;
            $('input.tag').each(function() {
                if ( this.value == tag ) existsTag = true;
            });
            if ( !existsTag ) $(this).removeClass('active');
        });

    }

});

