if (jQuery) {
	
(function() {

	$.categoryMenu = function(item, sub) {
		var me = item;
		var sub = sub;
		var bOver = false;
		var hideTimeout = 0;
		var aa='aa';
		
		var onMouseOver = function() {
			$(me).trigger("categoryMenuItem_mouseover");
			if (!bOver) {
				bOver = true;
				$(sub).stop();
				//$(sub).show();
				
				if (jQuery.browser.msie) {
					$(sub).show();
				} else {
					$(sub).animate({opacity:0.95},{queue:false,duration:200});
					$(sub).show();
				}
			}
		};
		
		var onMouseOut = function() {
			$(me).trigger("categoryMenuItem_mouseout");
			bOver = false;
			initHide();
		};
		
		var hideSubMenu = function() {
			clearHide();
			if (!bOver) {
				//$(sub).stop();
				//$(sub).css({zIndex:100});
				//$(sub).hide();
				
				if (jQuery.browser.msie) {
					$(sub).hide();
				} else {
					$(sub).animate({opacity:0},{queue:false,duration:400,
						complete:function(){
							if (!bOver) {
								$(sub).hide();
							}
						}
					});
				}
			}
		};
		
		var clearHide = function() {
			if (hideTimeout > 0) {
				window.clearTimeout(hideTimeout);
				hideTimeout = 0;
			}
		};
		
		var initHide = function() {
			clearHide();
			hideTimeout = window.setTimeout( function() {
				hideSubMenu();
			}, 400 );
		};
		
		$(item).bind('mouseover', onMouseOver);
		$(item).bind('mouseout', onMouseOut);
		
		$(item).bind('_mouseout', hideSubMenu);
		
		return this;
	};
	
	$.extend( $.categoryMenu.prototype, {
		
		test : function() {
			console.debug(aa);
		}
		
	} );
	
	$.fn.categoryMenu = function(p,l) {
		return this.each(function() {
			
			if ( $(this).is("._categoryMenu") ) {
				return;
			}
			$(this).addClass("_categoryMenu")
			
			var ele = this;
			var myParents = p || undefined;
			var curr = undefined;
			
			var onMouseOver = function() {
				if (this != curr) {
					if ( curr != undefined ) {
						var sub = $.data(curr, "_subCategoryMenu");
						if (sub != undefined) {
							//sub.hideNow();
							$(curr).trigger('_mouseout');
						}
					}
					//console.debug('new');
					curr = this;
				}
			};
			
			var onMouseOut = function() {
				
			};
			
			var init = function() {
				var pp = new Array();
				
				if (myParents != undefined) {
					pp = myParents.slice();
				}
				
				pp.push(ele);
				
				$(">li",ele).each(function() {
					var ul = $(">ul", this);
					if (ul.length > 0) {
						ul = $(ul[0]);
						if (!ul.is("ul.PeCategories")) {
							var sub = new $.categoryMenu(this,ul);
							//sub.test();
							
							$.data(this,"_subCategoryMenu", sub);
							
							ul.categoryMenu(pp);
						}
					}
					$(this).bind("mouseover", onMouseOver);
					$(this).bind("categoryMenuItem_mouseover", onMouseOver);
					$(this).bind("categoryMenuItem_mouseout", onMouseOut);
				});
			};
			
			init();
		});
	};
	
	$(function() {
		$('ul.PeCategories').categoryMenu();
	});
	
})(jQuery);
	
}
