$(document).ready(function () { 
	megaDropDown.init();
}); 

var megaDropDown = (function () {
	//	settings
	var contSelector = '#menu ul.m1st';
	
	// private vars
	var $cont,
		t;
	
	var init = function () {
		$cont = $(contSelector);
		
		$("ul.m2nd", $cont).each(function () {
			var h = $(this).height();
			$(this).data('height',h);
		});
		$("ul.m2nd", $cont).css({
			display : 'none',
			height: 0
		});
		
		// fade Out and in on dropdown elements
		$("ul.m2nd li a", $cont).hover(function () {
			$(this).fadeTo(100, 1);
			$(this).fadeTo(500, 1);
		});

		
		$(contSelector + " > li > a")
			.mouseover(function () {
				if ($(this).hasClass('active')) return;
				closeActive();
				var ul = $(this)
					.addClass('active')
					.siblings("ul.m2nd");
				if (/msie|MSIE 6/.test(navigator.userAgent)) {
					ul
					.css({
						display : 'block',
						height : ul.data('height')
					})
				} 
				else if (/msie|MSIE 7/.test(navigator.userAgent) || /msie|MSIE 8/.test(navigator.userAgent))  {
					ul
					.css({
						display : 'block'
					})
					.stop()
					.animate({
						height : ul.data('height')
					}, {
						duration : 250
					});
				}
				else {
					ul
					.css({
						opacity : 0,
						display : 'block'
					})
					.stop()
					.animate({
						height : ul.data('height'),
						opacity: 1
					}, {
						duration : 250
					});
				}
				
			});
		$cont
			.mouseover(function () {
				clearTimeout(t);
			})
			.mouseout(function () {
				//closeActive();
				t = setTimeout(closeActive, 250);
			});
	};
	

	var closeActive = function () {
		var $el = $(contSelector + " > li > a.active");
		var ul = $el.removeClass('active').siblings("ul.m2nd");
		if (/msie|MSIE 6/.test(navigator.userAgent)) {
			ul
			.css({
				display : 'none',
				height : 0
			})
		}
		else if (/msie|MSIE 7/.test(navigator.userAgent) || /msie|MSIE 8/.test(navigator.userAgent)) {
			ul.css({
				display: 'block'
			}).stop().animate({
				height: 0
			}, {
				duration: 250,
				complete: function(){
					$(this).hide();
				}
			});
		}
		else {
			ul.css({
				display: 'block'
			}).stop().animate({
				height: 0,
				opacity: 0
			}, {
				duration: 250,
				complete: function(){
					$(this).hide();
				}
			});
		}
	};
	
	
	return {
		init : init
	}
})();
