jQuery(document).ready(function() {
	jQuery("#pollAjaxLoader").hide(); //hide the ajax loader
	jQuery("#pollMessage").hide(); //hide the ajax loader
	jQuery("#pollSubmit").click(function() {
		var pollAnswerVal = jQuery('input:radio[name=pollAnswerID]:checked').val();//Getting the value of a selected radio element.
		var pollIdVal = jQuery('#pollID').val();//Getting the value of actual Poll.		
		if (jQuery('input:radio[name=pollAnswerID]:checked').length) {
			jQuery("#pollAjaxLoader").show(); //show the ajax loader
			jQuery.ajax({  
				type: "POST",  
				url: "/ezpoll/vote",  
				data: { pollAnswerID: pollAnswerVal, action: "vote", pollID: pollIdVal },
				success: function(theResponse) { 					
					jQuery("#poll-container").load("/ezpoll/result/"+pollIdVal+"/"+pollAnswerVal);
					//the functions.php returns a response like "1|13|#ffcc00-2|32|#00ff00-3|18|#cc0000-63" which the first number is the answerID, second is the points it has and third is the color for that answer's graph. The last number is the sum of all points for easilt calculating percentages.
					if (theResponse == "voted") { 
						jQuery("#pollAjaxLoader").hide(); //hide the ajax loader
						jQuery("#pollMessage").html("sorry, you already voted.").fadeTo("slow", 1);
					} else {					
						jQuery("#sondage").hide('slow'); //hide the ajax loader again
						jQuery("#result").show();
						jQuery("#pollAjaxLoader").hide(); //hide the ajax loader again
						jQuery("#pollSubmit").attr("disabled", "disabled"); //disable the submit button
					}
				}  
			});  
			return false; 
	
	
		
		} else {
			jQuery("#pollMessage").html("please select an answer.").fadeTo("slow", 1, function(){
				setTimeout(function() {
					jQuery("#pollMessage").fadeOut("slow");
				}, 3000);																		 
			});
			return false;
		}
	
	});

});
