﻿Cufon.replace('#crossSiteNav .strapline h3');

$(document).ready(function(){
	addSectionNavSpans();
});

function initCrossSiteNav() {
	// select random promotional items
	$('#crossSiteNav .sectionPromo').randomChild();
	
	// add hover state to list items
	$('#crossSiteNav li h3, #crossSiteNav li img').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	// add section link to strapline and image
	$('#crossSiteNav li h3, #crossSiteNav li img').click(function() {
		window.location = ($(this).parents('li').find('a').attr('href'));
	});
	
	// set cycle interval
	var playSlideshow =  setInterval( 'slideSwitch()', 5000 );
	
	// set default section if none is specified
	if ( $('#crossSiteNav li.active').length == 0 ) $('#crossSiteNav li:first').addClass('active');
	
	// jump to specific section and pause cycle on hover
	$('#crossSiteNav > li').hover(function() {
		slideSwitch($(this));
		clearInterval(playSlideshow);
	},
	function() {
		playSlideshow =  setInterval( 'slideSwitch()', 5000 );
	});
}

function slideSwitch(obj) {
	if ( $('#crossSiteNav').children().length > 1) {

		// jump to end of animation if another is initiated
		if ($('#crossSiteNav li.active .sectionFeature').is(':animated')) {
			$('#crossSiteNav li.active .sectionFeature').stop().css({opacity: 1.0});
			$('#crossSiteNav li.active.last-active').removeClass('active last-active');
		}
	
		var $active = $('#crossSiteNav li.active');
	
		// set next section in cycle, at end of list reset to first item
		var $next = $active.next().length ? $active.next()
			: $('#crossSiteNav li:first');
			
		// check if a specific section has been requested
		if(obj) $next = obj;

		// only animate if next section is different to current section
		if($active.attr('id') != $next.attr('id')) {
			$active.addClass('last-active');
			$next.addClass('active');
			$next.children('.sectionFeature').css({opacity: 0.0})
				.animate({opacity: 1.0}, 700, function() {
					// hide all other sections
					$('#crossSiteNav li .sectionFeature').not(this).css({opacity: 0.0});
					$active.removeClass('active last-active');
				});
		}
	}
}

function addSectionNavSpans() {
	$("#crossSiteNav .sectionTitle").append('<span class="rightBorder"></span><span class="arrow"></span>');
}