// make sure the Submit My Votes button is hidden from the start
document.write('<style type="text/css">.look_here input { display: none}</style>');

$.wait_for('div.footer', function(){
	var AJAX_URL = '/assets/scripts/namelist_ajax.php';

	// no need for the Submit My Votes button	
	$('input:submit').remove();

	$('input[@name^=v]:radio').click(function(){
		// figure out the ID from the name of this radio button (strip non-digit characters)
		// the name is like "v[213124]"
		var id = this.name.replace(/[^\d]/g, '');
		
		var container = this.parentNode; 

		// hide the radio buttons so they can't interact while the Ajax is happening
		$('input:radio', container).css('visibility', 'hidden');
		
		$.getJSON(AJAX_URL, {
			method: 'saveVote',
			id: id,
			nlid: window.NLID,
			vote: this.value
		}, function(json) {
			// toString() is needed or the before() functions below won't add anything
			var pos = json[0].toString(), neg = json[1].toString();
			
			// replace the radio button with value == 1 with the pos value
			$('input[@value=1]:radio', container).before(pos).remove();

			// replace the radio button with value == 9 with the neg value
			$('input[@value=9]:radio', container).before(neg).remove();
		});
	});
})
