$(function() {
	//function to handle ajax content in jquery ui dialogs:
	// Dependency: js/libs/jquery-ui-1.8.16.custom.min.js
	$('a.ajax').live('click', function() {
		var url = this.href;

		var dialog = $("#dialog");
		if ($("#dialog").length == 0) {
			dialog = $('<div id="dialog" style="clear:both; display:hidden"></div>').appendTo('body');
		}
		// load remote content
		dialog.load(
		url, {},
		function(responseText, textStatus, XMLHttpRequest) {
			dialog.dialog({
				open: function(e, ui) {
					var w = ($('#dialog').find('.overlay').width() + 40);
					$(this).closest('.ui-dialog').css({
						position: 'absolute',
						top: '150px',
						width: w
					});
				},
				modal: true
			});
		});
		//prevent the browser to follow the link
		return false;
	});

	// functions to handle the submenus on the main nav
	//submenu:
	$("#nav li").hoverIntent({
		over: makeTall,
		timeout: 100,
		out: makeShort
	});


	function makeTall() {
		$('ul.sub-nav', this).slideDown(200);
	}


	function makeShort() {
		$('ul.sub-nav', this).slideUp(200);
	}

	//right flyout menu
	$("#nav li ul li.show-sub-right").hoverIntent({
		over: flyOut,
		timeout: 100,
		out: flyIn
	});


	function flyOut() {
		$('.sub-sub-nav-right', this).show('slide', 200);
	}


	function flyIn() {
		$('.sub-sub-nav-right', this).hide('slide', 200);
	}

	//left flyout menu
	$("#nav li ul li.show-sub-left").hoverIntent({
		over: flyLeft,
		timeout: 100,
		out: flyInLeft
	});


	function flyLeft() {
		$('.sub-sub-nav-left', this).show('slide', {
			direction: 'right'
		},
		200);
	}


	function flyInLeft() {
		$('.sub-sub-nav-left', this).hide('slide', {
			direction: 'right'
		},
		200);
	}

	// function to pass hashchage events to browser history. Dependency: /js/libs/jquery.bbq.js

	var tabs = $('#tabs'),
		tab_a_selector = 'ul.ui-tabs-nav a';
	tabs.tabs({
		event: 'change'
	});
	tabs.find(tab_a_selector).click(function() {
		var state = {},
			id = $(this).closest('#tabs').attr('id'),
			idx = $(this).parent().prevAll().length;
		state[id] = idx;
		$.bbq.pushState(state);
	});

	$(window).bind('hashchange', function(e) {
		tabs.each(function() {
			var idx = $.bbq.getState(this.id, true) || 0;
			$(this).find(tab_a_selector).eq(idx).triggerHandler('change');
		});
	});
	$(window).trigger('hashchange');

	//forms:
	//simple function for making errors on form text fields disappear on focus
	$('input').focus(function() {
		$(this).parent().find('.field_error').fadeOut('fast');
	});
	// different function for select menus, because they're nested differently
	$('select').focus(function() {
		$(this).parent().find('.field_error').fadeOut('fast');
	});

	// page height scripts: These scripts resolve the various UI conflicts with regards to left nav and content well height.
	// this is neccessary in order to keep site gradient styles consistent with elements expanding and contracting vertically
	// this function sets the two columns to the same height when the page loads based on the tallest column.
	$.fn.setAllToMaxHeight = function() {
		return this.height(Math.max.apply(this, $.map(this, function(e) {
			return $(e).height();
		})));
	};
	$('.column').setAllToMaxHeight();

	// this function is for adjusting the heights of the columns on tabbed pages where the contentArea grows larger than leftNav
	// hooks into jquery tabs function after tab has loaded contents
	$.fn.adjustHeights = function() {
		var tabHeight = $('#tabs_content').css('height', '').height();
		var leftHeight = $('#leftNav').css('height', '').height();
		if (tabHeight >= leftHeight) {
			setTimeout(function() {
				$('#contentArea').css('height', tabHeight + 300 + 'px');
			},
			10);
		} else if (tabHeight < leftHeight) {
			setTimeout(function() {
				$('#contentArea').css('height', leftHeight + 150 + 'px');
			},
			10);
		} else {
			$('#contentArea').css('height', leftHeight + 180 + 'px');
		}
	};

	$('#tabs').bind('tabsload', function(event, ui) {
		$(this).adjustHeights();
	});

	// 	function for the left nav accordion menus 
	// this also sets a cookie to maintain the open state of the accordion
	// Dependency: /js/libs/jquery.cookies.js
	var accordion = $("#leftNav");
	var index = $.cookie("accordionActiveIndex");
	accordionActiveIndex = parseInt(index, 10);
	var active;
	if (index !== null) {
		active = accordion.find(".head:eq(" + index + ")");
	} else {
		active = false;
	}
	$("#leftNav").accordion({
		collapsible: true,
		header: ".head",
		event: "click",
		active: active,

		change: function(event, ui) {
			$('.column').setAllToMaxHeight();
			var index = $(this).find(".head").index(ui.newHeader[0]);
			$.cookie("accordionActiveIndex", index, {
				path: "/"
			});
		},
		autoHeight: false
	});

	// This is where the tabs function goes. temporarily placed on the individual html documents

	// function for exposing the accordion-like hidden areas onclick of blue arrows/text links (see careers page)
	$('a.expose-link').click(function() {
		var exposeHeight = $(this).parent().find('.expose').css('height', '').height();
		var contentHeight = $('#contentArea').css('height', '').height();
		$(this).parent().find('.expose').slideToggle('fast');
		$(this).toggleClass('down');
		if ($(this).hasClass('down')) {
			$('#contentArea').css('height', contentHeight + exposeHeight + 'px');
		} else {
			$('#contentArea').css('height', tabHeight + 'px');
		}
	});

	// global nav highlighting based on body ID and nav li class:
	var bodyID = $("body").attr("id");
	// check if body id / global nav li class match, add active class to the matching li	
	// every page must have an id on the body tag.
	$("li[class$='" + bodyID + "']").toggleClass('active');

	$(".toggleRow").click(function() {
		$(this).toggleClass("highlight");
	});

});

