
;(function($) {
	$.fn.extend({
		dropdown: function() {
		var thisSelector = this.selector;
		return this.each(function() {
			var theUL = $(this);
			var theLIs = theUL.find('li');
			theLIs.each(
				function(){
					var theLI = $(this);
					var prevItem = $(theLI.prev());				
					if(theLI.parent(thisSelector).get(0)){
						theLI.data('level', 1) ;					
						theLI.addClass('firstLevel');	
						if(theLI.hasClass('inSection'))theLI.addClass('firstLevelOver');
									
					}
					else{
						theLI.data('level', theLI.parents('li:eq(0)').data('level')+1) ;
						theLI.addClass('subLevel');					
					}
					theLI.data('childMenu',theLI.find('ul').get(0) || null )
					//CSS hacks for IE
					var theLink = $(theLI.find('a:eq(0)'));
					theLink.css('color', theLI.css('color'));
					theLink.css('text-decoration', 'none');
					theLI.data('link', theLink);
					
					var theSubMenu = theLI.data('childMenu')?$(theLI.data('childMenu')):null;
					if(theSubMenu){
						theSubMenu.data('parentItem', theLI);
						theLink.append('  ');
					};
					theLI.bind('mouseenter', function(){
						if(theSubMenu){
							theSubMenu.css('top', (theLI.data('level')==1)?theLI.position().top+theLI.outerHeight()-1:theLI.position().top);
							theSubMenu.css('left', (theLI.data('level')==1)?theLI.position().left+1:theLI.position().left+theLI.outerWidth());
							showMenu(theSubMenu)
						}
						else{
							itemStyleOver(theLI);
						}
					})
					.bind('mouseleave', function(){
						if(theSubMenu){
							hideMenu(theSubMenu)
						}
						else{
							itemStyleOut(theLI);
						}
						})
					.bind('click', function(evt){

						if (evt.target == this){
							var link = $('> a', evt.target);
							if ( link.length > 0 ) {
								var a = link[0];
								if ( !a.onclick ) {
									window.open( a.href, a.target || '_self' );
								} else {
									$(a).trigger('click');
								}
							}
							
						}

					});
				});
			});
	}});
	})
	
(jQuery);

function showMenu(menu){
	var parentItem = menu.data('parentItem');
	itemStyleOver(parentItem)
	menu.show();
}

function hideMenu(menu, noDelay){
	var closeDelay = noDelay?0:500;
	var parentItem = menu.data('parentItem');
	clearTimeout( this.$menuTimer );
	if(closeDelay==0){
		menu.find('ul:visible').andSelf().each(function(){
			$(this).hide();
			itemStyleOut($(this).data('parentItem'))
		});

	}
	else{
		this.$menuTimer = setTimeout(function() {
			menu.find('ul:visible').andSelf().each(function(){
				$(this).hide();
				itemStyleOut($(this).data('parentItem'))
			});
		}, closeDelay);
	}		
	
}

function itemStyleOver(menuItem){
	menuItem.siblings('li').find('ul:eq(0):visible')
	.each(function(){
		hideMenu($(this), true)
	});
	(menuItem.data('level')>1)?menuItem.addClass('subLevelOver'):menuItem.addClass('firstLevelOver');
	menuItem.data('link').css('color', menuItem.css('color'));
}
function itemStyleOut(menuItem){
	if(menuItem.data('level')>1){
		menuItem.removeClass('subLevelOver')
	}
	else{
		if(!menuItem.hasClass('inSection'))menuItem.removeClass('firstLevelOver');
	}
	menuItem.data('link').css('color', menuItem.css('color'));
}