// JavaScript Document

/* ==================================================================================================== */
// jQuery stuff

$(document).ready(function () {

	/* ==================================================================================================== */
	// Communicate JavaScript availability to relevant stylesheet declarations to allow for more graceful failure.
	// Requires: jQuery

	$('html').addClass('testClass_javascriptAvailable');
	$('html').removeClass('testClass_javascriptNotAvailable');

	/* ==================================================================================================== */
	// Top navbar
	// Requires: jQuery
	// Requires: hoverIntent.js

	// Define "over" function for hoverIntent()
	function open_topnavDropdownMenu() {

		var jMasthead_menuItem = $(this);
		var jMasthead_menuItemLink = $(this).find('a.masthead_menuItemLink');
		var jMasthead_sub01MenuWrapper01 = $(this).find('div.masthead_sub01MenuWrapper01');

		// Remove expandable icon back
		jMasthead_menuItem.addClass('masthead_menuItem_menuOpen');

		// Open menu div for sublevel links
		jMasthead_sub01MenuWrapper01.addClass('masthead_sub01MenuWrapper01_menuOpen');

		// Add new default state for toplevel link
		jMasthead_menuItemLink.addClass('masthead_menuItemLink_menuOpen');

		return false;
	}

	// Define "out" function for hoverIntent()
	function close_topnavDropdownMenu() {

		var jMasthead_menuItem = $(this);
		var jMasthead_menuItemLink = $(this).find('a.masthead_menuItemLink');
		var jMasthead_sub01MenuWrapper01 = $(this).find('div.masthead_sub01MenuWrapper01');

		// Add expandable icon back
		jMasthead_menuItem.removeClass('masthead_menuItem_menuOpen');

		// Close menu div for sublevel links
		jMasthead_sub01MenuWrapper01.removeClass('masthead_sub01MenuWrapper01_menuOpen');

		// Remove new default state for toplevel link
		jMasthead_menuItemLink.removeClass('masthead_menuItemLink_menuOpen');

		return false;
	}

	// Define settings and for hoverIntent() and call
	var megaConfig = {
		sensitivity: 100,
		interval: 100,
		over: open_topnavDropdownMenu,
		timeout: 0,
		out: close_topnavDropdownMenu
	}
	// Initialize hoverIntent
	$('li.masthead_menuItem').hoverIntent(megaConfig);

	/* ==================================================================================================== */
	// Side navbar script
	// Requires: jQuery

	// Store top-level link's intended browser behavior for the first click, to prevent default for the first click that opens sub-menus.
	var firstClick = new Object;

	$('a.sidebarPanel_menuItemLink').each(
		function () {
			var theText = $(this).text();
			firstClick[theText] = true;
		}
	);

	// On mousedown event...
	$('li.sidebarPanel_menuItem').bind('mousedown', function () {

		// Cache frequent DOM queries...
		var jSidebarPanel_menuItem = $(this);
		var jSidebarPanel_menuItemLink = $(this).find('a.sidebarPanel_menuItemLink');
		var jSidebarPanel_menuItemLink_wildcard01 = $(this).find('span.sidebarPanel_menuItemLink_wildcard01');
		var jSidebarPanel_sub01MenuWrapper01 = $(this).find('div.sidebarPanel_sub01MenuWrapper01');

		// Prevent default browser link behavior for first click on top-level links AND only if there is a submenu present...
		jSidebarPanel_menuItemLink.bind('click', function (e) {
			if (firstClick[jSidebarPanel_menuItemLink.text()] == true && jSidebarPanel_menuItemLink.children('span').hasClass('sidebarPanel_menuItemLink_wildcard01')) {
				e.preventDefault();
				firstClick[jSidebarPanel_menuItemLink.text()] = false;
			}
		});

		// Add "menuOpen" styles...
		jSidebarPanel_menuItem.addClass('sidebarPanel_menuItem_menuOpen');
		/* Apply this "menuOpen" style ONLY if top-level link has a nested submenu list (i.e., if it has the 'expandable' menu dingbat)... */
		if (jSidebarPanel_menuItemLink.children('span').hasClass('sidebarPanel_menuItemLink_wildcard01')) {
			jSidebarPanel_menuItemLink.addClass('sidebarPanel_menuItemLink_menuOpen');
		}
		jSidebarPanel_menuItemLink_wildcard01.addClass('sidebarPanel_menuItemLink_wildcard01_menuOpen');
		jSidebarPanel_sub01MenuWrapper01.addClass('sidebarPanel_sub01MenuWrapper01_menuOpen');

		return false;
	});

	// $('ol.sidebarPanel_menuList').mouseenter(function(){alert('mouse enter');});
	// $('ol.sidebarPanel_menuList').mouseleave(function(){alert('mouse leave');});
	$('ol.sidebarPanel_menuList').bind('mouseleave', function () {

		// Cache frequent DOM queries
		var jSidebarPanel_menuItem = $(this).find('li.sidebarPanel_menuItem');
		var jSidebarPanel_menuItemLink = $(this).find('a.sidebarPanel_menuItemLink');
		var jSidebarPanel_menuItemLink_wildcard01 = $(this).find('span.sidebarPanel_menuItemLink_wildcard01');
		var jSidebarPanel_sub01MenuWrapper01 = $(this).find('div.sidebarPanel_sub01MenuWrapper01');

		// Reset first-click behavior for top-level links...
		$('a.sidebarPanel_menuItemLink').each(
			function () {
				var theText = $(this).text();
				firstClick[theText] = true;
			}
		);

		// Remove "menuOpen styles...
		jSidebarPanel_menuItem.removeClass('sidebarPanel_menuItem_menuOpen');
		jSidebarPanel_menuItemLink.removeClass('sidebarPanel_menuItemLink_menuOpen');
		jSidebarPanel_menuItemLink_wildcard01.removeClass('sidebarPanel_menuItemLink_wildcard01_menuOpen');
		jSidebarPanel_sub01MenuWrapper01.removeClass('sidebarPanel_sub01MenuWrapper01_menuOpen');

		return false;

	});

	/* ==================================================================================================== */
	// DDSlider carousel slider
	// Requires: jQuery
	// Requires: jquery.easing.1.3.js
	// Requires: jquery.DDSlider.min.js

	$('#ddslider01').DDSlider({
		// nextSlide: '.ddslider01_arrowRight',
		// prevSlide: '.ddslider01_arrowLeft',
		selector: '.ddslider01_selector',

		duration: 500
	});


	/* ==================================================================================================== */
	// Footer02 -- custom scroll bars
	// Requires: jQuery
	// Requires: jquery.jscrollpane.js
	// Requires: jquery.mousewheel.js

	$('.jScrollPane').jScrollPane({
		verticalGutter: 1
	});

	/* ==================================================================================================== */
	// Expandable list_Fancy01 with profiles and headshots
	// Requires: jQuery

	jQuery.fn.toggle_listFancyProfiles = function (speed, easing, callback) {

		var jThis = this;
		var jListItem_fancy01 = this.parents('.listItem_fancy01');
		var jHeadshot = this.parents('.listItem_fancy01').children('.headshot');

		if (jThis.is(':hidden')) {
			jListItem_fancy01.addClass('expanded'); // Add class to allow CSS changes to styles if expanded.
			// jHeadshot.css('display','block');
			jHeadshot.addClass('headshot_open');
			this.slideDown(200).fadeTo(speed, 1, function () {
				jHeadshot.fadeTo('slow', 1);
			});
		} else {
			jListItem_fancy01.removeClass('expanded'); // Remove class to allow CSS changes to styles if collapsed.
			jHeadshot.removeClass('headshot_open');
			jHeadshot.fadeTo(100, 0);
			this.fadeTo(100, 0).slideUp(200);
		}
		return false;
	};

	$('.expand_listFancyProfiles').click(function () {
		$('.expandable_wrapper02').toggle_listFancyProfiles();
		return false;
	});

	/* THIS WORKS
	jQuery.fn.toggle_listFancyProfiles = function(speed, easing, callback) {
	if (this.is(':hidden')) {
	return this.slideDown(200, easing).fadeTo(speed, 1, easing, callback);
	} else {
	return this.fadeTo(100, 0, easing).slideUp(200, easing, callback);
	}
	};
	
	$('.expand_listFancyProfiles').click(function() {
	$('.expandable_wrapper02').toggle_listFancyProfiles();
	return false;
	});
	*/


	/* ==================================================================================================== */
	// Modal window for solutions videos: Fancybox
	// Requires: jQuery

	/* This is basic - uses default settings */
	$('.modalWindow_Fancybox').fancybox();

	/* Using custom settings */
	$('.modalWindow_Fancybox').fancybox({
		'hideOnContentClick': true
	});

	/* Apply fancybox to multiple items */
	$('a.group').fancybox({
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'speedIn': 600,
		'speedOut': 200,
		'overlayShow': false
	});

	/* ==================================================================================================== */
	// Colorbox
	// Requires: jQuery

	$('.colorbox01').colorbox( // This is used for "Related Resources"
		{
		inline: true,
		innerWidth: 418,
		maxHeight: "75%",
		transition: "elastic",
		opacity: 0.85,
		speed: 350
	}
	);
	$('.colorbox02').colorbox( // This is used for "Featured Resources"
		{
		inline: false,
		innerWidth: 418,
		maxHeight: "75%",
		transition: "elastic",
		opacity: 0.85,
		speed: 350
	}
	);

	/* ==================================================================================================== */
	// Dynamically adjust positioning of browser-frame-mounted position:fixed buttons (widget buttons)
	// Requires: jQuery

	var jWidgets_frameRight_wrapper01 = $('.widgets_frameRight_wrapper01');
	var jWidgets_frameLeftSocial_wrapper01 = $('.widgets_frameLeftSocial_wrapper01');
	var jWidgets_frameLeftSocial_list = $('.widgets_frameLeftSocial_list');
	var widgetButtonFrameRightHeight = 0;
	var widgetFrameLeftSocialHeight = 0; // The social widget height is based on the ul/ol list tag not the anchor link tags.

	// Frame Right
	jWidgets_frameRight_wrapper01.find('.widgetButtons').each(function () {
		widgetButtonFrameRightHeight += $(this).height();
	});

	widgetButtonFrameRightHeight = widgetButtonFrameRightHeight / 2;
	jWidgets_frameRight_wrapper01.css('margin-top', -widgetButtonFrameRightHeight);
	jWidgets_frameRight_wrapper01.hide().css('visibility', 'visible').fadeIn(1000);

	// Frame Left Social
	widgetFrameLeftSocialHeight += jWidgets_frameLeftSocial_list.height() / 2;
	jWidgets_frameLeftSocial_wrapper01.css('margin-top', -widgetFrameLeftSocialHeight);
	jWidgets_frameLeftSocial_wrapper01.hide().css('visibility', 'visible').fadeIn(1000);

	jWidgets_frameLeftSocial_wrapper01.find('.widgetButtonsSocialListItem').each(function () {
		$(this).hover(
			function () {
				jWidgets_frameLeftSocial_list.addClass('widgets_frameLeftSocial_listHover');
			},
			function () {
				jWidgets_frameLeftSocial_list.removeClass('widgets_frameLeftSocial_listHover');
			}
		);
	});
});
