
/************ ANIMATION OF RESULTS **************/

function animate() {
	var ieMax = 30;
	var delay = 75;
	var animation = 600;
	var i = 0;
	$('td.posgraph span, td.neggraph span').each(function () {
		var $td = $(this).parent('td');
		var $span = $(this);
		var fullWidth = parseInt($span.css('width'), 10);
		if (!isNaN(fullWidth)) {
			$td.css({'width': fullWidth+'px'});
			$span.css({'width': '1px'});
			if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && i>ieMax) {
				setTimeout(function(){
					if (fullWidth > 0) {
						$span.css({'visibility': 'visible'}); 
						$span.css({'width':fullWidth+'px'});
					}
				}, ieMax*delay+animation);
			}
			else {
				setTimeout(function () {
					if (fullWidth > 0) {
						$span.css({'visibility': 'visible'}); 
						$span.animate({'width':fullWidth+'px'}, {'duration':animation, 'easing':'linear'});
					}
				}, i*delay);
			}
			i++;
		}
	});
	var duration = 0;
	$('#resultspanel').each(function () {
		$(this).find('.twoparty .party .seatswon, .twoparty .party .predict').each(function (i) {
			var isPredict = $(this).hasClass('predict');
			var $span = $('.graph', this);
			var $num = $('.num', this);
			var $label = $('.label', this);
			$num.attr('id', 'num'+i);
			$num.attr('id', 'num'+i);
			$span.attr('id', 'span'+i);
			$label.attr('id', 'label'+i);
			var num = parseInt($num.html(), 10);
			var fullWidth = num*2;
			$span.css({'width': '1px'});
			$span.css({'visibility': 'visible'}); 
			$num.html(0);
			duration = Math.max(duration, num*40);
			var delay = ($(this).hasClass('predict') ? duration : 0)+250;
			for (var j=0; j<=num; j++) {
				setTimeout('$("#'+$num.attr('id')+'").html('+j+')', ((duration/num)*j)+delay);
			}
			for (var j=0; j<=fullWidth; j++) {
				setTimeout('$("#'+$span.attr('id')+'").width('+j+')', ((duration/fullWidth)*j)+delay);
			}
			setTimeout('$("#'+$label.attr('id')+', #'+$num.attr('id')+', #'+$label.attr('id')+', #'+$num.attr('id')+'").css({"visibility": "visible"})', delay);
		});
	});
}
document.write('<style type="text/css">td.neggraph span, td.posgraph span, #resultspanel .twoparty .party .predict .label, #resultspanel .twoparty .party .predict .graph, #resultspanel .twoparty .party .predict .num, #resultspanel .twoparty .party .seatswon .label, #resultspanel .twoparty .party .seatswon .graph, #resultspanel .twoparty .party .seatswon .num { visibility: hidden; }</style>'); // hide bars ASAP
$(document).ready(function () {
	animate();
});



/************* LINK MODIFICATION ****************/

$(document).ready(function () {
	$('a[href*=".pdf"]').each(function () {
		$(this).bind('click', function () {
			return popup(this.href, 950, 700);
		});
	});
});


$(document).ready(function () {
	// enhance election nav
	$("#election-nav > ul > li.dropdown").hoverIntent(
		function(){
			$(this)
				.addClass("hover")
				.children("div.drop")
					.css({left: 'auto'})
				.end()
				.each(function() {
					if ($(this).closest("#election-nav").length > 0) {
						$(this).children("div.wide").css({left: '0'});
					}
				});
		},
		function(){
			$(this)
				.removeClass("hover")
				.children("div.drop")
					.css({left: '-9999px'});
		}
	);
});


