var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;
var voted_question_id;
var voted_answer_id;

$(document).ready(function() {
	$('#poll-form').submit(formProcess);  
	$('#submit-vote').click(function(event) {
		formProcess(event);
	});
	if ($("#poll-results").length > 0 ) {
	    showResults();
	}
	$('#view-result').click(function(event) {
		event.preventDefault();
	    $("#poll-container").empty();
	    $.getJSON("poll.do",loadResults);
	});
//	if ($.cookie('voted_question_id')) {
//	    $("#poll-container").empty();
//	    votedID = $.cookie('vote_id');
//	    $.getJSON("poll.htm",loadResults);
//	}
});

function formProcess(event) {
	event.preventDefault();
	var pollQuestionId = $("input[name='pollQuestionId']").attr("value");
	var pollAnswerId = $("input[@name='pollAnswerId']:checked").attr("value");
	if(pollAnswerId == null) {
		alert('please select your vote.');
		return;
	}
	pollAnswerId = pollAnswerId.replace("answer",'');
	pollQuestionId = pollQuestionId.replace("question",'');
	
	$("#poll-container").fadeOut("slow",function(){   
		$(this).empty();
		voted_question_id = pollQuestionId;
		voted_answer_id = pollAnswerId;
		$.getJSON("poll.do?pollQuestionId="+pollQuestionId+"&pollAnswerId="+pollAnswerId,loadResults);   
		$.cookie('voted_question_id', voted_question_id, {expires: 365});   
	});      
}

function loadResults(data) {

	var total_votes = 0;
	var percent;

//	for(answer in data.question.pollAnswers) {
//		alert('answerId =' + data.question.pollAnswers[answer].id + 
//				'/answerText=' + data.question.pollAnswers[answer].answerText + 
//				'/order=' + data.question.pollAnswers[answer].order + 
//				'/voteCount=' + data.question.pollAnswers[answer].voteCount);
//	}
	for (answer in data.question.pollAnswers) {
		total_votes = total_votes + parseInt(data.question.pollAnswers[answer].voteCount);
	}
	var results_html = "<div id='poll-results'><h2>Poll Results</h2>\n<dl class='graph'>\n";
	for (answer in data.question.pollAnswers) {
		var vote = data.question.pollAnswers[answer].voteCount;
		percent = Math.round((parseInt(vote)/parseInt(total_votes))*100);
		if(isNaN(percent)) percent = 0;
		if (data.question.pollAnswers[answer].id == voted_answer_id && data.question.id == voted_question_id ) {
			results_html = results_html+"<dt class='bar-title'>"+data.question.pollAnswers[answer].answerText+"</dt><dd class='bar-container'><div id='bar"+data.question.pollAnswers[answer].id+"'style='width:0%;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
		} else {
			results_html = results_html+"<dt class='bar-title'>"+data.question.pollAnswers[answer].answerText+"</dt><dd class='bar-container'><div id='bar"+data.question.pollAnswers[answer].id+"'style='width:0%;background-color:#0066cc;'>&nbsp;</div><strong>"+percent+"%</strong></dd>\n";
	    }
	}

	results_html = results_html+"</dl></div><div class='clear'></div><p><strong>Total Votes: "+total_votes+"</strong></p>\n";

	$("#poll-container").append(results_html).fadeIn("slow",function(){
		animateResults();
	});
}

function animateResults(){
	  $("#poll-results div").each(function(){
	      var percentage = $(this).next().text();
	      $(this).css({width: "0%"}).animate({
					width: percentage}, 'slow');
	  });
	}


