$(document).ready(function(){

	$(".topLink").click(
		function(e) {
			menuID = $(this).attr('id');
			if(menuID!=='homeButton') {
				e.preventDefault();
			}
		}
	);

	$(".megaClose").click(
		function(e) {
			e.preventDefault();
		}
	);

	$(".topLink").mousedown(function() {
		menuID = $(this).attr('id');
		if(menuID!=='homeButton') {
			menuID = menuID.substring(10,11);
			currentState = $("#megaMenu"+menuID).css('display');

			if(currentState=='none') {
				$(".megaContainer").hide();
				$("#nav li a").removeClass('topOpened');
				$("#megaMenu"+menuID).fadeIn('fast');
				$(this).addClass('topOpened');
			}

			if(currentState=='block') {
				$("#megaMenu"+menuID).fadeOut('fast');
				$(this).removeClass('topOpened');
			}
		}
	});

	$(".megaClose").mousedown(function() {
		$(".megaContainer").fadeOut('fast');
		$("#nav li a").removeClass('topOpened');
	});

	$(".megaContainer").mouseleave(function() {
		setTimeout(closeMega,'200');
	});

	function closeMega() {
		$(".megaContainer").fadeOut('fast');
		$("#nav li a").removeClass('topOpened');
	}
});

