
$(function(){

	// Ensuring the content stays in the middle of the screen
	$(window).bind('resize', centerContent);
	setInterval(centerContent, 1000);
	centerContent();

	// Change the flyout position on the primary nav items that extend past the safe area
	$('ul.primary-navigation > li:gt(3) ul li ul[foposition=170,0]').each(function(){
		$(this).attr('foposition','-172,0');
	});

	// Apply flyouts
	$('ul.primary-navigation').flyouts();
	
	// Tools navigation: Remove the right border from the last link
	$('ul.tools-navigation > li:last > a').css({'border-right':'none','padding-right':'0px','background-image':'none'});
	
	// Primary navigation: Remove the right border from the last link
	$('ul.primary-navigation > li:first > a').css({'padding-left':'0px'});
	$('ul.primary-navigation > li:last > a').css({'background-image':'none', 'padding-right':'0px'});
	
	// Seondary navigaiton: If no items exist remove the container
	if ($('ul.secondary-navigation li').length == 0) {
		
		// Do not hide the secondary navigation if it's the contact us page
		if (document.location.href.match(/contactus.aspx/i) == null) {
			$('div.secondary-navigation-wrapper').css('display','none');
		}
		
	}
	
	// Apply transparency to the footer copyright
	$('div.footer span').css('opacity','0.7');
	
	// Hide tertiary navigation that hasn't been selected
	$('ul.secondary-navigation > li a:not(.selected) + ul')
		.slideToggle(0);

	// Add a click event to secondary nav's that have a tertiary nav
	$('ul.secondary-navigation > li:has(ul) > a').click(function(){
		$('ul', $(this).parent()).slideToggle(300);
	});

	// Add sub nav seperator line to the tertiary nav group
	$('ul.secondary-navigation > li ul')
		.css({
			'background':'transparent url(media/images/sub-nav-seperator.png) repeat-x top left'
		});

	// Remove the nav seperator line to the last li in the tertiary nav
	$('ul.secondary-navigation > li ul li:last')
		.css({
			'background-image':'none'
		});
	
	// Remove the nav seperator line to the last li in the secondarynav
	$('ul.secondary-navigation > li:last')
		.css({
			'background-image':'none'
		});

	

});

// Apply Cufon font styling
Cufon.replace('h1, h2, h3, h4, h5, .trajan, button')('ul.primary-navigation > li > a', {hover:true})('ul.secondary-navigation a', {hover:true});

jQuery.fn.attrs = function(n) {
	var a = [];
	$(this).each(function(){a.push($(this).attr(n))});
	return a;
};

function centerContent() {
	var el = $('div.page-wrapper');
	var fot = $('div.footer-wrapper');
	var win = $(window);
	var w = el.width();
	var h = el.height();
	var dw = win.width();
	var dh = win.height();
	
	el
		.css({
			'left':Math.max(10, (dw - w) >> 1) + 'px',
			'top':Math.max(10, (dh - h) >> 1) + 'px'
		});
	
	fot
		.css({
			'top':el.height()
		});
	
}
