
jQuery.noConflict();
jQuery(document).ready(function($) {

	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {

	// Lets add close buttons to all of the level2 menus
	$(".level2").append("<button class=\"largeCloseButton\">Close</button>");
	$(".level2 > .largeCloseButton").click(function() {
	  closeSubNav();
	  return false;
	});


	// When a top level nav link is clicked...
	$("#mainNavigation > .globalMenu > li > a").click(function(){
	  clickedNav($(this).parent());
	});


	//Functions

	function clearSubs() {
		$("#mainNavigation .level2").css("display", "none");
		$("#mainNavigation").removeClass("subNav");

		// Set all sub navs activeSubNav attribute to false
		$("#mainNavigation > ul > li").attr("activeSubNav", false);
	}
	
	
	function activateSubNav(item) {
		
		// Clear the current sub nav or .contentBox to get ready for the clicked one,
		clearSubs();
	
	     // Display the subNav item
          $("#mainNavigation ."+item+" .level2").css("display","block");
          $("#mainNavigation").addClass("subNav");
          $("#mainNavigation ."+item).attr("activeSubNav", true);
	
	}
	
	
	function clickedNav(clickedItem) {
	
	        // If there is no level2 nav under this item, exit
	        if(!$(clickedItem).children(".level2").attr("class")) return false;
	
	        // If the clicked item is already the active subNav, clear subs
	        if($(clickedItem).attr("activeSubNav") == "true") {
	        	clearSubs();
	        	return false;
	        }
	
	        // Activate this item's level2
	        activateSubNav(
	            $(clickedItem)
	            .attr("class")
	            .replace(/selectedPageRoot/g, "")
	            .replace(/selectedPage/g, "")
	            .replace(/hasChildren/g, "")
	            .replace(/ /g, "")
	        );
	
	    }
	
	
	function closeSubNav() {
	    // Get rid of subNavs
	    clearSubs();	
	}

	}

});

