// Onload scripts for all site pages
// Dependencies: 
//	jQuery 1.4+,
//	nhp-library.js (with nhpDomManipulator class instantiated into var nhpManipulateDom)
//	superfish.js
//	jquery-alerts.js
//	jquery.colorbox-min.js

// Define min. screen size for desktop browsers
var isDesktopBrowserNhp = ($(window).width() > 480);

// Apply default search words behavior
$('#searchWords').nhpInputDefaultText();

// Add pull-down submenu functionality to utility (upper right) navigation
$('#navUtility').superfish({autoArrows: false, delay: 125});

// Place structural html for dynamically-created pop-over navigation to use
$('#navPrimaryWrap').append('<div id="navPrimaryChildModal"><div class="genWrap"><a href=""><img class="menuClose menuCloseLeft" src="/_files/images/template/menu-close.gif" width="21" height="21" title="Close Menu" alt="Close Menu" /></a><div class="gen1"></div><div class="gen2"></div><a href=""><img class="menuClose menuCloseRight" src="/_files/images/template/menu-close.gif" width="21" height="21" title="Close Menu" alt="Close Menu" /></a></div><div class="genWrapBottom"></div></div>');

// Add open in new window functionality for marked links
$('a.linkNewWindow').attr('target', '_blank').attr('title', function() {
	return this.title + ' This will open a new window / tab';
});

if (isDesktopBrowserNhp && $.colorbox) {
	// Add modal behavior using Colorbox.js to iframe links - desktop browser only
	$('#contentPrimary a.modalWindowLink, #contentSecondary a.modalWindowLink, #contentTertiary a.modalWindowLink').colorbox({initialWidth: '100px', initialHeight: '100px', width: '90%', height: '90%', title: '', iframe: true, fixed: true});

	// Add modal behavior using Colorbox.js to video links - desktop browser only
	$('#contentPrimary a.videoLinkYouTube, #contentSecondary a.videoLinkYouTube, #contentTertiary a.videoLinkYouTube').colorbox({initialWidth: '100px', initialHeight: '100px', innerWidth: 640, innerHeight: 385, iframe:true, fixed: true});
	$('#contentPrimary a.videoLinkGeneral, #contentSecondary a.videoLinkGeneral, #contentTertiary a.videoLinkGeneral').colorbox({initialWidth: '100px', initialHeight: '100px', innerWidth: 640, innerHeight: 480, iframe:true, fixed: true});
}

// Add navigation warning to applicable navigation elements -- includes "do not show this again" feature via cookie.
$('#navUtility a.navigationNotice, #navPrimary a.navigationNotice, #navSecondary a.navigationNotice, #navFooter a.navigationNotice, #contentPrimary a.navigationNotice, #contentSecondary a.navigationNotice, #contentTertiary a.navigationNotice').click(function(event) {
	var cookies = '' + document.cookie;
	if (cookies.indexOf('disableNavConfirmation') == -1)
	{
		event.preventDefault();
		var anchorHref = $(this).attr('href');
		var linkTarget = $(this).attr('target');
		var linkHasNewWindowClass = $(this).hasClass('linkNewWindow');
		jConfirm($(this).attr('title'), 'Please Confirm', function(proceed) {
			if (proceed) {
				if (linkHasNewWindowClass || linkTarget == '_blank') {
					window.open(anchorHref);
				}
				else {
					window.location = anchorHref;
				}
			}
		});
	}
});

// Add formatted alert message (element's title attribute) on click
$('.showAlertTitle').click(function(event) {
	jAlert($(this).attr('title'), 'Information', null);
});

// Setup current main nav tree at page load so ready to use with dynamic JS modal nav
$('#navPrimary > li.current > a').siblings('ul').clone(true).appendTo('#navPrimaryChildModal .gen1');
$('#navPrimaryChildModal .gen1 ul li.current > a').each(function() {
	if ($(this).siblings('ul').length > 0) {
		$(this).siblings('ul').clone(true).appendTo('#navPrimaryChildModal .gen2');
		$('#navPrimaryChildModal .gen2').append('<div class="gen2Bottom"></div>');
	}
});
$('#navPrimaryChildModal .gen1 ul li > a').click(function(event) {
	if ($(this).siblings('ul').length > 0) {
		event.preventDefault();
		$('#navPrimaryChildModal .gen2').hide(0).empty();
		$(this).siblings('ul').clone(true).appendTo('#navPrimaryChildModal .gen2');
		$('#navPrimaryChildModal .gen2').append('<div class="gen2Bottom"></div>');
		$('#navPrimaryChildModal .gen2').show('slow');
		$(this).parent().siblings().removeClass('currentNav');
		$(this).parent().addClass('currentNav');
		$(this).blur();
	}
});


// Pause the banner rotation -- if present -- while on hover state so pull-down can overlap banner
// If navPrimary is already open, don't resume banner rotation. Closing main menu will do that.
$('#navUtility').hover (
	function () {
		if ($('#bannerRotationCollection').cycle) {
			$('#bannerRotationCollection').cycle('pause');
			$('#bannerRotationCollection .bannerRotation').css('z-index', 1);
		}
	}, 
	function () {
		if ($('#bannerRotationCollection').cycle && $('#navPrimaryChildModal').is(':hidden')) {
			$('#bannerRotationCollection').cycle('resume');
		}
	}
);
 

