$(document).ready(function(){
	$('.help').context_help();
});

jQuery.fn.context_help = function() {
	// Hide the precursor text in help text
	$('#help-tooltips strong').hide();

	// Hide the help text
	$('#help-tooltips').hide();

	// Set up a live function to handle the close buttons as they are dynamically created
	$('#close-help').live('click', function(){
		$(this).prevAll('.help').eq(0).focus();
		$('#context-help, #close-help').remove();
		return false;
	});
	
	// Setup the click function for each element passed in
	return $(this).each(function(){
		$(this).click(function(){
			// Remove any open instances of help
			$('#context-help, #close-help').remove();
		
			// Save the reference to the help button, get the href, and the content of the linked <p> element
			var $button = $(this);
			var href = $button.attr('href');
			var content = $(href).html();
			
			// Create the help item and insert it after the the help button in DOM order
			$button.after('<div id="context-help">'+content+'</div>');
			var $help = $('#context-help');
			
			// Create the close button and insert it after the help text in DOM order
			$help.after('<a href="#context-help" id="close-help"><img src="FILE/Apps/FixedRoute/CustomerInfo/images/close.png" alt="Exit Help" /></a>');
			var $close = $('#close-help');

			// Get the top and left position of the help button for positioning the close button and help text, also set the tabindex of the help text to make it focusable

			var $position = $button.position();

			$help
				.css("top",($position.top - 7) + "px")
				.css("left",($position.left + 19) + "px")
				.css("z-index", 100)
				.show();

			$close
				.css("top",($position.top - 7) + "px")
				.css("left",($position.left) + "px")
				.show();

			var help_el = document.getElementById('context-help');
			help_el.tabIndex = -1;
			help_el.focus();

			return false;
		});
	});
};
