var Favorite = {
    init: function() {
        $('a.favorite').live('click', Favorite.favorite);
        $('a.unfavorite').live('click', Favorite.unfavorite);
    },
    favorite: function(e) {
        e.preventDefault();
        link = $(this);
        $.post(link.attr('href'), {},
        function(response) {
            if (response.success) {
                var a = '';
                link.replaceWith(a);
            }
        }, 'json');
    },
    unfavorite: function(e) {
        e.preventDefault();
        link = $(this);
        $.post(link.attr('href'), {},
        function(response) {
            if (response.success) {
                var a = '';
                link.replaceWith(a);
            }
        }, 'json');
    }
};
var Relationship = {
    init: function() {
        $('a.follow').live('click', Relationship.follow);
        $('a.unfollow').live('click', Relationship.unfollow);
        $('a.block').live('click', Relationship.block);
        $('a.unblock').live('click', Relationship.unblock);
    },
    follow: function(e) {
        e.preventDefault();
        link = $(this);
        $.post(link.attr('href'), {},
        function(response) {
            if (response.success) {
                var a = '<a class="unfollow" href="/relationships/unfollow/' + response.to_user.username + '/"><span>Unfollow ' + response.to_user.username + '</span></a>';
                link.replaceWith(a);
            }
        }, 'json');
    },
    unfollow: function(e) {
        e.preventDefault();
        link = $(this);
        $.post(link.attr('href'), {},
        function(response) {
            if (response.success) {
                var a = '<a class="follow" href="/relationships/follow/' + response.to_user.username + '/"><span>Follow ' + response.to_user.username + '</span></a>';
                link.replaceWith(a);
            }
        }, 'json');
    },
    block: function(e) {
        e.preventDefault();
        link = $(this);
        $.post(link.attr('href'), {},
        function(response) {
            if (response.success) {
                var a = '<a class="unblock" href="/relationships/unblock/' + response.user_to_block.username + '/">Unblock</a>';
                link.replaceWith(a);
            }
        }, 'json');
    },
    unblock: function(e) {
        e.preventDefault();
        link = $(this);
        $.post(link.attr('href'), {},
        function(response) {
            if (response.success) {
                var a = '<a class="block" href="/relationships/block/' + response.user_to_block.username + '/">Block</a>';
                link.replaceWith(a);
            }
        }, 'json');
    }
};
$(function() {
    Favorite.init();
    Relationship.init();
    ProvideHtml5.all();
    $('a[rel=external]').click(function() {
        window.open(this.href);
        return false;
    });
	$('a[rel=tipsy]').tipsy({
	    fade: true,
	    gravity: 'n'
	});
    $('.timeago').timeago();
});