// JavaScript Document
$(function(){
	var loader=$('#loader');
	var pollcontainer=$('#pollcontainer');
	loader.fadeIn();
	// POLL FORMULIER TONEN
	$.get('/includes/poll.inc.php', '', function(data, status){
		pollcontainer.html(data);
		animateResults(pollcontainer);
		pollcontainer.find('#viewresult').click(function(){
			// TOON RESULT
			loader.fadeIn();
			$.get('/includes/poll.inc.php', 'result=1', function(data,status){
				pollcontainer.fadeOut(1000, function(){
					$(this).html(data);
					animateResults(this);
				});
				loader.fadeOut();
			});
			//prevent default behavior
			return false;
		}).end()
		.find('#pollform').submit(function(){
			var selected_val=$(this).find('input[name=poll]:checked').val();
			if(selected_val!=undefined){
				// POST DATA ALLEEN ALS OPTIE IS GESELECTEERD
				loader.fadeIn();
				$.post('/includes/poll.inc.php', $(this).serialize(), function(data, status){
					$('#formcontainer').fadeOut(100, function(){
						$(this).html(data);
						animateResults(this);
						loader.fadeOut();
					});
				});
			}
			//prevent form default behavior
			return false;
		});
		loader.fadeOut();
	});
	
/*	
	function animateResults(data){
		$(data).find('.bar').hide().end().fadeIn('slow', function(){
							$(this).find('.bar').each(function(){
								var bar_width=$(this).css('width');
								$(this).css('width', '0').animate({ width: bar_width }, 1000);
							});
						});
	}
*/	/* FIX FOR NEWER JQUERY*/	
	function animateResults(data){
		  $(data).find('.bar').hide().end().fadeIn('slow', function(){
				$(this).find('.bar').each(function(){
					  var bar_width=parseInt($(this).prev().find('em').text());
					  $(this).css('width', '0%').show().animate({ width: bar_width + '%' }, 1000);
				});
		  });
	}
	
});
