// a reference to the <a> tag, used in intercepting the click and displaying a warning if needed
var weatherby_buddy_request_current = false;
/**
 * @param object me - a reference to an <a> tag that has had its click event intercepted
 *                  - defaults to the variable 'weatherby_buddy_request_current' if null
 */
function weatherby_buddy_request(me){
    if (typeof me == 'undefined') {
        if (! weatherby_buddy_request_current) {
            return;
        } else {
            me = weatherby_buddy_request_current;
        }
    }

    var href = $(me).attr('href');
    var parent = $(me).parents('div.buddy-avatar:eq(0)');
    parent.addClass('loading');
    if ($(me).hasClass('approve') ) {
        parent.addClass('approve');
    }
    if ($(me).hasClass('deny') ) {
        parent.addClass('deny');
    }

    $.get(href, function(data,textStatus) {
        if(textStatus =="success") {
            if ($(me).hasClass('approve') || $(me).hasClass('request')) {
                $(me).parents('div.buddy-avatar:eq(0)').removeClass('loading').removeClass('approve').removeClass('deny').addClass('done');
            } else {
                $(me).parents('div.buddy-avatar:eq(0)').remove();
            }
        }
    });
}

function weatherby_buddy_prep_thickbox_div(templateId, newId)
{
    var template = $("#" + templateId);
    template.clone(true).attr("id", newId).hide().insertAfter(template);
}


$(document).ready(function(){

    // tabs
    $('.users-buddies .tabs .tab a').unbind().click(function(){
        var li = $(this).parents('li:eq(0)');
        li.addClass('active').siblings().removeClass('active');

        if (li.hasClass('user-requested')){
            $('.buddy-body .tab.user-requested').addClass('active').show();
            $('.buddy-body .tab.buddy-requested, .buddy-body .tab.approved').removeClass('active').hide();

        } else if (li.hasClass('buddy-requested')){
            $('.buddy-body .tab.buddy-requested').addClass('active').show();
            $('.buddy-body .tab.approved, .buddy-body .tab.user-requested').removeClass('active').hide();

        } else { // approved
            $('.buddy-body .tab.approved').addClass('active').show();
            $('.buddy-body .tab.buddy-requested, .buddy-body .tab.user-requested').removeClass('active').hide();

        }

        return false;
    });
    // make the first tab active
    $('.users-buddies .tabs .tab.approved a').click();


    // change buddy actions to ajax
    // approval
    $('div.buddy-avatar a.approve').unbind().click(function(){
        weatherby_buddy_request(this);
        $(this).parents('div.buddy-avatar:eq(0)').addClass('done');
        return false;
    });
    // remove buddy
    $('div.buddy-avatar a.deny, div.buddy-avatar a.cancel, div.buddy-avatar a.remove').unbind().click(function(){
        var parent = $(this).parents('div.buddy-avatar:eq(0)');
        var name = $('a.name', parent ).text();
        var iSrc = $('.avatar img', parent ).attr('src');
        var img = '<img src="'+iSrc+'" height="80" width="80" alt="Buddy" />';

        weatherby_buddy_request_current = this;
        weatherby_buddy_prep_thickbox_div("buddy_remove_warning", "buddy_thickbox");
        tb_show('', '#TB_inline?'+'height=170&width=300&modal=true&inlineId=buddy_thickbox', false);

        $('#TB_ajaxContent .buddy_warning h3').empty().append('Remove '+name+'?');
        $('#TB_ajaxContent .buddy_warning p span.name').empty().append(name);
        $('#TB_ajaxContent .buddy_warning .buddy-avatar .avatar img').before(img).remove();

        return false;
    });
    // request buddy
    $('div.buddy-avatar a.request').unbind().click(function(){
        var parent = $(this).parents('div.buddy-avatar:eq(0)');
        var name = $('a.name', parent ).text();
        var iSrc = $('.avatar img', parent ).attr('src');
        var img = '<img src="'+iSrc+'" height="80" width="80" alt="Buddy" />';

        weatherby_buddy_request_current = this;
        weatherby_buddy_prep_thickbox_div("buddy_request_warning", "buddy_thickbox");
        tb_show('', '#TB_inline?'+'height=160&width=300&modal=true&inlineId=buddy_thickbox', false);

        $('#TB_ajaxContent .buddy_warning span.name').empty().append(name);
        $('#TB_ajaxContent .buddy_warning .buddy-avatar .avatar img').before(img).remove();

        return false;
    });

    // remove buddy warning thickbox
    $('.buddy_warning a.remove, .buddy_warning a.add').unbind('click').click(function() {
        tb_remove();
        $("#buddy_thickbox").remove();
        weatherby_buddy_request(weatherby_buddy_request_current);
        weatherby_buddy_request_current = false;

        return false;
    });
    $('.buddy_warning a.cancel').unbind('click').click(function() {
        tb_remove();
        $("#buddy_thickbox").remove();
        weatherby_buddy_request_current = false;

        return false;
    });

    // remove search text for ie
    $('.buddy-search-wrap form input.button').val('');

});


