// from http://www.htmldog.com/articles/suckerfish/dropdowns/
sfHover = function() {
	var sfEls = document.getElementById("primary_nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function(trigger_event) {
			navover(this.id, trigger_event); // jkr added this line for vpr
		}
		sfEls[i].onmouseout=function(trigger_event) {
			navout(this.id, trigger_event); // jkr added this line for vpr
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
if (window.addEventListener) window.addEventListener("DOMContentLoaded", sfHover, false); // jkr added so this runs in firefox, too


function navover(nav_li_id, trigger_event) {
	var trigger_li = jQuery('#'+nav_li_id);
	trigger_li.addClass('sfhover');
	if (!trigger_li.hasClass('empty')) { // items with no 2nd level items

		pattern = /_li/i;
		tabname = nav_li_id.replace(pattern, ''); // strip 'listen_li' to straight 'listen'

		// only go through positioning if not already done -- save a lot of processing
		positioned_var = tabname + '_positioned';
		if (typeof window[positioned_var] == 'undefined') {
			// indicate we've already positioned this element
			window[positioned_var] = true;
			var primary_nav = jQuery('#primary_nav');
			var container_coords = {
				left: primary_nav.offset().left,
				right: primary_nav.offset().left + primary_nav.width(), 
				width: primary_nav.width()
			};
			var trigger_li = jQuery('#'+nav_li_id);
			var trigger_li_coords = {
				left: trigger_li.offset().left,
				right: trigger_li.offset().left + trigger_li.width(), 
				width: trigger_li.width()
			};
			var pixels_to_edge = container_coords.right - trigger_li_coords.left;
			var subnav_div = jQuery('#'+tabname+"_sub");
			// shove way left so it'll expand to natural size
			subnav_div.css('left', '-2000px');
			var subnav_div_coords = {
				left: subnav_div.offset().left,
				right: subnav_div.offset().left + subnav_div.width(), 
				width: subnav_div.width()
			};
			// fix the width at that size, so it won't change later
			subnav_div.css('width', subnav_div_coords.width);

			if (subnav_div_coords.width > pixels_to_edge) {
				subnav_nudge_left = subnav_div_coords.width - pixels_to_edge + 2; // 5 added just to give a bit of room
				subnav_div.css('left', '-'+ subnav_nudge_left + 'px');
			} else {
				// subnav_div.css('left', 'auto');
				subnav_div.css('left', '0px');
			}
		} // end if not already positioned
	} // end if not an empty menu
} // end function 

function navout(nav_li_id,trigger_element) {
	var trigger_li = jQuery('#'+nav_li_id);
	trigger_li.removeClass('sfhover');
} // end function 