/*
Additional attributes:
voffset
hoffset
*/

var dropdown_ver = '2.10';	
//2.00 compatible with jquery 1.3
//2.10 added hpadding (horizontal padding) and auto dropdown width if smaller than main menu
var dropdown_iTimeout = 500;

//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")).stop(true,true); $('#' + $(this)[0].getAttribute("dropdown")).hide(); }
				}
			)
		
			// Positions dropdown			
			var sIdDropdown = '#' + $(this)[0].getAttribute("dropdown");
			var offset = $(this).offset();
			
			var voffset = 25;
			var hoffset = -1;
			var hpadding = 0;
			if($(sIdDropdown)[0].getAttribute("voffset")) { voffset = Number($(sIdDropdown)[0].getAttribute("voffset")); }
			if($(sIdDropdown)[0].getAttribute("hoffset")) { hoffset = Number($(sIdDropdown)[0].getAttribute("hoffset")); }
			if($(sIdDropdown)[0].getAttribute("hpadding")) { hpadding = Number($(sIdDropdown)[0].getAttribute("hpadding")); }

			$(sIdDropdown)[0].style.left = (offset.left + hoffset - hpadding/2) + 'px';
			$(sIdDropdown)[0].style.top = (offset.top + $(this).height() + voffset) + 'px';
			
			if($(sIdDropdown).width() < ($(this).outerWidth() + hpadding))  {
				$(sIdDropdown).width( $(this).outerWidth() + hpadding );
			}
			
			// Displays dropdown
			$(sIdDropdown).stop(true,true);
			$(sIdDropdown).slideDown('fast');

			// Dropdown element event handlers
			var myTimeout = dropdown_iTimeout;
			if( $(sIdDropdown)[0].getAttribute("timeout") ) { myTimeout = $(sIdDropdown)[0].getAttribute("timeout"); }
			
			$(sIdDropdown).hover(
				function() {
					clearTimeout(dropdown_pTimeout);
				},
				function() {
					clearTimeout(dropdown_pTimeout);
					dropdown_pTimeout = setTimeout("dropdown_mouseOut('" + sIdDropdown + "')", myTimeout);					
				}
			)

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

});


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