$( document).ready(function(){
	if ( (!$.browser.msie) || ($.browser.version >= 7.0)) // leave everything alone if browser is IE6
	{
		var header_visible = true;
		var psuedo_history_hash_prefix = 'nav'; // prefixed in URL, e.g. domain.com/foo#nav:bar as alias to domain.com/foo#bar
		var top_of_page_id = 'top-of-page'; // visible in URL

		var use_psuedo_history = false;
		var part_to_hide_height = $('div#hd div.part-to-hide').height(); // 93
		var part_to_reveal_height = $('div#hd div.part-to-toggle div.part-to-reveal').height(); // 19
		var part_to_toggle = $('div#hd div.part-to-toggle');
		var speed_of_animation = 500;
		var header = $('div#hd');
		header.is_animating = false;

		// we'll probably want to do this in CSS, but for now, do it programmatically
		part_to_toggle.css('overflow','hidden');
		part_to_toggle.css('height', part_to_hide_height + 'px');
		function hide_header_show_tab( instantly) {
			if (( !header.is_animating) && (header_visible))
			{
				if ( ($(window).height() < ($(document).height() - (part_to_hide_height))))
				{
					var header_width = header.width(); // setting position to fixed loses 100% width

					header.width( header_width);

					header.css('position', 'fixed');
					header.css('top', '0' + 'px');

					header_visible = false;

					// add a spacer to prevent the content from moving up
					// console.log('adding spacer: ' + part_to_hide_height + ' - ' + part_to_reveal_height);
					$('div#hd-space').height( part_to_hide_height - part_to_reveal_height);

					// set the container of both hiding/revealing parts to the height of the element being revealed
					part_to_toggle.height( part_to_reveal_height);
					// scroll so only the bottom is visible (but not the part to be revealed, yet)
					part_to_toggle.scrollTop( part_to_hide_height - part_to_reveal_height);

					// animate -- scroll to reveal
					if ( instantly == true) {
						part_to_toggle.scrollTop( part_to_hide_height);
						check_window_scroll_top();
					} else {
						header.is_animating = true;
						part_to_toggle.animate(
							{
								// height: part_to_reveal_height,
								scrollTop: part_to_hide_height
							},
							speed_of_animation,
							null,
							function() { header.is_animating = false; check_window_scroll_top();}
						);
					}
					// console.log('space height: ' + (part_to_hide_height - part_to_reveal_height) + " toggle height: " + part_to_reveal_height +
					// 	" part_to_hide_height: " + part_to_hide_height);
				}
			}
		}

		function show_tab_hide_header() {
			if ( (!header_visible) && ( !header.is_animating))
			{
				header.css('position', 'static');
				header.css('top', 'auto');

				// "turn off" the spacer (animating it to 0 doesn't look right)
				$('div#hd-space').height(0);
				// $('div#hd-space').animate( { height: 0}, speed_of_animation);

				// animate the toggle area to its original height and scroll to top
				header.is_animating = true;
				part_to_toggle.animate(
					{
						height: part_to_hide_height,
						scrollTop: 0
					},
					speed_of_animation,
					function() { header.is_animating = false; check_window_scroll_top();}
				);

				header_visible = true;
			}
		}

		function check_window_scroll_top( instantly) {
			var top = $(window).scrollTop(); // dimensions is now part of core
			if (( ($('div#hd div.part-to-keep').position().top - part_to_reveal_height) <= top) && ( header_visible)) {
				hide_header_show_tab( instantly);
			} else if ( top < $('div#hd').height()) {
				show_tab_hide_header();
			}
		}

		$(window).scroll( function( e) {
			check_window_scroll_top();
		});

		// expects anchor name to contain leading #, e.g. #foo
		function remove_psuedo_history_prefix_from_anchor( anchor_name)
		{
			if ( typeof(anchor_name) =='string')
			{
				var anchor_parts = anchor_name.split(':');
				if ( anchor_parts.length > 1)
				{
					anchor_name = anchor_parts[anchor_parts.length - 1];
					if ( anchor_name == '') anchor_name = top_of_page_id;
					anchor_name = '#' + anchor_name;
				} else if ( anchor_name == '#') anchor_name = '#' + top_of_page_id;
				return anchor_name;
			} else
				return '#' + top_of_page_id;
		}
		// expects anchor name to contain leading #, e.g. #foo
		function scroll_to_named_anchor( anchor_name, scrolling_on_load)
		{
			var top_offset = 0;

			// console.log('scroll_to_named_anchor: ' + anchor_name);
			anchor_name = remove_psuedo_history_prefix_from_anchor( anchor_name);
			// console.log('rewrote anchor names as: ' + anchor_name);
			// console.log( "-----------");
			anchor = $(anchor_name);
			if ( anchor.position())
			{
				if ( (header_visible) || (scrolling_on_load)) {
					// console.log('part to keep: ' + $('div#hd div.part-to-keep').height() + ' part to reveal: ' + part_to_reveal_height + ' part to hide: ' + part_to_hide_height);
					top_offset =  $('div#hd div.part-to-keep').height() + /*part_to_hide_height - */part_to_reveal_height * 2;
					top_offset = 103;
				} else {
					// console.log('part to keep: ' + $('div#hd div.part-to-keep').height() + ' part to reveal: ' + part_to_reveal_height);
					top_offset = ($('div#hd div.part-to-keep').height()) + part_to_reveal_height;
				}
				// console.log('top offset: ' + top_offset);
				if (scrolling_on_load == true) {
				 	$('html,body').scrollTop( anchor.position().top - top_offset);
					// if ( anchor_name != ('#' + top_of_page_id)) {
					// 	hide_header_show_tab();
					// }
				} else {
					$('html,body').animate( {scrollTop: anchor.position().top - top_offset}, 500);
				}
			}
		}

		// for any link clicked on with a class of "jump," scroll to correct position (and don't allow normal behavior)
		$("a.jump").click( function( e) {
			var anchor = e.target.href.replace(/^.*#/, ''); //e.target.href.split('#');

			// if there is actually a local anchor or id to go to, and
			// if we're not on the home page, the anchor is not the top-of-page link
			if (($('#' + anchor + ',a[name=' + anchor + ']').length > 0) &&
				((window.location.pathname == '/') || (window.location.pathname == '/index.php') || (anchor != 'top-of-page')))
			{
				if ( use_psuedo_history) {
					// console.log('history load: ' + psuedo_history_hash_prefix + ':' + anchor);
		        	$.history.load( psuedo_history_hash_prefix + ':' + anchor);
				} else {
					// console.log('setting location: ' + '#' + psuedo_history_hash_prefix + ':' + anchor);
					window.location.hash = '#' + psuedo_history_hash_prefix + ':' + anchor; //
					scroll_to_named_anchor( '#' + anchor);
				}
				e.preventDefault();
				return false;
			}
			return true;
		});

		function go_to_point_in_history( anchor)
		{
			// console.log( "-----------");
			// console.log("going to point in history: " + anchor);
			if ( (typeof(anchor) != 'string') || ( anchor == '')) anchor = top_of_page_id;
			scroll_to_named_anchor('#' + anchor);
		}
		// jquery.history
		if (( $.browser.msie) && ( $.browser.version < 8.0))
		{
			use_psuedo_history = true;
	    	$.history.init(go_to_point_in_history);
		}
	    // $("a[rel='history']").click(function(){
	    // 	        $.history.load(this.href.replace(/^.*#/, ''));
	    // 	        return false;
	    // });

		// if we've come in by way of http://url#anchor_name, scroll to the correct position
		if ( window.location.hash) {
			scroll_to_named_anchor( window.location.hash, true);
			check_window_scroll_top( true);
			// hide_header_show_tab();

			if ( use_psuedo_history)
			{
				// we remove the psuedo prefix since it may or may not be there and we want to add it
				var anchor_name = remove_psuedo_history_prefix_from_anchor( window.location.hash);
        		$.history.load( psuedo_history_hash_prefix + ':' + anchor_name.replace(/^.*#/, ''));
			}
		} else
		{
			check_window_scroll_top();
			if ( use_psuedo_history)
        		$.history.load( psuedo_history_hash_prefix + ':' + top_of_page_id);
		}
	} // if browser is not msie 6
});

$(document).ready(function(){
	$(".colorbox").colorbox();
    });

// accomplishments navigation
$(document).ready(function(){
	if ( $('.accomp_super_nav').length)
	{
		var	super_nav_lock = true; // set this to false to get the initial roll over behavior
		var sub_sub_list_body_left_offset = 0; // set this to 40 or so to offset to the right
		function query_string_value_by_key( key )
		{
		  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		  var regexS = "[\\?&]"+key+"=([^&#]*)";
		  var regex = new RegExp( regexS );
		  var results = regex.exec( window.location.href );
		  if( results == null )
		    return "";
		  else
		    return results[1];
		}

		function hover_super_nav_li(li, click) {
			$(li).siblings().each( function()
			{
				if ( (!super_nav_lock) || (click)) {
						$(this).removeClass('hover');
						$('.accomp_super_nav ul.sub_list > li.sub_sub_list').each( function() {
							$(this).removeClass('hover');
						});
				}
			});
			if ( (!super_nav_lock) || (click)) {
				$(li).addClass('hover');
			}
		}

		var which_li = parseInt(query_string_value_by_key('idx'));
		if ( which_li > 0)
		{
			hover_super_nav_li( $('.accomp_super_nav > li').eq( which_li-1), true);
		}

		$('.accomp_super_nav > li').mouseenter( function( e) { hover_super_nav_li( this);});

		$('.accomp_super_nav > li > h3, .accomp_super_nav > li > h3 > a').click( function( e) {
				hover_super_nav_li( $(this).parents('li')[0], true);
				super_nav_lock = true;
				return false;
			});

		$('.accomp_super_nav ul.sub_list > li.sub_sub_list').click( function(e) {
			var highest_top = $(this).position().top;
			super_nav_lock = true;
			if ( !$(this).hasClass("hover"))
			{
				$(this).siblings().each( function()
				{
					$(this).removeClass('hover');
					$(this).css('zIndex', 0);
					if ( $(this).position().top < highest_top)
						highest_top = $(this).position().top;
				});
				$(this).addClass('hover');
				var new_top = (highest_top - $(this).position().top);
				// comment out the line below to revert to the original behavior (expanded items positioned at top of list)
				new_top = 0;

				$(this).children('.sub_list_body').css({top: (new_top + 15)+ 'px', left: sub_sub_list_body_left_offset + 'px', opacity:  0.5});
				$(this).css('zIndex', 1100);
				$(this).children('.sub_list_body').animate({'opacity':1.0, top: new_top + 'px'},125);
			} else
			{
				var li = this;
				$(this).children('.sub_list_body').animate({'opacity':.5, top: '+=15px'},125, null, function() {
					$(li).removeClass('hover');
				});
				// $(this).removeClass('hover');
			}
		});
	}
});


$(window).load(function() {
	if (window.location.href.search('start_feature=1') >= 0) {
	    $(".top-feature .size1of2 a.colorbox").click();
	}
    });


/* ================================================================ */
$(function() {
$('#tabs').tabs();
});

$(function() {
$('#tabs-organize').tabs();
});

$(function() {
$('#tabs-sub1').tabs();
});

$(function() {
$('#tabs-sub2').tabs();
});

$(function() {
$('#tabs-sub3').tabs();
});

/*
function show_tab (ev, num) {
    $(".ui-tabs-panel").addClass ("ui-tabs-hide");
    $(".ui-tabs-nav li").removeClass ("ui-tabs-selected");
    var id = "#tabs-"+num;
    $(id).removeClass ("ui-tabs-hide");

    $(ev.target).parent().addClass("ui-tabs-selected");
}

$( ".ui-tabs-nav a" ).each(
	function (intIndex){
		$(this).bind (
			"click",
			function(ev){
			show_tab (ev , intIndex + 1);
			return false;
			}
		)

	}

);


$(function () {

	$("#tab1").click (function (ev) {show_tab (ev, 1); return false;});
	$("#tab2").click (function (ev) {show_tab (ev, 2); return false;});
	$("#tab3").click (function (ev) {show_tab (ev, 3); return false;});
	$("#tab4").click (function (ev) {show_tab (ev, 4); return false;});
	$("#tab5").click (function (ev) {show_tab (ev, 5); return false;});
	$("#tab6").click (function (ev) {show_tab (ev, 6); return false;});
	$("#tab7").click (function (ev) {show_tab (ev, 7); return false;});
	$("#tab8").click (function (ev) {show_tab (ev, 8); return false;});
	$("#tab9").click (function (ev) {show_tab (ev, 9); return false;});
    });
*/

