var fx = Array();
var h  = Array();
var ZTMegaMenu = function(boxTimer, xOffset, yOffset, smartBoxSuffix, smartBoxClose, isub, xduration, xtransition)
{	
	var smartBoxes 	= $(document.body).getElements('div[id$=' + smartBoxSuffix + ']');
	var closeElem 	= $(document.body).getElements('.' + smartBoxClose);
	var closeBoxes 	= function(){smartBoxes.setStyle('display', 'none');};
	
	closeElem.addEvent('click', function(){closeBoxes()});
	
	var closeBoxesTimer = 0;
	
	smartBoxes.each(function(item)
	{
		smartBoxes.setStyle('display', 'none');
		var currentBox 	= item.getProperty('id').replace('' + smartBoxSuffix + '', '');
		var id = currentBox.replace('menu-', '');
		
		fx[id] = new Fx.Elements(item.getChildren(), {wait: false, duration: xduration, transition: xtransition, 
		onComplete: function(){item.setStyle('overflow', '')}});
		
		$(currentBox).addEvent('mouseleave', function(){
			$(currentBox).removeClass('hover');
			
			item.setStyle('overflow', 'hidden');
			fx[id].start({'0':{'height': [h[id], 0],'opacity': [1, 0]}});
			
			closeBoxesTimer = closeBoxes.delay(boxTimer);
		});
 
		item.addEvent('mouseleave', function(){
			
		});
 
		$(currentBox).addEvent('mouseenter', function(){
			fx[id].stop();
			$(currentBox).addClass('hover');
			if($defined(closeBoxesTimer)) $clear(closeBoxesTimer);
		});
 
		item.addEvent('mouseenter', function(){
			if($defined(closeBoxesTimer)) $clear(closeBoxesTimer);
		});
 
		item.setStyle('margin', 0);
		$(currentBox).addEvent('mouseenter', function()
		{						
			
			if(item.getStyle("height").toInt() < h[id] && h[id] != undefined && !window.ie && smartBoxSuffix == '_megamenu')
			{
				fx[id].stop();
				closeBoxes.delay(0);
				return false;
			}
			
			smartBoxes.setStyle('display', 'none');
			item.setStyles({display: 'block', position: 'absolute'}).setStyle('z-index', '1000000');
			item.setStyle('overflow', 'hidden');
			
			if(h[id] == undefined) { h[id] = item.getStyle("height").toInt(); item.getChildren().setStyle('height', 0); }
			fx[id].start({'0':{'height': [0, h[id]],'opacity': [0, 1]}});
			
			var WindowX		= window.getWidth();
			var boxSize 	= item.getSize().size;
			var inputPOS 	= $(currentBox).getCoordinates();
			var inputCOOR 	= $(currentBox).getPosition();
			var inputSize 	= $(currentBox).getSize().size;
			
			var inputBottomPOS 	= inputPOS.top + inputSize.y;
			var inputLeftPOS 	= inputPOS.left + xOffset;
			var inputRightPOS 	= inputPOS.right;
			var leftOffset 		= inputCOOR.x + xOffset;
			
			if(!isub) {
				item.setStyle('top', inputBottomPOS + yOffset);
				
				if(WindowX - inputLeftPOS - boxSize.x > 0) {
					item.setStyle('left', inputLeftPOS - 10);
				} else if(inputRightPOS > boxSize.x) {
					item.setStyle('left', inputRightPOS - boxSize.x + 10);
				} else {
					item.setStyle('left', (WindowX - boxSize.x)/2);
				}
			} else {
				if(WindowX - inputRightPOS - boxSize.x > 0) {
					item.setStyle('left', inputSize.x);
				} else if(inputRightPOS > boxSize.x) {
					item.setStyle('left', -boxSize.x);
				} else {
					item.setStyle('left', -(WindowX - boxSize.x));
				}
			}
		});
	});
};
