function showMenuItem(i) {
	jQuery('#nav ul ul').hide();
	jQuery(this).next('ul').show();
}
function hideMenuItem(i) {
	return false;
}

$(document).ready(function() {
	// ============
	// = Menu Hover Intent =
	// ============
	jQuery('#nav ul.group>li>a').hoverIntent(showMenuItem, hideMenuItem);

	jQuery('#nav ul ul').hide().bind('mouseleave', function(){
		jQuery(this).fadeOut(200);
	});


	// ============
	// = Religion =
	// ============
	if($('#religion').length>0) {
		$('.religion-copy').hide();
		$('.religion h3').css('cursor','pointer').css('margin', 0).click(function() {
			$(this).parent().find('.religion-copy').slideToggle();
		});
	}
	
	
	// ===================
	// = Events Calendar =
	// ===================
	if($('#recent-events').length>0) {
		
		// recent events
		$('#recent-events li .event-copy').hide();
		$('#recent-events li h3').css('cursor','pointer').click(function() {
			$(this).next().slideToggle();
		});
		
		// past events
		$('#past-events ol li .event-copy').hide();
		$('#past-events ol li h3').css('cursor','pointer').click(function() {
			$(this).next().slideToggle();
		});
	}
	
	
	
	// =======================
	// = Request Information =
	// =======================
	if($('#request-information').length>0) {
		
		// we need to mark the form as enhanced
		$('.form').addClass('enhanced');
		
		// first we'll take care of the accordion
		$('.toggle-content').hide();
		$('h2.toggle').css('cursor','pointer').css('margin-bottom',0).click(function() {
			// $('.toggle-content').slideUp();
			$(this).next().slideToggle();
			return false;
		});
		
		// now we need to enhance the labels
		
		// let's hide the labels and move them inside the inputs
		$('.enhanced form .textfield label').hide();
		$('.enhanced form .textarea label').hide();
		$('.textfield').each(function() {
			$(this).find('input').attr( 'value', $(this).find('label').text() );
		});
		$('.textarea').each(function() {
			$(this).find('textarea').val( $(this).find('label').text() );
		});

		// let's clear out the value when clicked
		$('.textfield input').focus(function() {
			if( $(this).attr('value')==$(this).parent().find('label').text() ) {
				$(this).attr('value', '');
			}
		});
		$('.textfield input').blur(function() {
			if( $(this).attr('value')=='' ) {
				$(this).attr( 'value', $(this).parent().find('label').text() );
			}
		});
		$('.textarea textarea').focus(function() {
			if( $(this).val()==$(this).parent().find('label').text() ) {
				$(this).val('');
			}
		});
		$('.textarea textarea').blur(function() {
			if( $(this).val()=='' ) {
				$(this).val( $(this).parent().find('label').text() );
			}
		});
	}
	
	
	
	// ==============
	// = The Legacy =
	// ==============
	if($('#legacy-archive').length>0) {
		$('.legacy-details a').hover(
			function() {
				$('#tocholder').html($(this).parent().parent().find('.toc').html());
			},
			function() {
				$('#tocholder').html(' ');
			}
		);
	}
	
	// ================
	// = Body Diagram =
	// ================
	if($('#body-diagram').length>0) {
		
		$('#body-diagram img').hide(); // just get rid of it for now...
		$('#reference-points li').css('visibility','hidden').css('position','absolute');
		
		// prep the data
		$('#body-diagram').append('<ul id="hooks"></ul>');
		newlist = '';
		allpoints = $('ol#reference-points li').length;
		for (var i=1; i <= allpoints; i++) {
			newlist = newlist + '<li class="hook point" id="hook' + i + '"><a href="#">Hook ' + i + '</a></li>';
		};
		$('#hooks').append(newlist);
		$('#hooks').append( $('#hook3').clone().attr('id', 'hook3b' ) );
		$('#hooks').append( $('#hook10').clone().attr('id', 'hook10b' ) );
		
		// hook the hooks
		$('li.hook a').hover(
			function() {
				hookindex = $(this).parent().attr('id').replace(new RegExp("hook"), "").replace(new RegExp("b"), "");
				hookindex = hookindex - 1;
				$('#reference-points li:eq(' + hookindex + ')').css('visibility','visible');
			},
			function() {
				$('#reference-points li').css('visibility','hidden');
			}
		);
		
	}
	
});
