var Menu = new Class({
	Implements: [Options,Events],

	options: {
		containerId:'mainMenuContainer',
		firstLevelMenuId:'menuFirstLevel',
		secondLevelMenuId:'menuSecondLevel',
		usedIdPrefix:'edco_',
		timeAutoHide:3000,
		timeToSlide:400 
	},

	initialize: function(options){
		this.setOptions(options);
		if(this.checkConfig()){
			this.setMenuEvents();
		}
	},

	setMenuEvents: function(){
		
		var runFirstTime = false;
		
		// All <li> Elements (firstLevelMenuId).
		var liElements = $(this.options.firstLevelMenuId).getElement('ul').getElements('li');
		// Root <div> Element (secondLevelMenuId).
		var divElement = $(this.options.secondLevelMenuId);
		// All <div> Elements under Root <div> Element (secondLevelMenuId).
		var divElements = $(this.options.secondLevelMenuId).getElements('div');
		
		var liCurrent = null;
		
		// What <li> Element is active?
		var liElementActive = null;
		// Whats the current <div> element?
		var divCurrentElement = null;
		
		// Is there a timer running?
		var mouseoutTimer = false;
		// Whats the time untill the timer goes of?
		var timeTillHide = this.options.timeAutoHide;
		// Whats the time from hidden untill dropped down?
		var timeDrop = this.options.timeToSlide;
		
		// visibility has 2 states: hidden, or visible.
		var visibilityState = 'hidden';		
		
		// Morph effect on divElement.
		var myFx = new Fx.Morph(divElement, {
			unit: 'px',
			link: 'cancel',
			duration: timeDrop,
			onComplete : function(passed_element){
				myFx2.start({
					'visibility' : visibilityState
				});
			}
		});
		

		// top level items
		liElements.addEvent('mouseover', function(){
			runFirstTime = true;
			liCurrent = this;
			
			liElements.each(function(item, index){
				if(item.hasClass('current')){
					item.removeClass('current');
				}
			});
			
			
			var liCurrentClass = this.getProperty('class');
			
			if(liCurrentClass === null || liCurrentClass === ''){
				var liIsEmpty = true;
			}else{
				var liIsEmpty = false;
			}
			
			divElements.each(function(item, index){
				if(liCurrentClass !== item.getProperty('class')){
					item.setStyles({  
						visibility : 'hidden',
						display: 'none'
					});
				}
				
			});

			if(mouseoutTimer) {
				clearTimeout(mouseoutTimer);
			}
			
			var divCurrentElement = null;
			
			divElements.each(function(item, index){
				if(item.getProperty('class').trim() === liCurrentClass.trim() ||  item.getProperty('class').trim() + ' current' === liCurrentClass.trim() ){
					divCurrentElement = item;
				}
			});

			// topmenu has submenu items
			if(liIsEmpty === false){
				
				visibilityState = 'visible';
				
				var divHeight = 0;
				
				divCurrentElement.setStyle('display','block');
				
				divCurrentElement.getElements('ul').each(function(item, index){

					if(divHeight === 0){
						 divHeight = item.getStyle('height').toInt();
					}
					if(item.getStyle('height').toInt() > divHeight){
						divHeight = item.getStyle('height').toInt();
					}
				});
				
				myFx2 = new Fx.Morph(divCurrentElement,{
					duration : 1,
					unit: 'px',
					onStart : function(passed_element){ 
					},
					onComplete : function(passed_element){
						
					}
				});
				
				
				myFx.start({ 
					'height': (divHeight + 30) });

			// topmenu has NO submenu items
			} else {
				visibilityState = 'hidden';
				
				myFx2 = new Fx.Morph(divElements[0],{
								duration : 1,
								unit: 'px',
								onStart : function(passed_element){ 
								},
								onComplete : function(passed_element){
									
								}
							});
					
				myFx.start({ 
					'height': 0 });
				
				
			}			
			
			
			
		});

		// toplevel menu
		liElements.addEvent('mouseleave', function(){
			
			liLastElement = this.getProperty('class');

			if(runFirstTime === true){
				if(liCurrent.hasClass('current') === false){
					liCurrent.addClass('current');
				}	
			}
			
			
			
			if(mouseoutTimer) {		
				clearTimeout(mouseoutTimer);
			}
	
			mouseoutTimer = setTimeout(function(){
				divHeight = 0;
				
				myFx2.start({
					'visibility' : 'hidden',
					'display' : 'none'
				});
					
				myFx.start({ 'height': 0 });

				mouseoutTimer = false;

				liElements.each(function(item, index){
						if(item.hasClass('current')){
							item.removeClass('current');
						}
				});
				
			}, timeTillHide);
			
		});
		
		// submenu items
		$(this.options.secondLevelMenuId).addEvent('mouseover', function() {

			
			if(liCurrent.hasClass('current') === false){
				liCurrent.addClass('current');
			}
			
			if(mouseoutTimer) {
				clearTimeout(mouseoutTimer);
			}
			
		});

		// submenu items
		$(this.options.secondLevelMenuId).addEvent('mouseleave', function() {
			liElements.fireEvent('mouseleave');
		});	

	},

	checkConfig: function(){
		var rootElement = $(this.options.containerId);
		var firstMenuElement = null;
		var secondMenuElement = null;

		if(rootElement === null){
			this.msg("options.containerID: " + this.options.containerID + " was not found!");			
		}else{
			firstMenuElement = rootElement.getElementById(this.options.firstLevelMenuId);
			secondMenuElement = rootElement.getElementById(this.options.secondLevelMenuId);
		}
		
		if(firstMenuElement === null){
			this.msg("options.firstLevelMenuID: " + this.options.firstMenuElement + " was not found!");
		}
		if(secondMenuElement === null){
			this.msg("options.secondLevelMenuID: " + this.options.secondMenuElement + " was not found!");
		}

		if(rootElement !== null && firstMenuElement !== null && secondMenuElement !== null){
			return true;
		}else{
			return false;
		}
		
	},

	msg: function(txt){
		if(txt !== null && txt.length > 1) {
			alert(txt);
		}else{
			alert("The provided Message call was empty! Please provide a string for the message.");
		}		
	}
	
});
