function Menu(mainId){
	this.mainId = mainId;
	this.showing = "";
}

function myTransition(input){
	return input*0.8;
}

function showElement(element){
	if(!element.isOpening){
		if(element.isClosing){	    		
			element.closeEffect.cancel();
			element.isClosing = false;
		}
		
		element.isOpening = true;
		element.openEffect = Effect.Appear(element, {duration: element.openAnimationDuration, to:0.9});
	}
}

function hideElement(element){
	if(!element.isClosing){			
		if(element.isOpening){				
			element.openEffect.cancel();
			element.isOpening = false;
		}
		element.isClosing = true;
		element.closeEffect = Effect.Fade(element, {duration: element.closeAnimationDuration})
	}
}

function showMenu(event, element, options){
	element = $(element);		
	
	var caller = Event.element(event);
	// Search for li in caller parents if caller is not such element	
	if(!caller.id || (caller.tagName != 'LI' && caller.tagName != 'UL')){
		caller = caller.ancestors().find(function(el){											
											return (el.id)&&(el.tagName == 'LI' || el.tagName == 'UL');
										  })
	}
	
	if(caller.id){		
		// Positioning
		if(options){
			// Animation timing
			if(options['openDuration']){
				element.openAnimationDuration = options['openDuration'];
			}else if(options['duration']){
				element.openAnimationDuration = options['duration']; 
			}else{
				element.openAnimationDuration = 0.5;
			}
			
			// Animation timing
			if(options['closeDuration']){
				element.closeAnimationDuration = options['closeDuration'];
			}else if(options['duration']){
				element.closeAnimationDuration = options['duration']; 
			}else{
				element.closeAnimationDuration = 0.5;
			}
			
			var elementOffsetTop = 0;
			var elementOffsetLeft = 0;
			if(options['parentPosition'].split('-').include('below')){	
				elementOffsetTop += caller.getHeight();		
			}
			
			if(options['parentPosition'].split('-').include('above')){
				elementOffsetTop -= caller.getHeight();		
			}
			
			if(options['parentPosition'].split('-').include('right')){		
				elementOffsetLeft += caller.getWidth();		
			}
			
			if(options['parentPosition'].split('-').include('left')){
				elementOffsetLeft -= caller.getWidth();		
			}
			
			if(options['offsetLeft']){
				elementOffsetLeft += options['offsetLeft'];
			}
			
			if(options['offsetTop']){
				elementOffsetTop += options['offsetTop'];
			}
			
			Element.clonePosition(element,
					  caller,
					  {setWidth: false,
					   setHeight: false,
					   offsetLeft: elementOffsetLeft,
					   offsetTop: elementOffsetTop}
					  );
			
			
		}
		
		if(!options || !options['preventPropagation']){
    		var bits = element.id.split('-');
    		bits.pop();    		
    		if(bits.length > 1){    			
    			var index = 1;
    			var current = bits[0];
    			while(index<bits.length){
    				current += "-"+bits[index];    				
    				var currentElement = $(current);
    				showElement(currentElement);
    				index++;
    			}
    		}	    		
    	}
		
	    showElement(element);
	    
	}
}

function hideMenu(event, element) {	
	element = $(element);	
	
	var caller = Event.element(event);
	// Search for li in caller parents if caller is not such element	
	if(!caller.id || (caller.tagName != 'LI' && caller.tagName != 'UL')){
		caller = caller.ancestors().find(function(el){											
											return (el.id)&&(el.tagName == 'LI' || el.tagName == 'UL');
										  })
	}	
	
	if(caller.id){		
    	var bits = element.id.split('-');
    	bits.pop();    		
    	if(bits.length > 1){    			
    		var index = 1;
    		var current = bits[0];
    		while(index<bits.length){
    			current += "-"+bits[index];    				
    			var currentElement = $(current);
    			hideElement(currentElement);
    			index++;
    		}
    	}    	
		
		hideElement(element);
	}
}


