﻿$(function() {
$(".childmenu").hide();
	
	$('.childmenu > li').mouseover(function() {
	$(this).addClass('childmenu_item_hover');
		$(this).removeClass('childmenu_item');
	});

	$('.childmenu > li').mouseout(function() {
		$(this).addClass('childmenu_item');
		$(this).removeClass('childmenu_item_hover');
	});

	$().mousemove(function(e) {

		if (currentMnu != null
                && currentNav != null
                && !insideBounds(currentMnu, e.pageX, e.pageY)
                && !insideBounds(currentNav, e.pageX, e.pageY)) {

			currentMnu.fadeOut();
			currentNav = null;
			currentMnu = null;
		}
	});
});


var currentNav = null;
var currentMnu = null;

function onOver(mychild, caller) {

	$(".childmenu").hide();

	if ($(mychild).children().length > 0) {
		currentMnu = $(mychild).show();

		if (caller != null) {

			currentNav = $(caller);
			var offset = currentNav.offset();
			currentMnu[0].style.left = offset.left;
			currentMnu[0].style.top = offset.top + currentNav.height();
		}
	}
}

function insideBounds(the_element, x, y) {
	var offset = the_element.offset();
	var bottom = offset.top + the_element.height();
	var right = offset.left + the_element.width();

	// the -10 is a buffer.. should the text not quite mesh with the top of the UL
	return (x >= offset.left && x <= right
            && y >= offset.top - 10 && y <= bottom);
}