// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;

var votedID;

var appear = 'Vote';

$(document).ready(function()
{
	$("#btnVote").click(function()
	{
		$(".formError").fadeTo("fast",0,function()
		{	// to hide errors on close comments form
			$(".formError").remove();
		});

		if ( ($.cookie('vote_id') != $("#idPollQuestionID").val() && $("#result").length > 0) || ($.cookie('vote_id') != $("#idPollQuestionID").val() && appear == 'Comments')) 
		{
		  	$("#ResultReplace").empty();

			$("#voting").fadeOut(500);
			$("#btnAddComments").removeClass("btnAddComments2");
			$("#btnAddComments").addClass("btnAddComments");
			$("#btnResult").removeClass("btnResult2");
			$("#btnResult").addClass("btnResult");
		}
		else
		{
			formProcess();
		}
		appear = 'Vote';
	}); // setup the submit handler

	$("#btnResult").click(function()
	{
		$("#voting").fadeOut(500);
		$("#btnAddComments").removeClass("btnAddComments2");
		$("#btnAddComments").addClass("btnAddComments");
		
		$(".formError").fadeTo("fast",0,function()
		{	// to hide errors on close comments form
			$(".formError").remove();
		});

		if ($("#result").length == 0 ) 
		{
			$("#AddCommentAjaxLoad").fadeIn(500);
	
			var idQuestionID = $("#idPollQuestionID").val();
		
			$.getJSON($("#idProjectPathUrl").val()+"ajax/Poll.php?QuestionID="+idQuestionID+"&OptionsID=none",loadResults);
		}
		else if(appear == 'Comments' || appear == 'Vote')
		{
			$("#ResultReplace").show();
			$("#btnResult").removeClass("btnResult");
			$("#btnResult").addClass("btnResult2");
			animateResults();
		}
		appear = 'Result';
	}); // setup the submit handler

	$("#btnAddComments").click(function()
	{
		$("#btnAddComments").removeClass("btnAddComments");
		$("#btnAddComments").addClass("btnAddComments2");
		$("#btnResult").removeClass("btnResult2");
		$("#btnResult").addClass("btnResult");
		$("#ResultReplace").hide();
		$("#voting").fadeIn(500);
		appear = 'Comments';
	});
	if ($("#result").length > 0 ) 
	{
		animateResults();
	}
	if ($.cookie('vote_id') == $("#idPollQuestionID").val()) 
	{
		$("#AddCommentAjaxLoad").fadeIn(500);
	
		var idQuestionID = $("#idPollQuestionID").val();
	
		$("#idlineAnswer").empty();
		//votedID = $.cookie('vote_id');
		$.getJSON($("#idProjectPathUrl").val()+"ajax/Poll.php?QuestionID="+idQuestionID+"&OptionsID=none",loadResults);
	}
});

function formProcess()
{
	//event.preventDefault();
  
  	var id = $("input[@name='PollOption']:checked").attr("value");
	if(id)
	{
		var idQuestionID = $("#idPollQuestionID").val();
	
		$("#idlineAnswer").fadeOut("slow",function()
		{
			$(this).empty();
		
			//votedID = id;
			$.getJSON($("#idProjectPathUrl").val()+"ajax/Poll.php?QuestionID="+idQuestionID+"&OptionsID="+id,loadResults);
		
			$.cookie('vote_id', idQuestionID, {expires: 365});
		});
	}
}

function animateResults()
{
	$("div.result_percentage div").each(function()
	{
    	var percentage = $(this).parent().prev().prev().text();
      	$(this).css({width: "0%"}).animate({width: percentage}, 'slow');
 	});
}

function loadResults(data) 
{
	var total_votes = 0;
  	var percent;
  
  	for (id in data) 
	{
    	total_votes = total_votes+parseInt(data[id][OPT_VOTES]);
  	}
  
  	var results_html = "<div id='result'>\n";
  	results_html += "		<div class='result_header'>\n";
  	results_html += "			<div class='result_header_content'></div>\n";
  	results_html += "		</div>\n";
	results_html += "		<div class='result_title'>"+$("#questionVote").html()+"</div>";
	results_html += "		<div class='result_content'>";
	var i=1;
	var classAdd ='';
  	for (id in data) 
	{
    	percent = Math.round((parseInt(data[id][OPT_VOTES])/parseInt(total_votes))*100);
    	//if (data[id][OPT_ID] !== votedID) 
		{
			if(i == 1)	j=2;
			else if(i == 2)	j=4;
			else if(i == 3)	j=1;
			else j=3;
			classAdd = 'result_menu'+j;
			results_html += "	<div class='result_menu "+classAdd+"'>";
			results_html += "		<div class='percentage'>"+percent+"%</div>";
			results_html += "		<div class='result_menu_content'>"+data[id][OPT_TITLE]+"</div>";
			results_html += "		<div class='result_percentage'>";
			results_html += "			<div style='width:0%'></div>";
			results_html += "		</div>";
			results_html += "	</div>";
    	}
		//else 
		{
			//results_html = results_html+"<div class='percentage'>"+percent+"%</div><div id='bar-labelAnswer'><div style='position:absolute;margin-right:10px;white-space:nowrap;'>"+data[id][OPT_TITLE]+"</div><div class='bar-container' style='width:0%;border-color:#1E518B;'></div></div>";
    	}
		i++;
  	}
	results_html += "		</div>\n";
	results_html += "	</div>\n";
	//results_html = results_html+"</dl><p>Total Votes: "+total_votes+"</p></div>\n";
  
	$("#AddCommentAjaxLoad").fadeOut(500);
  	$("#ResultReplace").append(results_html).fadeIn("slow",function()
	{
    	animateResults();
		$("#btnResult").removeClass();
		$("#btnResult").addClass("btnResult2");
	});
}