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()}).setStyle('cursor', 'pointer');
	
	var closeBoxesTimer = 0;
	var fx = Array();
	var h  = Array();
	
	smartBoxes.each(function(item, i)
	{
		smartBoxes.setStyle('display', 'none');		
		var currentBox 	= item.getProperty('id');
		currentBox 		= currentBox.replace('' + smartBoxSuffix + '', '');
		
		fx[i] = new Fx.Elements(item.getChildren(), {wait: false, duration: xduration, transition: xtransition});
		
		$(currentBox).addEvent('mouseleave', function(){
			$(currentBox).removeClass('hover');
			
			
			
			closeBoxesTimer = closeBoxes.delay(boxTimer);
		});
 
		item.addEvent('mouseleave', function(){
			if($defined(closeBoxesTimer)) $clear(closeBoxesTimer);
		});
 
		$(currentBox).addEvent('mouseenter', function(){						
			$(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()
		{			
			smartBoxes.setStyle('display', 'none');
			item.setStyles({display: 'block', position: 'absolute'}).setStyle('z-index', '1000000');
			
			var o  = {};
			if(h[i] == null)
				h[i] = item.getStyle("height").toInt();
			o[0] = {height: [0, h[i]]};
			item.getChildren().setStyle('height', '0px');
			fx[i].start(o);
			
			//coordinates and size vars and math
			var halfWindowY = window.getHeight() / 2;
			var halfWindowX = window.getWidth() / 2;
			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 inputBottomPOSAdjust 	= inputBottomPOS - window.getScrollHeight();
			
			var inputLeftPOS 	= inputPOS.left + xOffset;
			var inputRightPOS 	= inputPOS.right;
			var leftOffset 		= inputCOOR.x + xOffset;
			
			if(halfWindowY < inputBottomPOSAdjust)
			{
				if(!isub)
				{
					item.setStyle('top', inputPOS.top - boxSize.y - yOffset);
					if((boxSize.x + inputLeftPOS) < (2 * halfWindowX)){item.setStyle('left', leftOffset);}
					else{item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset);};
				}
			}
			else
			{
				if(!isub)
				{
					item.setStyle('top', inputBottomPOS + yOffset - 5);
					if((boxSize.x + inputLeftPOS) < (2 * halfWindowX - 50)){item.setStyle('left', leftOffset);}
					else{item.setStyle('left', (inputPOS.right - boxSize.x) - xOffset);};
				}
				else
				{
					if((boxSize.x + inputLeftPOS) < (2 * halfWindowX - 50)){item.setStyle('left', inputSize.x);}
					else{item.setStyle('right', inputSize.x);}
				}
			};
		}).setStyle('cursor', 'pointer');
	});
};
/*window.addEvent('domready', function() {
	ZTMegaMenu(
		700, //delay before vanishing
		0, //x offset
		0,  //y offset
		'_megamenu', //smart hover box suffix
		'megamenu_close', //hover box close class
		false,
		2000, //duration
		Fx.Transitions.Bounce.easeOut //Transition
	   );
	
	var megas = $(document.body).getElements('div[class="menusub_mega"]');
	megas.each(function(mega, i){
		var id = mega.getProperty('id').split('_');
		if(id[2] != null)
		{
			var smart = '_' + id[1] + '_' + id[2];
			ZTMegaMenu(
				700, //delay before vanishing
				0, //x offset
				0,  //y offset
				smart, //smart hover box suffix
				'megamenu_close', //hover box close class
				true,
				2000, //duration
				Fx.Transitions.Bounce.easeOut //Transition
		   );
		}
	});	
});*/
