// JavaScript Document

$(function () {
	$('#poll').each(function () {
		var distance = 10;
		var time = 250;
		var easemode = 'swing'
		var hideDelay = 250;

		var hideDelayTimer = null;

		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var info = $('.popup', this).css('opacity', 0);
		var eles = trigger.get();
		eles.push(info.get(0));
		
		//$([trigger.get(), info.get(0)]).mouseover(function () {
		$(eles).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			tid = $(this).attr('id');
			
			choice = tid.split('_')[1];
			
			if (choice) {
				$('#popup-contents').html(choices[choice]);
			} else if (this['tagName'] =='H1') {
				$('#popup-contents').html(choices[0]);
			}

			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;
				var loc = $(this).offset();
				info.css({
					top: loc.top-130,
					left: loc.left+170,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, easemode, function() {
					beingShown = false;
					shown = true;
				});
			}

			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, easemode, function () {
					shown = false;
					info.css('display', 'none');
				});

			}, hideDelay);

			return false;
		});
	});
});