/* jQuery('document').ready(function(){ */

var alley_fighters = $H();
var fl_class = 'fight_link';
var sf_class = 'start_fight';
var c1_class = 'contender_1';
var c2_class = 'contender_2';
var ff_class = 'fight_fight';

function post_selector(post_id, topic_id, selected) {

    var body = jQuery('#post_' + post_id + '_wrapper');

    if(selected) {

        alley_fighters[post_id] = body.attr('user_id');
        body.addClass('alley_fight_suggestion');

    } else {
        body.removeClass('alley_fight_suggestion');
    }

}

function alley_fight_post(post_id, topic) {

    selected = alley_fighters[post_id];
    post_selector(post_id, topic, !selected);

    switch(alley_fighters.values().length) {
    case 1:
        // Only one post selected
        //   Remove the initial class from the fight link
        //   Add the second class to the fight link
        jQuery('#start_fight_for_post_' + alley_fighters.keys()[0]).removeClass(sf_class).addClass(c1_class);

        // Disable the fight link for the first select
        jQuery('a.' + c1_class).click(function(){ return false; });

        // Setup the Choose Contender links
        jQuery('a.' + sf_class).removeClass(sf_class).addClass(c2_class).each(function(){
                var p = jQuery(this);
                var pid = p.attr('post_id');
		//p.unbind('click');
                p.click(function(){ alley_fight_post(pid, topic); return false;});
            });
        break;

    case 2:
        // Both contenders are selected
        // Setup the 'Submit' links (selecting by id is the fastest method)
        var c1 = jQuery('#start_fight_for_post_' + alley_fighters.keys()[0]);
        var c2 = jQuery('#start_fight_for_post_' + alley_fighters.keys()[1]);

        // for the two selected posts:
        jQuery.each([c1, c2], function(){

                //   Remove their contender_x class
                //   Add the final fight class
                this.removeClass(c1_class).removeClass(c2_class).addClass(ff_class);

                //   Assign the new onclick methods to the two chosen posts using an inline form
		//this.unbind('click');
                this.click(function(){
                        var f = document.createElement('form');
                        this.parentNode.appendChild(f);
                        f.method = 'POST';
                        f.action = '/alley_fights/create/?id=' + topic + alley_fighters.collect(function(v,k){
                                return '&posts['+v[0]+']='+v[1]; }) + '&return_to=' + document.location.pathname;
                        f.submit();
                        return false;
                    });
            });


        // Erase the class for all fight links except those for the two selected posts and hide them
        jQuery('a.' + c2_class).removeClass(c2_class).hide();

        break;
    }
}

function alley_fight(post_id, topic_id){

    alley_fight_post(post_id, topic_id);
    jQuery('#alley_fight_dialog').show();

}

function cancel_alley_fight() {

    jQuery('.post_body_wrapper').each(function(){
            var pbw = jQuery(this);
            post_selector(pbw.attr('post_id'), pbw.attr('topic_id'), false);
        });

    jQuery('a.' + fl_class).removeClass(c1_class).removeClass(c2_class).removeClass(ff_class).addClass(sf_class).each(function(){
            var p = jQuery(this);
            var pid = p.attr('post_id');
            var tid = p.attr('topic_id');
	    p.unbind('click');
            p.click(function(){ alley_fight(pid, tid); return false; });
        }).show(); // we do a show() at the end to show any links which were hidden during the process

    jQuery('#alley_fight_dialog').hide();
    alley_fighters = $H();

}

/*    }); */
