function thumbs_submit_vote(object_id, vote)
{
	// make sure this thumb-link hasn't been disabled already
	if (vote==0 && $('disagree_'+object_id).hasClassName('disagree-disabled')) {
		return;
	}
	if (vote==1 && $('agree_'+object_id).hasClassName('agree-disabled')) {
		return;
	}
	
	$('value_'+object_id).value = vote;
	
	form = $('vote_'+object_id);
	
	new Ajax.Request(
    		form.action,
            {   method:form.method,
                parameters:form.serialize(),
                onSuccess: function(transport, results){
    				if (results.vote)
    				{
    					comment_vote(
    						results.object_id,
    						(results.vote==1)?'agree':'disagree',
    					    results.yes,
    					    results.no
    					);
    					
    				}
    			}
            });  
}

function comment_vote(id,which,yes_votes,no_votes)
{
	$('agree_'+id).innerHTML=yes_votes;
	$('disagree_'+id).innerHTML=no_votes;
	switch(which)
	{
	case "agree":
		$('agree_'+id).removeClassName('agree');
		$('agree_'+id).addClassName('agree-disabled');
		$('disagree_'+id).removeClassName('disagree-disabled');
		$('disagree_'+id).addClassName('disagree');
		break;
	case "disagree":
		$('disagree_'+id).removeClassName('disagree');
		$('disagree_'+id).addClassName('disagree-disabled');
		$('agree_'+id).removeClassName('agree-disabled');
		$('agree_'+id).addClassName('agree');
		break;		
	}
}

function thumbs_setup()
{
	$$('.agreeable').each( function(thumb) {
		thumb.stopObserving("click");
		thumb.observe("click", function() {
			thumbs_submit_vote(thumb.rel,1);
		});
	});
	$$('.disagreeable').each( function(thumb) {
		thumb.stopObserving("click");
		thumb.observe("click", function() {
			thumbs_submit_vote(thumb.rel,0);
		});
	});
}

// setup after load
document.observe("dom:loaded", function() {
	thumbs_setup();
});


// re-setup after each XMLHTTPRequest
Ajax.Responders.register({
	  onComplete: function() {
		thumbs_setup();
	  }
	});
