function animate_poll(){
	METER_HEIGHT = 175;
	ANIMATION_SPEED = 250;

	scores = [];
	
	
	var sum = 0;
	var min = 101;
	var score = 101;
	$('.number').each(function(){
		score = parseInt($(this).html().split("<span>")[0]);
        sum += score;
        if(score < min)
        {
            min = score;
        }
	});

	$('.number').each(function(){
		score = parseInt($(this).html().split("<span>")[0]);
		
		// Fixing rounding if sums not up to 100
		// e.g. 12.4 + 12.4 + 75.2 will all round down
		if( sum < 100 && score == min)
		{
            score += 100 - sum;
		}

		el = $(this).parent().find(".result");
		el.css("height", (METER_HEIGHT / 100) * score); 
		scores[scores.length] = [score, el];
	});

	$('.number').html("0<span>%</span>");

	function sorter(a, b){
		if (a[0] < b[0]){
			return -1;
		}
		if (a[0] == b[0]){
			return 0;
		}
		if (a[0] > b[0]){
			return 1;
		}
	}

	scores.sort(sorter);

	var results = [];

	for (var i in scores){
		results[results.length] = $(scores[i][1]);
	}

	nextAnim(results);

	function nextAnim(results) {
		if(results.length == 0){ return; }
	    results[0]
				.animate(
					{ height: 'toggle' },
					{
						duration: ANIMATION_SPEED,
						complete: function(){
                            // Cufon.refresh();
							nextAnim(results.slice(1));
						},
						step: function(now, fx){
							score = Math.round((now / 175) * 100);
							$(this).parent().next().html(score + "<span>%</span>");
						}
					}

				)
	}
}

function makeOverlay() {
	$('.video, #overlay').hide();
	$('.videoLink').bind('click',function(e){
		e.preventDefault();		
		var thisurl = $(this).attr('href');

		$('#overlay').show();
		$(thisurl).show();
		
	});
	$('.close, #overlay').bind('click',function(e){
		e.preventDefault();
		$('.video, #overlay').hide();
	});
}
$(document).ready(function(){

	makeOverlay();

	animate_poll();

});
