/*********************
//* jQuery Multi Level CSS Menu (horizontal)- By Dynamic Drive DHTML code library: http://www.dynamicdrive.com
//* Menu instructions page: http://www.dynamicdrive.com/dynamicindex1/ddlevelsmenu/
//* Last modified: Sept 6th, 08'. Usage Terms: http://www.dynamicdrive.com/style/csslibrary/tos/
*********************/

/********************
//* Totally rewritten by Adam Mezei on 2009-01-11
*********************/

var jquerycssmenu = {

	//duration of fade in/ out animation, in milliseconds
	fadesettings: {
		overduration: 350,
		outduration: 250
	},

	buildmenu:function(menuid){
		$('#' + menuid).addClass('jquerycssmenu').append('<div style="clear: both;"/>');
		var $mainmenu = $('#' + menuid + ' > ul');
		$mainmenu.find('ul').hide();
		$mainmenu.children('li').addClass('main').find('li').addClass('item');
		var $headers = $mainmenu.find('ul').parent();
		$headers.each(function(i){
			var $curobj = $(this);
			var $subul = $curobj.find('ul:eq(0)');
			this._dimensions = {
				w: this.offsetWidth,
				h: this.offsetHeight,
				subulw: $subul.outerWidth(),
				subulh: $subul.outerHeight()
			};
			this.istopheader = $curobj.parents('ul').length == 1 ? true : false;
			if (this.istopheader)
			{
				$subul.css('top', this._dimensions.h + 'px');
				$curobj.children('a:eq(0)').addClass('downarrow');
			}
			else
			{
				$subul.css('top', 0);
				$curobj.children('a:eq(0)').addClass('rightarrow');
				$curobj.removeClass('item').addClass('node');
			}
			$curobj.hover(
				function(e){
					if (this._timer) clearTimeout(this._timer);

					var $targetul = $(this).children('ul:eq(0)');
					var left, top, bottom, right, w, h;
					if (this.istopheader) {
						var offs = $(this).offset();
						left = offs.left;
						top = offs.top + $(this).outerHeight();
					} else {
						var uloffs = $(this).parents('ul:eq(0)').offset();
						var lioffs = $(this).offset();
						left = lioffs.left - uloffs.left + $(this).outerWidth();
						top = lioffs.top - uloffs.top;
					}

					w = $targetul.width();
					h = $targetul.outerHeight();
					right = left + w;
					bottom = top + h;
					if (right > $(window).width())
						left = $(window).width() - w;

					if (bottom > $(window).height())
						top = $(window).height() - h;

					$targetul.css({
						left: left + 'px',
						top: top + 'px'
					})
					this._timer = setTimeout(function() {
						$targetul.show();
					}, jquerycssmenu.fadesettings.overduration);
				},
				function(e){
					if (this._timer) clearTimeout(this._timer);

					var $targetul = $(this).children("ul:eq(0)");
					this._timer = setTimeout(function() {
						$targetul.hide();
					}, jquerycssmenu.fadesettings.outduration);
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}
}

