var timer;

function switch_heading() {

	// Grab all the headings.
	var headings = $('#MastHead h2');

	var next = false;
	var current = false;
	
	// Figure out which is the current heading.
	for (var i=0; i < headings.length; i++) {
		if ($(headings[i]).css('display') == 'block') {
			current = i;
		}
	};	

	// Figure out which is the next heading to deal with.
	if (current + 1 < headings.length) {
		next = current + 1;
	}
	else {
		next = 0;
	}
	
	// Fade out/in the current/next heading.
	$(headings[current]).fadeOut("slow");
	$(headings[next]).fadeIn("slow");

	// Switch the #MastHead class.
	$('#MastHead').attr('class', $(headings[next]).attr('class'));
	
	// Start the timer for the next cycle.
	timer = setTimeout("switch_heading()", 4000);
		
}

$('document').ready(function() {

	// Hide all the headers, show the search header first.
	$('#MastHead h2').hide();
	$('#MastHead h2.search').show();
	
	// Switch header on link hover.
	$('a.marketleading, a.webtech, a.search').hover(function() {
	
		clearTimeout(timer);
	
		// Hide all the headers.
		$('#MastHead #carousel h2').hide();

		// Show the relevant header.
		$('#MastHead #carousel h2.' + $(this).attr('class')).show();
		
		// Set the class on #MastHead.
		$('#MastHead').attr('class', $(this).attr('class'));
		
	}, function() {

		timer = setTimeout("switch_heading()", 5000);
		
	});
	
	timer = setTimeout("switch_heading()", 5000);
	
});


