//Initializes the home page

var transitionSpeed = 400;
var visibleNumbers = {
	n09: false,
	n58: false,
	n50: false
};

function hover_audience_over(element) {
	//this doesn't work for some reason
	if ($.support.opacity) {
		var max = 12.1;
		var min = 12;
		$(element).find(':animated').stop(true);
		$(element).find('.audience_scroll').css('font-size', min);
		$(element).find('.ellipse').hide();
		if ($(element).find('.full').css("visibility") != "visible") {
			$(element).find('.full').css("visibility", "visible");
		}
		$(element).find('.audience_scroll').animate(
			{ 'font-size': max },
			{
				duration: transitionSpeed,
				step: function(currentStep) {
					var percent = (currentStep - min) / (max - min);
					$(this).find('img').css('opacity', 1 - percent);
					$(this).find('.full').css('opacity', percent);
					$(this).css('margin-top', -35 * percent);
				},
				complete: function() {
					$(this).find('img').css('opacity', 0);
					$(this).find('.full').css('opacity', 1).css('visibility','visible');
					$(this).css('margin-top', -35);
				}
			}
		);
	} else {
		//support IE
		$(element).find(":animated").stop(true);
		$(element).find('.ellipse').css('display', 'none');
		$(element).find('.full').css('visibility','visible');
		$(element).find('.audience_scroll').animate(
			{ 'margin-top': -35 },
			{ 
				duration: transitionSpeed,
				complete: function () {
					$(this).find('img').css('visibility','hidden');
				}
			}
		);
	}
}

function hover_audience_out(element) {
	if ($.support.opacity) {
		var max = 12.1;
		var min = 12;
		$(element).find(":animated").stop(true);
		$(element).find('.audience_scroll').css("font-size", min);
		$(element).find('.audience_scroll').animate(
			{ 'font-size': max },
			{
				duration: transitionSpeed,
				step: function(currentStep) {
					var percent = (currentStep - min) / (max - min);
					$(this).find('img').css('opacity', percent);
					$(this).find('.full').css('opacity', 1 - percent);
					$(this).css('margin-top', -35 * (1 - percent));
				},
				complete: function() {
					$(this).find('.ellipse').css('display','inline').show();
					$(this).find('img').css('opacity', 1).css('display','block');
					$(this).find('.full').css('opacity', 0).css('visibility', 'hidden');
					$(this).css('margin-top', 0);
				}
			}
		);
	} else {
		//support IE
		$(element).find(":animated").stop(true);
		$(element).find('img').css('visibility','visible');
		$(element).find('.audience_scroll').animate(
			{ 'margin-top': 0 },
			{ 
				duration: transitionSpeed,
				complete: function () {
					$(this).find('.ellipse').css('display', 'inline');
					$(this).find('.full').css('visibility','hidden');
				}
			}
		);
	}
}

function hover_number_over(element) {
	var max = 12;
	var min = 0.1;
	
	$(element).stop(true, true);
	$(element).find(":animated").stop(true);
	$(element).css('font-size', min);
	$(element).animate(
		{
			"font-size": max
		}, 
		{
			duration: transitionSpeed, 
			step: function (currentStep) {
				var percent = (currentStep - min) / Math.abs(max - min);
				$(this).css("height", 327 * percent);
				$(this).css("top", -327 * percent);
				$(this).find('.full').css("top", 327 * (percent - 1));
			},
			complete: function() {
				var curr = this;
				$('.numbers .full_mask').each(
					function() {
						if (this != curr) {
							$(this).css('height', 0).css('top',0)
							$(this).find('.full').css('top', 1);
						}
					}
				);
			}
		}
	);
	visibleNumbers['n' + $(element).attr('id').substr(10,2)] = true;
}

function hover_number_out(element) {
	var max = 12;
	var min = 11.9;
	$(element).stop(true, true);
	$(element).find(":animated").stop(true);
	$(element).css('font-size', max);
	$(element).animate(
		{
			'font-size': min
		},
		{
			duration: transitionSpeed,
			step: function (currentStep) {
				var percent = (currentStep - min) / Math.abs(max - min);
				$(this).css("height", 327 * percent);
				$(this).css("top", -327 * percent);
				$(this).find('.full').css("top", 327 * (percent - 1));
			}
		}
	);
	visibleNumbers['n' + $(element).attr('id').substr(10,2)] = false;
}

$(function() {
		//load a random image for the background
		$('.bg_img').attr('src', 'images/home_img_'+Math.ceil(Math.random() * 3).toString()+'.jpg');
		
		//Set up the audience hover states
		$('.audience').hover(
		
			/* MouseOver */
			function () {
				hover_audience_over(this);
			}, 
			
			/* MouseOut */
			function () {
				hover_audience_out(this);
			}
		);
		
		$('.numbers').bind(
			'mouseleave',
			function() {
				if (visibleNumbers.n09) {
					hover_number_out($('#full_mask_09'));
				}
				if (visibleNumbers.n58) {
					hover_number_out($('#full_mask_58'));
				}
				if (visibleNumbers.n50) {
					hover_number_out($('#full_mask_50'));
				}
			}
		);
		
		$('.numbers .midground .full_mask').bind(
			'mouseleave',
			function() {
				hover_number_out(this);
			}
		);
		
		$('.numbers .foreground .trigger').bind(
			'mouseenter',
			function() {
				hover_number_over($('#full_mask_'+$(this).attr('id').substr(8,2)));
			}
		);
		
		//establish rollover states for learn more images
		$('.learn_more').hover(
			function() {
				$(this).css('background-image', $(this).css('background-image').replace('.gif','_over.gif'));
			},
			function() {
				$(this).css('background-image', $(this).css('background-image').replace('_over.gif', '.gif'));
			}
		);
	}
);
