$(document).ready(function(){
	$('.data').each(function(){
		$(this).prepend('<div class="rdo"></div>');
		$(this).append('<div class="cnt"></div>');
		$(this).append('<div class="bar"></div>');
	});
	$.getJSON('data.php',{},function(data){
		finished(data);
//		if (data.posted) {
//			posted(data);
//		} else {
//			$('.data').each(function(){$('<input type="radio" name="answer">').attr({id:'answer'+$(this).attr('id'),value:$(this).attr('id')}).appendTo($(this).children('.rdo'));});
//			$('<div class="button"><button type="submit" name="submit" value="submit"></button></div>').hide().appendTo('.enquete').fadeIn();
//			$('.button button').hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');}).focus(function(){$(this).addClass('active');}).blur(function(){$(this).removeClass('active');});
//			$('.button button').click(function(){
//				var answer = $('input:checked[name=answer]').val();
//				if ( answer != undefined ) {
//					$('.button').fadeOut('normal',function(){
//						$(this).remove();
//						$('.rdo input[name=answer]').fadeOut('normal').remove();
//						$.post('data.php',{submit:true,token:data.token,answer:answer},function(data){
//							posted(data);
//						},'json');
//						return false;
//					})
//				} else {
//					$(this).blur();
//				}
//			});
//		}
	});
	function posted(data){
		$('.data .bar').append('<div class="value">&nbsp;</div>');
		$('.data').each(function(){
			var enq = $( '#' + $(this).attr('id') );
			var gdata = data.graph[$(this).attr('id')];
			if (!gdata) {gdata = {par:0,cnt:0,key:$(this).attr('id')};}
			$(enq).children('.bar').children('.value').css({width: 0}).animate({width: gdata.par + '%'}, 1000, 'easeOutBounce', function(){
				if ( data.posted == gdata.key ) {
					$(enq).children('.bar').children('.value').effect('pulsate',{times: 1},300);
					$(this).addClass('posted');
				}
				$(enq).children('.cnt').html(gdata.cnt);
			});
		});
	}
	function finished(data){
		$('.data .bar').append('<div class="value">&nbsp;</div>');
		$('.data').each(function(){
			var enq = $( '#' + $(this).attr('id') );
			var gdata = data.graph[$(this).attr('id')];
			if (!gdata) {gdata = {par:0,cnt:0,key:$(this).attr('id')};}
			$(enq).children('.bar').children('.value').css({width: 0}).animate({width: gdata.par + '%'}, 1000, 'easeOutBounce', function(){
				if ( gdata.max ) {
//					$(enq).children('.bar').children('.value').effect('pulsate',{times: 1},300);
					$(this).addClass('posted');
				}
				$(enq).children('.cnt').html(gdata.cnt);
			});
		});
	}
});