// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 'answer';
var OPT_VOTES = 'votes';

$(document).ready(function(){
	$("#poll").submit(formProcess); // setup the submit handler

	if ($("#poll-results").length > 0 ) {
		animateResults();
	}
});

function formProcess(event){
	event.preventDefault();

	var vote_id = $("input[@name='answer']:checked").attr("value");
	var id = $("#poll_id").attr("value");

	$("#poll-container").fadeOut("slow",function(){
		$(this).empty();

		$.getJSON("/inc.poll.php?poll_id=" + id + "&vote_id=" + vote_id,loadResults);
	});
}

function animateResults(){
	$("#poll-results div").each(function(){
		var percentage = $(this).next().text();
		$(this).css({width: "0%"}).animate({width: percentage}, 'slow');
	});
}

function loadResults(data) {
	results_html = data['html'];

	$("#poll-container").append(results_html);
	$(".bar").css({width: "0%"});
	$("#poll-container").fadeIn("slow",function(){
		animateResults();
	});
}