// Add close menu event to element within modal menu
$('#navPrimaryChildModal .menuClose').click(function(event) {
	event.preventDefault();
	$('#navPrimaryChildModal').slideUp();
	if ($('#bannerRotationCollection').cycle) {


		$('#bannerRotationCollection').cycle('resume');
	}
	$('#navPrimary > li.currentDisabled').removeClass('currentDisabled').addClass('current');
	$('#navPrimary li').removeClass('currentNav');
});

// Fire close menu event on everything but modal window
$('body').click(function(event) {
	if ($(event.target).closest('#navPrimaryWrap').length == 0) {
		$('#navPrimaryChildModal').slideUp();
		if ($('#bannerRotationCollection').cycle) {
			$('#bannerRotationCollection').cycle('resume');
		}
		$('#navPrimary > li.currentDisabled').removeClass('currentDisabled').addClass('current');
		$('#navPrimary li').removeClass('currentNav');
	}
});

// 1. Add events to gen 0 main nav that create & reveal first generation primary nav menu in modal div below "menu bar"
// 2. Add gen2 build event to gen1's elements that have children
$('#navPrimary > li > a').click(function(event) {
	if ($(this).siblings('ul').length > 0) {
		event.preventDefault();

		if ($(this).parent().hasClass('currentNav')) {
			$('#navPrimaryChildModal').slideUp();
			$('#navPrimary > li.currentDisabled').removeClass('currentDisabled').addClass('current');
			$('#navPrimary li').removeClass('currentNav');
			$(this).blur();
			if ($('#bannerRotationCollection').cycle) {


				$('#bannerRotationCollection').cycle('resume');
			}
		}
		else {
			if ($('#bannerRotationCollection').cycle) {
				$('#bannerRotationCollection').cycle('pause');
				$('#bannerRotationCollection .bannerRotation').css('z-index', 1);

			}
			$('#navPrimary > li.current').removeClass('current').addClass('currentDisabled');
			$('#navPrimaryChildModal').slideDown();
			$(this).blur();
			$("#navPrimaryChildModal .genWrap div[class*='gen']").hide(0).empty();
			$(this).siblings('ul').clone(true).appendTo('#navPrimaryChildModal .gen1');
			$('#navPrimaryChildModal .gen1').show('slow');
			$(this).parent().siblings().removeClass('currentNav');
			$(this).parent().addClass('currentNav');
			
			// Add an indicator that the gen2 generation of menu children is present
			$('#navPrimaryChildModal .gen1 ul li > a').each(function() {
				if ($(this).siblings('ul').length > 0) {
					$(this).append(' &raquo;');
				}
			});
		
			$('#navPrimaryChildModal .gen1 ul li > a').click(function(event) {
				if ($(this).siblings('ul').length > 0) {
					event.preventDefault();
					$('#navPrimaryChildModal .gen2').hide(0).empty();
					$(this).siblings('ul').clone(true).appendTo('#navPrimaryChildModal .gen2');
					$('#navPrimaryChildModal .gen2').append('<div class="gen2Bottom"></div>');
					$(this).parent().siblings().removeClass('currentNav');
					$(this).parent().addClass('currentNav');
					$(this).blur();
					
					var gen2Count = $('#navPrimaryChildModal .gen2 ul li').length;
					if (gen2Count > 6) {
						var col2StartIndex = gen2Count - parseInt(gen2Count / 2) - 1;
						$('#navPrimaryChildModal .gen2 ul').after('<ul></ul>');
						$('#navPrimaryChildModal .gen2 ul').addClass('twoCol');
						$('#navPrimaryChildModal .gen2 ul:last').append($('#navPrimaryChildModal .gen2 ul:first li:gt(' + col2StartIndex + ')'));
					}
					
					$('#navPrimaryChildModal .gen2').show('slow');
				}
			});
		}
	}
});


// If there is a user-entered footer navigation column (move into the correct place on the page)
// CMS can't do the placement into the HTML; must place it outside of #navFooter
$(document).ready(function() {
	if ($('#navFooterUserColumn').length) {
		$('#navFooterUserColumn').appendTo('#navFooter');
		var columnCountClass = 'columns' + $('#navFooter div.subSiteNavBlock').length;
		$('#navFooter')[0].className = $('#navFooter')[0].className.replace(/\bcolumns[0-9]+\b/g, columnCountClass);
	}
});

// Add alternate row formatting and hover behavior to tables
$('#contentPrimary table[class!=calendarGrid] tbody tr:odd,#contentSecondary table[class!=calendarGrid] tbody tr:odd').addClass('altRow');
$('#contentPrimary table[class!=calendarGrid] tbody tr,#contentSecondary table[class!=calendarGrid] tbody tr').hover(
	function() {
		$(this).addClass("highlight");
	},
	function() {
		$(this).removeClass("highlight");
	}
);
