(function($) {	
	$.nxsSlideMenu = function(menuObj, aOptions){
		var $sMenuObject 	= $(menuObj);
		var $sActiveElement	= 0;
		var $oTimeout		= 0;
		var $oTimeout2		= 0;
		var $iLiCounter 	= 0;
		var $iUlCounter 	= 0;
		initMenu();		
		designMenu();
		activateMenu();
						
		function activateMenu() {
			$(".nsmMainPoint").hover(
				function () {					
					if ($oTimeout) {
						window.clearTimeout($oTimeout);
						if ($sActiveElement == 0)
							$(".nsmSubNavigation").removeClass("nsmon");
					}
					if ($oTimeout2) {
						window.clearTimeout($oTimeout2);
					}
					$sActiveElement = this;		
					$(this).children(".nsmSubNavigation").addClass("nsmon");
				},
				function () {
					$sActiveElement = 0;
					hideNavigation(this);					
				}
			);
			$(".nsmSubPoint").hover(
				function () {										
					if ($oTimeout2) {
						window.clearTimeout($oTimeout2);
					}
					if ($sActiveElement != 0)
						closeSubnavigation(this);
					$sActiveElement = this;				
					$(this).children(".nsmSubNavigation").addClass("nsmon");
					iWeight = $(this).css('width');
					iWeight = parseInt(iWeight);
					iWeight = aOptions.subNavMarginLeft + iWeight;
					$(this).children(".nsmSubNavigation").css('left', iWeight+"px");
				},
				function () {
				}
			);
		}
		
		function closeSubnavigation(object) {
			$oTimeout2 = window.setTimeout(function () {
				$(".nsmon").each(function() {
					if ($(this).hasClass("nsmLowerLevelNav")) {
						if (!isInTree(object, this)) {
							$(this).removeClass("nsmon");
						}
					}
				});	
			}, aOptions.speedCloseSnapNav);		
		}
		
		function isInTree(activepoint, navbox) {
			if (activepoint != undefined) {
				if ($(activepoint).attr('nsmIdent') == $(navbox).attr('nsmParentIdent')) 
					return true;
				
				if ($(activepoint).attr('nsmParentIdent') == undefined)
					return false;

				parentIdent = $(activepoint).attr('nsmParentIdent');
				while (parentIdent != undefined) {
					$sParent = $(".nsmIdent_" + parentIdent);
					if ($sParent.attr('nsmIdent') == $(navbox).attr('nsmParentIdent')) 
						return true;
					parentIdent = $sParent.attr('nsmParentIdent');
				}
			}
			return false;
		}
				
		function hideNavigation(object) {
			$oTimeout = window.setTimeout(function () {
				$(".nsmSubNavigation").removeClass("nsmon");
			}, aOptions.speedCloseNav);
		}
		
		function designMenu() {			
			$(".nsmMainPoint").each(function () {
				iCountSubPoints = 0;
				$(this).children("ul").each(function () {
					iCountSubPoints++;
				});
				if (iCountSubPoints > 0) {
					$(this).children("a").addClass("hasSubPoints")
				}
			});
			
			$(".nsmSubPoint").each(function () {
				iCountSubPoints = 0;
				$(this).children("ul").each(function () {
					iCountSubPoints++;
				});
				if (iCountSubPoints > 0) {
					$(this).children("a").addClass("hasSubPoints")
				}
			});
		}				
		
		function addSubNavLevel(object, iTmpLiCounter, lowerlevel) {
			parentCounter = iTmpLiCounter;
			$(object).addClass("nsmSubNavigation");
			if (lowerlevel == 1)
				$(object).addClass("nsmLowerLevelNav");
			$(object).attr("nsmParentIdent", parentCounter);
			$(object).children("li").addClass("nsmSubPoint");			
			$(object).children("li").each(function (){	
				$iLiCounter++;
				$(this).attr("nsmParentIdent", parentCounter);
				$(this).attr("nsmIdent", $iLiCounter);
				$(this).addClass("nsmIdent_"+$iLiCounter);
			});
			$(object).children("li").each(function (){	
				iTmpSubLiCounter = $(this).attr('nsmIdent');
				$(this).children("ul").each(function (){
					addSubNavLevel(this, iTmpSubLiCounter, 1);
				});
			});
		}
		
		function initMenu() {
			$sMenuObject.addClass('nsmNavigation');
			$sMenuObject.children("li").addClass("nsmMainPoint");	
			$sMenuObject.children("li").each(function (){
				$iLiCounter++;
				$(this).attr("nsmIdent", $iLiCounter);
				$(this).addClass("nsmIdent_"+$iLiCounter);
			});
			$sMenuObject.children("li").each(function (){
				iTmpLiCounter = $(this).attr('nsmIdent');
				$(this).children("ul").each(function (){
					$(this).css("top", aOptions.subNavMarginTop);
					$(this).css("left", aOptions.subNavMarginLeft);
					addSubNavLevel(this, iTmpLiCounter);
				});
			});
		}		
	}
	
	
	$.fn.nxsSlideMenu = function(aOptions){
		aOptions = aOptions || {};
		aOptions.subNavMarginLeft = aOptions.subNavMarginLeft || 0;
		aOptions.subNavMarginTop = aOptions.subNavMarginTop || 0;
		aOptions.speedCloseNav = aOptions.speedCloseNav || 500;
		aOptions.speedCloseSnapNav = aOptions.speedCloseSnapNav || 10;
							
		this.each(function() {
			new $.nxsSlideMenu(this, aOptions);
		});
	}
	
})(jQuery);
