function prepare_hover_menus() {
	// ensure the page contains a hover menu
	if(!$('body#newsroom #navigation')) {
		return;
	}

	// hide all links and set the appropriate class
	$('body#newsroom #navigation li.page_item ul').hide().addClass('hidden').each(function (){
		// add convenience class
		var container = $(this).parent('.page_item');
		container.addClass('parent-item');
		
		// insert span to hold triangle
		var first_li = $(this).children(':first-child');
		first_li.prepend('<span></span>');
	});

	// add a mouseover trigger to the elements
	$(".parent-item > a, .hidden").bind("mouseover", function() {
		// get container
		var container = $(this).parent('.page_item');

		if(container.attr('timer')) {
			// clear any existing timeout in case mouseout then mouseover
			clearTimeout(container.attr('timer'));
			container.removeAttr('timer');
		}

		if(!container.attr('on')) {
			// set state attributes
			container.attr('on', true).addClass("active");
			$('.hidden', container).show();
		}
	}).bind("mouseout", function() {
		// get parent container
		var container = $(this).parent('.page_item');

		// set timeout to hide menu with delay
		container.attr('timer', setTimeout(function(){hide_hover_menus(container);}, 100));
	});
}

function hide_hover_menus(e) {
	// stop the executer
	clearTimeout(e.attr('timer'));

	// remove state attributes
	e.removeAttr('timer').removeAttr('on');

	// hide menu
	$('.hidden', e).hide();
	e.removeClass("active");
}

function prepare_links() {
	// order matter for priority

	// add pdf icon to left of pdf links
	$('a[href$=".pdf"]').filter(function() {t = $(this).text(); return t && $.trim(t) != "";}).addClass("icon_pdf").click( function() {
				window.open($(this).attr('href'), 'pdfWindow');
				return false;
	});

	// add external icon to any link that does not contain the baseURL
	$('a[href^="http://"], a[href^="https://"]').filter(function(){return $(this).attr('href').indexOf('.e-farmcredit.') == -1 && $(this).attr('class').indexOf('icon') == -1;}).addClass("icon_external").click(function() {
		window.open($(this).attr('href'), 'externalWindow');
		return false;
	});
	
}

$(document).ready(function() {
	prepare_hover_menus();
	prepare_links();
});


