function voteLink() {
	return '<a class="vote" href="#">Choose</a>';
}

function setVoteButtons() {
	$('.entry-vote').each(function(){
		
		var id = $(this).attr('id').split('_')[1];
		$(this).find('form').replaceWith(voteLink());
		
		$(this).find('a.vote').unbind().click(function() {
			var clicked = $(this);
			var id = $(this).parent().parent().attr('id').split('_')[1];
			var postObj = { entry_id:id };
			// show the dialog box
			$('#voteDialog').fadeIn(200, function() {
				
				$('#voteDialog #yes').unbind().click(function() {
					// submit the vote
					$.post('/energizer/submit_vote/', postObj, function(r) {
						if (r.success) {
							for (var entry_id in r.entry_votes) {
								var vote = r.entry_votes[entry_id];
								$('#entry_'+entry_id+' p.votes').html(vote+' votes');
							}
							$('span.your-choice').replaceWith(voteLink());
							$('.entry-vote').removeClass('pick');
							clicked.parent().parent().addClass('pick');
							clicked.replaceWith('<span class="your-choice">Your Choice</span>');
							$('#voteDialog').fadeOut(100, setVoteButtons); // set it up again
						} else {
							alert(r.message);
						}
					}, 'json');
					return false;
				});
				
				$('#voteDialog #no').unbind().click(function() {
					// hide the dialog
					$('#voteDialog').fadeOut(250, setVoteButtons); // set it up again
					return false;
				});
				
			});
			return false;
		});
	});
}

// start it up
$(function() {
	$('#jumpToCategory')
		.change(function() {
			window.location = '/energizer/vote/'+$(this).val()+'/';
		})
		.next()
			.hide();
	setVoteButtons();
	// set up the colorbox
	$('a.zoom').colorbox();
});
