

$.fn.navigation = function(opt){

    return this.each(function(){
        
		var NAVIGATION_OBJECT = this;
		
		NAVIGATION_OBJECT.option = {
            effect:   null,
            zIndexActiveItem:    1,
            zIndexActiveSubMenu: 0
        };
        NAVIGATION_OBJECT.option = $.extend(NAVIGATION_OBJECT.option,opt);
		
		var debug       = false;
		var loaded      = false;
		var type        = null;
		var orientation = null;
		var delay       = 500;
		var timer       = new Object();
		var timerFadeIn = null;
		var effect      = NAVIGATION_OBJECT.option.effect;
		
		NAVIGATION_OBJECT.hideSubMenu = function (parentItemId, levelId) {
			levelId = parseInt(levelId);
			var menuId = parentItemId.split('-')[0];
			var parentItem = $('#'+parentItemId);
			var parentItemContent = $('#'+parentItemId+' .itemcontent-L'+levelId);
			var subMenuCurrent = $("div."+menuId+"-L"+levelId, parentItem);
			
			parentItemContent.css('z-index', levelId*10)
			subMenuCurrent.css('z-index', levelId*10)
			parentItem.css('z-index', levelId*10);
			
			if (debug) {
				console.debug(subMenuCurrent.attr('id') + ' z-index=' + subMenuCurrent.css('z-index'))
				console.debug(parentItem.attr('id') + ' z-index=' + parentItem.css('z-index'))
				console.debug(parentItemContent.attr('id') + ' z-index=' + parentItemContent.css('z-index'))
			}						
			
			if (!parentItem.hasClass('hover')) {
				var parentItemLink = $('div.itemcontent-L'+(levelId-1)+' a', parentItem);	
				parentItemLink.removeClass('hover');
		    	$("div."+menuId+"-L"+levelId, parentItem).hide();
		    }
			return;
		}

		$.fn.hideSubMenu = NAVIGATION_OBJECT.hideSubMenu
				
		NAVIGATION_OBJECT.showSubMenu = function (parentItemId, levelId) {
			
			
			var menuId = parentItemId.split('-')[0];
			
			if (debug)
				console.debug('showSubMenu parentItemId' + parentItemId + ', menuId=' + menuId)
			
			var parentItem = $('#'+parentItemId);	
			var parentItemContent = $('#'+parentItemId+' .itemcontent-L'+levelId);
			var parentMenu = parentItem.parent();
			var subMenuCurrent = $("div."+menuId+"-L"+levelId, parentItem);
			
			parentItemContent.css('z-index', levelId*10+10+NAVIGATION_OBJECT.option.zIndexActiveItem)
			subMenuCurrent.css('z-index', levelId*10+10+NAVIGATION_OBJECT.option.zIndexActiveSubMenu)
			parentItem.css('z-index', levelId*10+10);
			
			if (debug) {
				console.debug(subMenuCurrent.attr('id') + ' z-index=' + subMenuCurrent.css('z-index'))
				console.debug(parentItem.attr('id') + ' z-index=' + parentItem.css('z-index'))
				console.debug(parentItemContent.attr('id') + ' z-index=' + parentItemContent.css('z-index'))
			}	
				
				
			
			//other sub menus of same level
			$("div."+menuId+"-L"+levelId, parentMenu).each(function () {
				var subMenu = $(this);
				
				if (subMenu.attr('id')!=subMenuCurrent.attr('id')) {
					$(subMenu).hide();
				} else {
					if ( ! subMenu.is(':visible') ) {
						if (effect == 'show') {
							$(subMenu).show();					
						} else {
							$(subMenu).fadeIn();
						}
					}		
				}
			});
			return;
		}
		$.fn.showSubMenu = NAVIGATION_OBJECT.showSubMenu
		
		
		NAVIGATION_OBJECT.init = function () {
			var menuId = NAVIGATION_OBJECT.id;
			
			var menuLevel = $("#"+menuId+"-L1", NAVIGATION_OBJECT);
			
			// type
			if (menuLevel.hasClass('drop_down_static')) {
				type = 'drop_down_static';
			} else if (menuLevel.hasClass('tree')) {
				type = 'tree';
			} else {
				type = 'static';
			}
			
			// z-index
			$("div.item-L1 .itemcontent-L1", NAVIGATION_OBJECT).css('z-index', NAVIGATION_OBJECT.option.zIndexActiveItem);
			$("div.item-L1 .menu-L2", NAVIGATION_OBJECT).css('z-index', NAVIGATION_OBJECT.option.zIndexActiveSubMenu);
			
			// orientation
			if (menuLevel.hasClass('vertical')) {
				orientation = 'vertical';
			} else {
				orientation = 'horizontal';
			}
			
			/** LEVEL 2 **/	
		    $("div.item-L1", NAVIGATION_OBJECT).mouseenter(function (e) {	    		    	
				var menuId = NAVIGATION_OBJECT.id;
		    	
		    	if(!loaded) { return; }
		    	
		    	if (timerFadeIn != null) {
			    	clearTimeout( timerFadeIn );
			    }
		    	
		    	if (debug)
		    		console.debug("hover " + this.id, timer);
		    	
		    	$("div.itemcontent-L1 a", NAVIGATION_OBJECT).removeClass('hover');
		    	if($('div.itemcontent-L1 a', this).hasClass('active')) {
		    		$('div.itemcontent-L1 a', this).removeClass('active').addClass('off_active');;
		    	}    	
		    	if($('div.itemcontent-L1 a', this).hasClass('sub')) {
		    		$('div.itemcontent-L1 a', this).removeClass('sub').addClass('off_sub');
		    	}  
		    	$('.itemcontent-L1 a', this).addClass('hover');
		    	
		    	if ( $("div.item-L2 div."+menuId+"-L2", this) ) {
		    		
			    	if (type == 'drop_down_static' || type == 'tree') {
			    		/** do nothing **/
			    	} else {
				    	posX  = 0; //$(this).position().left;
				    	posY  = 0; //$(this).position().top;
				    	
				    	if(orientation == 'vertical') {
				    		posX += $(this).width();
				    	} else if (orientation == 'horizontal') {		    		
				    		
				    		itemOffsetLeft = $(this).offset().left;
				    		itemWidth      = $(this).width();
				    		menuWidth      = $("."+menuId+"-L2", this).width();
				    		menuHeight     = $("."+menuId+"-L2", this).innerHeight();
				    		windowWidth    = $(window).width();
				    		if(debug)
				    			console.debug("itemWidth="+ itemWidth, ", menuWidth="+ menuWidth, ", menuHeight="+ menuHeight,  ", windowWidth="+ windowWidth,  ", itemOffsetLeft="+ itemOffsetLeft);
				    		if ( (itemOffsetLeft+menuWidth) > (windowWidth-20) ) {
				    			posX = posX - (menuWidth-itemWidth);
				    		}
				    		
				    		posY += $(this).height();
				    		
				    	}
				    	
				    	if ($("#"+menuId+"-L1").hasClass('drop_down_left')) {
				    		posX = $("."+menuId+"-L2", this).innerWidth() * -1;
				    		
				    	} else if ($("#"+menuId+"-L1").hasClass('drop_down_top')) {    		
				    		posY = $("."+menuId+"-L2", this).innerHeight() * -1;				    		
				    	}
				    	
				    	$("."+menuId+"-L2", this).css('left', posX+'px')
				    	$("."+menuId+"-L2", this).css('top',  posY+'px')  
				    	
			    	}
			    	
			    	// isOneSubMenuVisible
			    	if (type != 'tab') {
				    	var isOneSubMenuVisible = false;
				    	$("div."+menuId+"-L2", this.parentNode).each(function () {
							var subMenu = $(this);
							if (subMenu.is(':visible')) {
								isOneSubMenuVisible = true;
							}
				    	});
				    } else {
				    	var isOneSubMenuVisible = true;
				    }
			    	
			    	// fastShow
			    	if (isOneSubMenuVisible) {
			    		var showImmidiatly = false;
				    } else {
			    		var showImmidiatly = true;
			    	}
			    	
			    	if (showImmidiatly) {
				    	NAVIGATION_OBJECT.showSubMenu(this.id, '2');
						timerFadeIn = null;
				    } else {
				    	timerFadeIn = setTimeout("$('#"+menuId+"').showSubMenu('"+this.id+"', '2');", delay);			    	
					}
			    	
		    	}
		    	
		    });
		    
		    $("div.item-L1", NAVIGATION_OBJECT).mouseleave(function (e) {	
		    	if(!loaded) { return; }
				
				var menuId = NAVIGATION_OBJECT.id;
		    	
		    	clearTimeout( timerFadeIn );	
		    	
		    	if (debug)
		    		console.debug("mouseleave " + this.id + ', menuId: '+menuId, timer);
		    	
		        var reltg   = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		        var topNode = document.getElementById(this.id);
		 	    if (typeof reltg != 'undefined' && reltg != null) {       
		 	        while (reltg != topNode && reltg.nodeName != 'BODY') {
		 	        	if (typeof reltg != 'undefined' && reltg != null) {
		 	        		reltg = reltg.parentNode;
		 	        	}
		 	        }
		 	        if (reltg == topNode) {
		 	           return;
		 	        }
		        }
		 	    
		 		if($('div.itemcontent-L1 a', this).hasClass('off_active')) {
		 			$('div.itemcontent-L1 a', this).addClass('active');
		 		} else if($('div.itemcontent-L1 a', this).hasClass('off_sub')) {
		 			$('div.itemcontent-L1 a', this).addClass('sub');
		 		}
		 	    
		        timer[$("#"+this.id+" ."+menuId+"-L2").attr('id')] = setTimeout("$('#"+menuId+"').hideSubMenu('"+this.id+"', '2');", delay);
		        
		        if(debug)
		        	console.debug('setTimeout -> ' + $("#"+this.id+" ."+menuId+"-L2").attr('id'));
		    });    
		    
			
			/** LEVEL 3 **/	
		    $("div.item-L2", NAVIGATION_OBJECT).mouseenter(function (e) {	    		    		    	
		    	if(!loaded) { return; }
		    	
				var menuId = NAVIGATION_OBJECT.id;
		    	
		    	clearTimeout( timerFadeIn );
		    	if($(this).parent().parent().attr('id') in timer)
		    		clearTimeout(timer[$(this).parent().parent().attr('id')]);
		    	
		    	if (debug)
		    		console.debug('hover ' + this.id + ', clearTimeout ' + $(this).parent().parent().attr('id'), timer);
		    	
		    	$("div.itemcontent-L2 a", NAVIGATION_OBJECT).removeClass('hover');
		    	if($('div.itemcontent-L2 a', this).hasClass('active')) {
		    		$('div.itemcontent-L2 a', this).removeClass('active').addClass('off_active');;
		    	}    	
		    	if($('div.itemcontent-L2 a', this).hasClass('sub')) {
		    		$('div.itemcontent-L2 a', this).removeClass('sub').addClass('off_sub');
		    	}  
		    	$('div.itemcontent-L2 a', this).addClass('hover');
	
		    	if ( 1==1 ) {
		    		
			    	if (type == 'drop_down_static' || type == 'tree') {
			    		/** do nothing **/
			    	} else {
				    	posX  = 0;//$(this).position().left;
				    	posY  = 0;//$(this).position().top;
				    	
				    	if($("#"+menuId+"-L1").hasClass('vertical')) {
				    		posX += $(this).width();
				    		
				    	} else if ($("#"+menuId+"-L1").hasClass('horizontal')) { 
				    		
				    		windowWidth = $(window).width();			    		
				    		menuWidth   = $("."+menuId+"-L3", this).width();
				    		parentMenuWidth      = $(this).parent().parent().width();
				    		parentMenuOffsetLeft = $(this).parent().parent().offset().left;
				    					    		
				    		if(debug)
				    			console.debug("windowWidth=" + windowWidth, ", parentMenuOffsetLeft=" + parentMenuOffsetLeft,  ", parentMenuWidth=" + parentMenuWidth, ", menuWidth=" + menuWidth);
				    		if ( (parentMenuOffsetLeft + parentMenuWidth + menuWidth) > (windowWidth-20) ) {
				    			if(debug)
				    				console.debug(posX);
				    			posX = parentMenuWidth * -1;
				    			if(debug)
				    				console.debug(posX);
				    		} else {			    		
				    			posX += $(this).width();
				    		}
				    	}
				    		    	
				    	if ($("#"+menuId+"-L1").hasClass('drop_down_left')) {
				    		
				    		posX = $("."+menuId+"-L3", this).width() * -1;
				    		
				    	} else if ($("#"+menuId+"-L1").hasClass('drop_down_top')) {
				    		posY = $("."+menuId+"-L3", this).height() * -1;
				    		
				    	}
				    	
				    	$("."+menuId+"-L3", this).css('left', posX+'px')
				    	$("."+menuId+"-L3", this).css('top',  posY+'px')   
				    	
			    	}
			    	 	
			    	//$("."+menuId+"-L3", this).show();
			    	timerFadeIn = setTimeout("$('#"+menuId+"').showSubMenu('"+this.id+"', '3');", delay);
			    	
		    	}
		    	
		    });
		    
		    
		    $("div.item-L2", NAVIGATION_OBJECT).mouseleave(function (e) {	    		    		    	
		    	if(!loaded) { return; }
		    	
				var menuId = NAVIGATION_OBJECT.id;
		    	
		    	clearTimeout( timerFadeIn );
		    	if($(this).parent().parent().attr('id') in timer)
		    		clearTimeout(timer[$(this).parent().parent().attr('id')]);
		    	
		    	if (debug)
		    		console.debug('hover ' + this.id + ', clearTimeout ' + $(this).parent().parent().attr('id'), timer);
		    	
		        var reltg   = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		        var topNode = document.getElementById(this.id);
		 	    if (reltg != null && typeof reltg != 'undefined') {       
		 	        while (reltg != topNode && reltg.nodeName != 'BODY') {
		 	        	if (typeof reltg != 'undefined' && reltg != null) {
		 	        		reltg = reltg.parentNode;
		 	        	}
		 	        }
		 	        if (reltg == topNode) {
		 	           return;
		 	        }
		        }	    	
	
		 		if($('div.itemcontent-L2 a', this).hasClass('off_active')) {
		 			$('div.itemcontent-L2 a', this).addClass('active');
		 		} else if($('div.itemcontent-L2 a', this).hasClass('off_sub')) {
		 			$('div.itemcontent-L2 a', this).addClass('sub');
		 		}
		    		 	    
		 	    timer[$("#"+this.id+" ."+menuId+"-L3").attr('id')] = setTimeout("$('#"+menuId+"').hideSubMenu('"+this.id+"', '3');", delay);
		        if(debug)
		        	console.debug('setTimeout -> ' + $("#"+this.id+" ."+menuId+"-L3").attr('id'));
		    });
		    
		    $("div.item-L3", NAVIGATION_OBJECT).hover(function (e) {	    	
		    	if(!loaded) { return; }
		    	
				var menuId = NAVIGATION_OBJECT.id;
			
		    	clearTimeout( timerFadeIn );
		    	clearTimeout( timer[$(this).parent().parent().attr('id')] );
		    	
		    	if (debug)
		    		console.debug('hover ' + this.id + ', clearTimeout ' + $(this).parent().parent().attr('id'), timer);
		    	
		    	//console.debug("timer"+$(this).parent().parent().attr('id'));
		    	//console.debug(timer[$(this).parent().parent().attr('id')]);
		    	
		    });	
		
		    
		    $("div."+menuId+"-L2", NAVIGATION_OBJECT).hover(function (e) {
		    	if(!loaded) { return; }
		    	
		    	if($(this).attr('id') in timer)
		    		clearTimeout(timer[$(this).attr('id')]);
		    });	
		    $("div."+menuId+"-L2", NAVIGATION_OBJECT).mouseleave(function (e) {	
		    	if(!loaded) { return; }
		    	
		    	//if($(this).attr('id') in timer)
		    	//	clearTimeout(timer[$(this).attr('id')]);	    	
		    });	
		    
		    /** onclick **/
		    /*
		    $(""div.item-L1", NAVIGATION_OBJECT).click(function () {
		    	$("div.itemcontent-L1").removeClass('active');
		    	$("div.itemcontent-L1 a", this).removeClass('hover');
		    	$("div.itemcontent-L1 a", this).addClass('active');
		    });
		    $("div.item-L2", NAVIGATION_OBJECT).click(function () {
		    	$("div.itemcontent-L2 a").removeClass('active');
		    	$("div.itemcontent-L2 a", this).removeClass('hover');
		    	$("div.itemcontent-L2 a", this).addClass('active');
		    }); 
		    */
		    
		    loaded = true;
		    
		    return;
		}
	
        NAVIGATION_OBJECT.init();
	
    });
}