mam.nav = new function() {
	// Public Methods
	jQuery.extend(this,  {
		version: "1.0",
		load: function(func) {
			$.get(mam.util.path.getSiteNavPath(), function(response) {
				if(mam.config.getSiteConfigXml() != undefined) {
					$("#site-nav > ul").append(response);
					showNavElements(mam.config.getSiteConfigXml()) // Show modules
					$("#site-nav ul li.coreNav").show(); // show all core nav modules
				} else {
					$("#site-nav ul li").show(); // show all modules if site config cannot be loaded
				}
				addNavBehaviour();
				setActive();
				if(func) func();
				
			});
		}
	});
	// Private Methods
	var behaviours = {
		behaviour_01: function() { // Show/Hide
			$("#site-nav > ul > li").hover(function()
							{ 
								$("#site-nav > ul > li").each(function() {
									$("+ ul", this).hide();
									$('a:first',this).removeClass('selected');
									$(this).removeClass("msieFix");
								});
								
								$('a:first',this).addClass('selected');
								$(this).addClass("msieFix");
								$("+ ul", this).show(); 
								
							}
							,
						   function()
						   	{  
						   		var thisactive = false;
						   		
						   		$(this).removeClass("msieFix");
								$('a:first',this).removeClass('selected');
						   		$("+ ul", this).hide(); 
						   		
						   		$("#site-nav > ul > li").each(function() {
								
									var thisactive = false;
								
									$("ul > li > a", this).each(function() {
															
										if(isActive($(this).attr('href')) )
										{
											$(this).addClass('selected');
											thisactive = true;
											
										}
								});
													
																								
								if(thisactive) {
									
									$('a:first',this).addClass('selected');
									$(this).addClass("msieFix");
									$("+ ul", this).show(); 
								}
																
																
						   	});
						   		
						   		
						   	}
	
						  	); // make css menus work in ie
							
							
							
		},
		behaviour_02: function() { // Slide Up/Slide Down
			$("#site-nav ul li").hover(function(){$(this).addClass("over");},function(){ $(this).removeClass("over"); }); // make css menus work in ie
			$("#site-nav > ul > li > a").click(function() {
				//alert($("#site-nav > ul > li > ul:visible").size());
				//alert($("#site-nav > ul > li > ul:visible").not($("+ ul", this).get(0)).size());
				$("#site-nav > ul > li > ul:visible").not($("+ ul", this).get(0)).slideUp("slow"); // Hide all visible second level navs
				$("+ ul", this).slideToggle("slow"); // Show requested second level navs
				if($("+ ul", this).size() == 0) return true; // if this nav element has no children then return true so that the link will fire
				return false;
			});
		},
		behaviour_03: function() { // Slide Up/Slide Down 1st and 2nd level
			$("#site-nav ul li").hover(function(){$(this).addClass("over");},function(){ $(this).removeClass("over"); }); // make css menus work in ie
			$("#site-nav > ul > li > a").click(function() {
				//alert($("#site-nav > ul > li > ul:visible").size());
				//alert($("#site-nav > ul > li > ul:visible").not($("+ ul", this).get(0)).size());
				$("#site-nav > ul > li > ul:visible").not($("+ ul", this).get(0)).slideUp("slow"); // Hide all visible second level navs
				$("+ ul", this).slideToggle("slow"); // Show requested second level navs
				if($("+ ul", this).size() == 0) return true; // if this nav element has no children then return true so that the link will fire
				return false;
			});
			$("#site-nav > ul > li > ul > li > a").click(function() {
				$("#site-nav > ul > li > ul > li > ul:visible").not($("+ ul", this).get(0)).slideUp("slow"); // Hide all visible third level navs
				$("+ ul", this).slideToggle("slow"); // Show requested second level navs
				if($("+ ul", this).size() == 0) return true; // if this nav element has no children then return true so that the link will fire
				return false;
			});
		}
	};
	var addNavBehaviour = behaviours["behaviour_01"] // onclick behaviour for level 1 nav
	var showNavElements = function(xml) {
		$("module", xml).each(
			function(i) {
				$("#site-nav ul li#"+this.getAttribute("id")+"-nav").show();
			}
		);
	};
	var isActive = function(href) {
	
		var path = getPath(href);
		
		// remove page name i.e. everything after final slash
		path = path.substring(0, path.lastIndexOf("/"));
		if(path == "") return false; // assume external link ie. www.bbc.co.uk Need to find a better solution to this.
		
		
		if(getCurrentPath().indexOf(path) > -1) 
		{
			// alert(getCurrentPath() + "\n" + path);
		}
		
		return (getCurrentPath().indexOf(path) > -1);
	};
	var getCurrentPath = function() {
		var path;
		if(mam.util.env.isTeamsite()) {
			path = document.location.pathname;
			path = path.replace(mam.util.env.getCurrentHost(), "");
			for(var i=0; i<3; i++) { // remove workarea and branch
				path = path.substring(path.indexOf("/") + 1);
				// alert("i: "+i+" Path: "+path);
			}
			path = "/" + path // prepend slash
		} else {
			path = document.location.pathname;
		}
		
		
		// alert("getCurrentPath: "+path);
		return path;
	};
	var getPath = function(href) {
		
		var path = href;
		
		if(href.indexOf('http') == 0) 
		{
			for(var i=0; i<3; i++) { // remove everything up to the 3rd slash - this removes protocol, authority and port
				path = path.substring(path.indexOf("/") + 1);
			}
			path = "/" + path // prepend slash
		}
	
		
		// alert("getPath: "+path);
		
		return path;
	};
	var setActive = function() {
		var setLevelActive = function(context, callbacks) {
			if($("> ul > li", context).size() == 0) return;
			$("> ul > li", context).each(function() {
				if(isActive($("> a", this).get(0).href)) {
					$(this).addClass("msieFix");
					$('a:first',this).addClass('selected');
					$("> ul > li", this).each(function() {
						if(isActive($("> a", this).get(0).href)) {
							$('a:first',this).addClass('selected');
						}
					});
					
					
					// var callback = callbacks[callbacks.length - 1]; // execute callback at the top of the stack
					// if(typeof callback == "function") callback(this);
					// callbacks.pop(); // remove callback from the top of the stack
					// setLevelActive(this, callbacks); // recursively calls itself untill all levels have been processed
				}
			});
		};
		var callbacks = []; // array of callback functions starting with the lowest level (acts as a stack i.e. level 1 is at the top of the stack)
		callbacks[callbacks.length] = function(that) {}; // level 3
		callbacks[callbacks.length] = function(that) {$("> ul", that).slideDown("slow");}; // level 2
		callbacks[callbacks.length] = function(that) {$("> ul", that).slideDown("slow");}; // level 1
		setLevelActive($("#site-nav"), callbacks); // Call set active with the site nav div as the context
	};
}
