/*
Additional attributes:
voffset
hoffset
*/

var dropdown_iTimeout = 500;
var dropdown_xOffset = 8;
var dropdown_yOffset = -4;
//var dropdown_bGrowWidth = true;	// if submenu's width is smaller than mainmenu item, it grows to its width
var dropdown_pTimeout;
var dropdown_bHover = false;


$(document).ready(function() {
	$('#DropDownMenu').find('a').hover(
		function() {
			if($(this)[0].getAttribute("dropdown") == undefined) { return(false); }
			dropdown_bHover = true;
			clearTimeout(dropdown_pTimeout);

			// Hide open dropdowns
			$('#DropDownMenu').find('A:not([@dropdown="' + $(this)[0].getAttribute("dropdown") + '"])').each(
				function() {
					if($(this)[0].getAttribute("dropdown") != undefined) { $('#' + $(this)[0].getAttribute("dropdown")).hide(); }
				}
			)
		
			// Positions dropdown			
			var sIdDropdown = '#' + $(this)[0].getAttribute("dropdown");
			var offset = $(this).offset();
			
			var voffset = 0;
			var hoffset = 0;
			if($(sIdDropdown)[0].getAttribute("voffset")) { voffset = Number($(sIdDropdown)[0].getAttribute("voffset")); }
			if($(sIdDropdown)[0].getAttribute("hoffset")) { hoffset = Number($(sIdDropdown)[0].getAttribute("hoffset")); }

			$(sIdDropdown)[0].style.left = (offset.left + hoffset + dropdown_xOffset) + 'px';
			$(sIdDropdown)[0].style.top = (offset.top + $(this).height() + voffset + dropdown_yOffset) + 'px';
	
			// Displays dropdown
			$(sIdDropdown).show();

			// Dropdown element envent handlers
			$(sIdDropdown).hover(
				function() {
					clearTimeout(dropdown_pTimeout);
				},
				function() {
					clearTimeout(dropdown_pTimeout);
					dropdown_pTimeout = setTimeout("dropdown_mouseOut('" + sIdDropdown + "')", dropdown_iTimeout);					
				}
			)

		},
		function() {
			$('#DebugDiv').html('MouseOUT: '+$(this).attr('id'));
			if($(this)[0].getAttribute("dropdown") == undefined) { return(false); }
			dropdown_bHover = false;
			var sIdDropdown = '#' + $(this)[0].getAttribute("dropdown");
			clearTimeout(dropdown_pTimeout);
			dropdown_pTimeout = setTimeout("dropdown_mouseOut('" + sIdDropdown + "')", dropdown_iTimeout);
		}
	);

});


function dropdown_mouseOut(sIdDropdown) {
	if(dropdown_bHover == false) { $(sIdDropdown).hide(); }
}