$(document).ready(function(){
	var topNavItems = new Array($("#nav ul li.level1").length);
	var lastClickedTopNavItemIndex = -1;
	$.each(topNavItems, function(index, value) {
		topNavItems[index] = false;
	});
	
	if (Modernizr.touch) {
		$('#nav ul li.level1').click(
			function() {
				// Find which item was clicked
				var clickedTopNavItemIndex = $("#nav ul li.level1").index(this);
				
				// If it's the second click on top level item then take 
				// users to that page; otherwise, just show 2nd level navigation
				if (topNavItems[clickedTopNavItemIndex] && (clickedTopNavItemIndex == lastClickedTopNavItemIndex)) {
					
					// Reset all navigation items
					$.each(topNavItems, function(index, value) {
						topNavItems[index] = false;
					});
					return true;
					
				} else {
					// Mark item as 1-time clicked
					topNavItems[clickedTopNavItemIndex] = true;
					
					// Hide all 2nd level navigation items
					$('#nav ul li.level1 ul').css('display', 'none'); 
					
					// Display 2nd level navigation only for the clicked top level one
					$('ul', this).css('display', 'block');
					
					lastClickedTopNavItemIndex = clickedTopNavItemIndex;
					
					return false;
				}
			},
			function() { $('ul', this).css('display', 'none'); }
		);
	} else {
		$('#nav ul li.level1').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); });
	}
});
