// JavaScript Document for Aussie SE home page.

var isPlaying = true;
var callout = 0;
var next = 1;

$(document).ready(function() {
													 
	// We need to hide the callouts that aren't displayed by default for the animation that takes place on the homepage.
	$('#containerCallouts div').each(function(i) {
		if (i == 0) {																 
			$(this).css('display', 'block');
		} else {
			$(this).css('display', 'none');
		}
	});
	
	// We add the class that shows what callout number in the sequence is highlighted at any given time. This doesn't need to be there if JS is disabled.
	$('#containerCallouts ul li').eq(0).addClass('showing');
	
	// We then need to make the current one hide and the relevant content, for the one that is being clicked, show.
	$('#containerCallouts ul li a').click(function() {
		
		// Stop the onLoad animation.
		isPlaying = false;
		
		// Get the URL from the clicked element and remove the # so that we can use it as an id reference.																			 
		var href = $(this).attr('href').substr(1);
		
		// if the link already has the class 'showing' meaning it's already active we don't want it to do anything when clicked.
		if ($(this).parent().hasClass('showing')) {
			return false;
		// Otherwise we need remove the class 'showing' from any elements that have it applied and then add it to the parent of the link that was clicked.
		} else {
			$(this).parent().parent().children().removeClass('showing');
			$(this).parent().addClass('showing');
			
			// We then fade out whatever child div has display set to 'block' and fade in the one that has a matching id for our href variable.
			$('#containerCallouts div').each(function() {
				if ($(this).css('display') == "block") {
					$(this).fadeOut(200, function() {
						window.setTimeout (sifrTxt, 1);
						$('#' + href).fadeIn(1200);														 
					});
				}
			});
			
			return false;
		}
		
	});
	
	// Function to animate callouts.
	function animateCallouts() {
		
		// First, we check if the animation is playing. We do this because it is stopped if the user clicks on the links.
		if (isPlaying) {
			// callout is set to 0 onLoad and next is set to 1. By default, we fade out callout0 and fade in callout1 and then increment or tweak the values of callout and next based on where we are in the relevant div array.
			$('#callout' + callout).fadeOut(200, function() {
				
				// We need to remove the 'showing' class from the current callout link and add it to the next one.
				$('#containerCallouts ul li').eq(callout).removeClass('showing');
				$('#containerCallouts ul li').eq(next).addClass('showing');
				
				window.setTimeout (sifrTxt, 1);
				
				// Finally, we fade in next and adjust the values of callout and next.
				$('#callout' + next).fadeIn(1200);	
				
				// At the beginning.
				if (next == 0) {
					callout = 0;
					next = 1;
				// At the end.
				} else if (next == $('#containerCallouts').children('div').size() - 1) {
					callout = $('#containerCallouts').children('div').size() - 1;
					next = 0;
				// Anywhere else.
				} else {
					callout = callout + 1;
					next = next + 1;
				}																						
																																						 
			});
		}
		
	};
	
	// Animate the callouts.
	var anim = setInterval (animateCallouts, 6000);
	
});

function sifrTxt() {
	
	// homepage callout 0.
	sIFR.replace(helveticaNeueLtStd, {
		selector: 'div#callout0 a',
		css: [
			'.sIFR-root { font-size: 21px; font-weight: bold; color: #ffffff; leading: -7; cursor: pointer; }',
			'.homeCallout { font-size: 13px; font-weight: normal; leading: -12; letter-spacing: 0.5; }',
			'.break { leading: -4; }'
		],
		wmode: 'transparent',
		tuneHeight: -11,
		offsetTop: -6
	});
	
	// homepage callout 1.
	sIFR.replace(helveticaNeueLtStd, {
		selector: 'div#callout1 a',
		css: [
			'.sIFR-root { font-size: 21px; font-weight: bold; color: #ffffff; leading: -7; cursor: pointer; }',
			'.homeCallout { font-size: 13px; font-weight: normal; leading: -10; letter-spacing: 0.5; }',
			'.homeCallout2 { font-size: 13px; font-weight: normal; leading: -2; letter-spacing: 0.5; }',
			'.homeCalloutLge { font-size: 28px; leading: -7; }',
			'.break { leading: -4; }'
		],
		wmode: 'transparent',
		tuneHeight: -15,
		offsetTop: -8
	});
	
}