// Define Array.indexOf for browsers that don't implements this
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(val, fromIndex) {
		if (typeof(fromIndex) != 'number') fromIndex = 0;
		for (var index = fromIndex,len = this.length; index < len; index++)
			if (this[index] == val) return index;
		return -1;
	};
}
// Fix IE6 background image flicker.
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {};
// create the mam namespace if it does not already exist
if(mam == undefined) var mam = {};
/*******************************************************************************
* mam.config
*******************************************************************************/
mam.config = new function() {
	// Public Methods - this is the API (these are infact Priviledged Methods as they have access to private members)
	jQuery.extend(this,  { // use the jQuery extend function to add methods to the current object (this)
		version: "1.0",
		getControlXml: function() {
			return controlXml;
		},
		getSiteConfigXml: function() {
			return siteConfigXml;
		},
		getSiteNavJson: function() {
			return siteNavJson;
		},
		getSiteSelectorXml: function() {
			return siteSelectorXml;
		},
		initialize: function(callback, parms) {
			if(!parms) {
				parms = {
					controlXml: true,
					siteConfigXml: true,
					siteNavJson: true,
					siteSelectorXml: false
				}
			}
			if(controlXml == undefined && parms.controlXml && loading["controlXml"] == false) initControlXml();
			if(siteConfigXml == undefined && parms.siteConfigXml) initSiteConfigXml();
			if(siteNavJson == undefined && parms.siteNavJson) initSiteNavJson();
			if(siteSelectorXml == undefined && parms.siteSelectorXml) initSiteSelectorXml();
			queue[queue.length] = callback;
			if(activeLoads == 0) {
				process();
			}
		},
		setControlXmlPath: function(path) {
			controlXmlPath = path;
		},
		setSiteSelectorXmlPath: function(path) {
			siteSelectorPath = path;
		}
	});
	var controlXml = undefined; // internal variable to hold the control xml
	var siteConfigXml = undefined; // internal variable to hold the site config xml
	var siteNavJson = undefined; // internal variable to hold the site nav json
	var siteSelectorXml = undefined; // internal variable to hold the site selector xml
	
	//var controlXmlPath = "/iw-mount/default/main/mcm_suite/control/WORKAREA/control/control.xml"; // path to load the control xml from
	var controlXmlPath = "/control/control.xml"; // path to load the control xml from
							// Note: site config xml path is determined held in the control xml
	var siteSelectorPath = "/control/site-selector.xml"; // path to load the site selector xml from
	
	var queue = []; // queue of functions to be executed on completion of initialisation
	var queues = []; // array of queues for syncronizing dependent config
	var blocking = []; // array to hold the blocking state of the above queues
	var loading = {controlXml: false, siteConfigXml: false, siteNavJson: false}; // array to hold the loading state of a particular config. Used when there is a dependency. 
				// A dependent canfig loader can check whether the config it depends on is currently being loaded before it decides to load it itself
	var timeout = []; // array of timeouts
	var activeLoads = 0; // variable to hold the numbver of loads currently active. Subsequent processing must wait until activeLoads = 0
	var loadID = 0; // Load ID assigned to each load. Used to identify metadata relating to the load such as its timeout process
	var queueIds = ["default", "controlXml"]; // registered syncronization queues
	for(var i=0; i<queueIds.length; i++) {
		queues[queueIds[i]] = [];
	}
	var asyncTimeout = 10; // seconds for async timeout
	var debug = false;
	
	// Private Methods
	var initControlXml = function() {
		loading["controlXml"] = true;
		var loadID = registerLoad();
		synchronize(function() {
			stop("controlXml");
			$.get(controlXmlPath,function(xml) {
				controlXml = xml;
				loading["controlXml"] = false;
				start("controlXml");
				loadComplete(loadID);
			});
		}, "controlXml");
	};
	var initSiteConfigXml = function() {
		if(controlXml == undefined && loading["controlXml"] == false) initControlXml();
		if(siteConfigXml == undefined && loading["siteConfigXml"] == false) { // do not load if alredy queued or loading
			loading["siteConfigXml"] = true;
			var loadID = registerLoad();
			synchronize(function() {
				stop("controlXml");
				$.get(mam.util.path.getSiteConfigPath(),function(xml) {
					siteConfigXml = xml;
					loading["siteConfigXml"] = false;
					start("controlXml");
					loadComplete(loadID);
				});
			}, "controlXml");
		}
	};
	var initSiteNavJson = function() {
		if(controlXml == undefined && loading["controlXml"] == false) initControlXml();
		if(siteNavJson == undefined && loading["siteNavJson"] == false) { // do not load if alredy queued or loading
			loading["siteNavJson"] = true;
			var loadID = registerLoad();
			synchronize(function() {
				stop("controlXml");
				$.getJSON(mam.util.path.getSiteNavJson(),function(json) {
					//alert(2);
					siteNavJson = json;
					loading["siteNavJson"] = false;
					start("controlXml");
					loadComplete(loadID);
				});
			}, "controlXml");
		}
	};
	var initSiteSelectorXml = function() {
		var loadID = registerLoad();
		$.get(siteSelectorPath,function(xml) {
			siteSelectorXml = xml;
			loadComplete(loadID);
		});
	};
	var synchronize = function(callback, id) {
		if(!id) id = "default";
		queues[id][queues[id].length] = callback;
		printDebug("synchronize");
		if(!blocking[id]) {
			processQueue(id);
		}
	};
	var stop = function(id) {
		if(!id) id = "default";
		blocking[id] = true;
		timeout[id] = setTimeout(function(){start(id);}, asyncTimeout * 1000);
		printDebug("stop");
	};
	var start = function(id) {
		if(!id) id = "default";
		if(timeout[id])
			clearTimeout(timeout[id]);
		blocking[id] = false;
		printDebug("start");
		processQueue(id);
	};
	var process = function(id) {
		if(!id) id = "default";
		while(queue.length && activeLoads == 0) {
			var call = queue[0];
			queue = queue.slice(1);
			call();
		}
	};
	var processQueue = function(id) {
		if(!id) id = "default";
		while(queues[id].length && !blocking[id]) {
			printDebug("process");
			var call = queues[id][0];
			queues[id] = queues[id].slice(1);
			call();
		}
	};
	var registerLoad = function() {
		activeLoads++;
		var loadID = getNextLoadID();
		timeout[loadID] = setTimeout(function(){loadComplete(loadID);}, asyncTimeout * 1000);
		return loadID
	};
	var loadComplete = function(loadID) {
		if(timeout[loadID])
			clearTimeout(timeout[loadID]);
		activeLoads--;
		process();
	};
	var getNextLoadID = function() {
		return loadID++;
	};
	var printDebug = function(caller) {
		if(this.debug) {
			for(var i=0; i<this.queueIds.length; i++) {
				$("#debug").append(caller+": queue id: "+this.queueIds[i]+" queue length: "+this.queues[this.queueIds[i]].length+" blocking: "+this.blocking[this.queueIds[i]]+"<br/>");
			}
			$("#debug").append("<br/>");
		}
	};
};
/*******************************************************************************
* mam.nav
*******************************************************************************/


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
					showNavElementsSorted(displayArray.sort()); // 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
				}
				showNavElements(mam.config.getSiteNavJson()); // Show modules
			});
			*/
			

      function ltrim(str){
                return str.replace(/ /g,"_");
            }
	
			
			
			var xmlFile = (mam.config.getSiteConfigXml() != undefined) ? mam.config.getSiteConfigXml() : null;
			var items = (mam.config.getSiteNavJson() != undefined) ? mam.config.getSiteNavJson().items : null;
			//alert(3);
			var output = "<ul>";
			for (i in items.levels){
				for (j in items.levels[i].level0){
					if(items.levels[i].level0[j].url != '' &&  typeof items.levels[i].level0[j].url != 'undefined'){
						
						output += "<li class=\""+ ltrim(items.levels[i].level0[j].title) +"\"><a href=\"" + items.levels[i].level0[j].url + "\" class=\"toplevel\"><span class=\"nav-text\">" + items.levels[i].level0[j].title + "</span></a>";
						output += (items.levels[i].level0[j].level1) ? "<ul>" : "";
							for (k in items.levels[i].level0[j].level1){
								if(items.levels[i].level0[j].level1[k].url != '' &&  typeof items.levels[i].level0[j].level1[k].url != 'undefined'){
									
									output += "<li class=\""+ ltrim(items.levels[i].level0[j].level1[k].title) +"\"><a href=\"" + items.levels[i].level0[j].level1[k].url + "\" class=\""+ ltrim(items.levels[i].level0[j].level1[k].title) +"\"><span class=\"nav-text\">" + items.levels[i].level0[j].level1[k].title + "</span></a>";
									output += (items.levels[i].level0[j].level1[k].level2) ? "<ul>" : "";
										for (l in items.levels[i].level0[j].level1[k].level2){
											if(items.levels[i].level0[j].level1[k].level2[l].url != '' &&  typeof items.levels[i].level0[j].level1[k].level2[l].url != 'undefined'){
												output += "<li><a href=\"" + items.levels[i].level0[j].level1[k].level2[l].url + "\"><span class=\"nav-text\">" + items.levels[i].level0[j].level1[k].level2[l].title + "</span></a></li>";
											}
										}
									output += (items.levels[i].level0[j].level1[k].level2) ? "</ul></li>" : "";
								}
							}
						output += (items.levels[i].level0[j].level1) ? "</ul></li>" : "";
					}
				}
			}
			output += "</ul>";

			//alert ("output = " + output);
			$("#site-nav").empty().append(output);
			$("#site-nav ul li").show();
			addNavBehaviour();
			setActive();
			
			if(func) func();
			
			$("a[class='toplevel']").click(function() {
				var v = $('span:first', this).html().toLowerCase();
				
				
				if(v == 'collective fund prices') {
					
					$(this).parent().find('ul').show();
					return false;
				} else {
					return true;
				}
			});
			
			$("a").click(function() {

				var href = $(this).attr('href');
				
				if(mam.util.env.isTeamsite()) {
					href = href.replace(/^(.*?\/clients\/)(.)([^\/].*)$/, "$1$2/$2$3");
				}

				if(href.match("\.pdf$") || href.match('\/clients\/')) {
					window.open(href);
					return false;
				}
				
				if(mam.util.env.isTeamsite()) {
					window.location.href = href;
					return false;
				}
				
			});
			
		},
		isActive: function(href) {
				return isActive(href);
		}
	});
	// Private Methods
	var behaviours = {
		behaviour_01: function() { // Show/Hide
			$("#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() {
				$("+ ul", this).toggle(); // toggle display
				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").hover(function(){
						
				$(this).find('ul').show();
				},function(){
				$(this).find('ul').hide();
			
				});
			
			
		},
		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;
			});
		},
		behaviour_04: function() { // Slide Up/Slide Down 1st and 2nd level
			
		
		$(".mcm_wrappers_home_body > #page-container > #content-container > #left-column > #site-nav > ul > li").hover(function(){
									
						$(this).find('ul').show();
						},function(){
						$(this).find('ul').hide();
					
				});
				
		$("#page-container > #content-container > #left-column > #site-nav > ul > li").hover(function(){$(this).addClass("over");},function(){ $(this).removeClass("over"); }); // make css menus work in ie	
		
		
		}
	};
	var addNavBehaviour = behaviours["behaviour_04"]; // onclick behaviour for level 1 nav
	var showNavElements = function(xml) {
	 alert(1);
		$("module", xml).each(
			function(i) {
				$("#site-nav ul li#"+this.getAttribute("id")+"-nav").show();
			}
		);
	};
	
	var showNavElementsSorted = function(displayArrayItems) {
	 alert(1);
		$(displayArrayItems).each(
			function(i) {
				//alert($("#site-nav ul li#"+this-1[1]+"-nav") + " -- " + $("#site-nav ul li#"+this-1[1]+"-nav"));
				$("#site-nav ul li#"+this[1]+"-nav").after( $("#site-nav ul li#"+this-1[1]+"-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("/"));
		//alert('path: ' + path);
		//alert('current path: ' + getCurrentPath());
		
		if(window.location.href.indexOf('/private/funds/content/publications/') > -1) { return false; }
		if(window.location.href.indexOf('/private/funds/content/procedures_disclosures/') > -1) { return false; }
		
		if(path == "") return false; // assume external link ie. www.bbc.co.uk Need to find a better solution to this.

		

		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($("> a", this).get(0).href.indexOf('/private/statements') > -1) {
					if(mi0.hideStatements) {
						$(this).hide();
					}
				}
				if(isActive($("> a", this).get(0).href)) {
					// alert($("> a", this).get(0).href);
					$(this).addClass("active");
					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 until 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) {$("> ul", that).slideDown("slow");} // 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
	};
};

// Core and Utility nav initialisation
$(document).ready(function(){
	// Set active nav element
	$("#core-nav ul a").each(function(){
		// alert("active: "+mam.nav.isActive(this.href)+", href: "+this.href);
		if(mam.nav.isActive(this.href)) $(this).addClass("active");
	});
	$("#utility-nav ul a").each(function(){
		if(mam.nav.isActive(this.href)) $(this).addClass("active");
	});
});
/*******************************************************************************
* mam.page
*******************************************************************************/
// create the mam namespace if it does not already exist
if(mam == undefined) var mam = {};
mam.page = {
	version: "1.0",
	clearLogo: function() {
		$("#logo").html("");
	},
	clearTitle: function() {
		document.title = "";
	},
  	loadContent: function(id) {
	var path;
     path = mam.util.dom.getElementsByAttribute($("content pages page", mam.config.getSiteConfigXml()), "id", id).get(0).getAttribute("path");
	   $("#"+id).load(path, function() {
			   $("*").hover(function(){ $(this).addClass("over"); },function(){ $(this).removeClass("over"); }); // fix ie hover bug
	   });
	},
	loadQuickLinks: function() {
		//alert("load quick links...")
		$("#left-column").prepend("<div id=\"quickLinks\"></div>");
		$("#quickLinks").load(mam.util.path.getSitePath()+"/quick_links.html", function() {
			if($("#quickLinks ul li").size() > 0) { $("#quickLinks").show();}
			else {
				$("#quickLinks").hide();
			}
		});
	},
	loadStyleSheet: function() {
		var css;
		if(mam.config.getSiteConfigXml() != undefined) {
			css = $("css", mam.config.getSiteConfigXml()).text();
			// only works in FF. Need to find a way of doing this cross browser
			if(css != undefined && css != "") $("head").append('<link rel="stylesheet" type="text/css" href="'+css+'" media="all"/>'); // ff
		}
	},
	loadHeadings: function() {
		$("h1").jQIR("gif", "/core/library/images/headings/h1/");
		$("h2").jQIR("gif", "/core/library/images/headings/h2/");
		$("h3").jQIR("gif", "/core/library/images/headings/h3/");
		$("h1").jQIR("jpg", "/core/library/images/headings/h1/");
		$("h2").jQIR("jpg", "/core/library/images/headings/h2/");
		$("h3").jQIR("jpg", "/core/library/images/headings/h3/");
	},
	loadLeftColumn: function(parms) {
		var o = {};
		jQuery.extend(o, parms);
		if(o.title == undefined) {o.title = o.alt;}
		if(mam.util.env.isDefaultHost()) {
			var img = $("<img>");
			$(img).attr(
			{
				src: o.src,
				alt: o.alt,
				title:  o.title
			});
			// $("#left-column").empty();
			if(o.src != undefined && o.src != "") {$("#left-column").append(img);}
		} else {
			mam.nav.load();
		}
	},
	loadPage: function(parms) {
		// alert("loadPage");
		mam.hub.pageHandler(); // do they need to go to the hub page
		var o = {
			title: "",
			separator: " - "
		};
		jQuery.extend(o, parms);
		mam.config.initialize(function(){
			mam.page.loadStyleSheet();
			$(document).ready(function(){
				mam.page.loadLeftColumn(o.leftColumnImg);
				mam.page.setTitle(o);
				// mam.page.setLogo();
				// mam.page.loadHeadings();
				$("*").hover(function(){ $(this).addClass("over"); },function(){ $(this).removeClass("over"); }); // fix ie hover bug
			});
		},{controlXml: true,siteConfigXml: true, siteNavJson: true});
	},
	setLogo: function() {
		var alt, logo, src, title;
		if(mam.config.getSiteConfigXml() != undefined) {
			this.clearLogo();
			alt = $("logo alt", mam.config.getSiteConfigXml()).text();
			src = $("logo src", mam.config.getSiteConfigXml()).text();
			title = $("logo title", mam.config.getSiteConfigXml()).text();
			$("#logo").html("<img src=\""+src+"\" alt=\""+alt+"\" title=\""+title+"\"/>");
			$("#logo img").show();
		}
	},
	setTitle: function(parms) {
		var title;
		if(mam.config.getSiteConfigXml() != undefined) {
			title = $("site-name", mam.config.getSiteConfigXml()).text() + parms.separator + parms.title;
			document.title = title;
		}
	}
};
/*******************************************************************************
* mam.popups
*******************************************************************************/
$("document").ready(function(){
	$(".popup").click(function(){
		var theWindow = window.open(this.href);
		theWindow.focus();
		return false;
	});
});
/*******************************************************************************
* mam.util
*******************************************************************************/
// create the mam namespace if it does not already exist
if(mam == undefined) var mam = {};
mam.util = {};
mam.util.version = "1.0";

// mam.util.dom
mam.util.dom = {};
mam.util.dom.filterSelect = function(values, selectElement) {
	for(var i=0; i<selectElement.options.length; i++) {
		var remove = true;
		values.each(function() {
			if(this.getAttribute("id") == selectElement.options[i].value || selectElement.options[i].value == "") remove = false; 
		});
		if(remove) {
			// alert("remove "+selectElement.options[i].value);
			selectElement.remove(i);
			i--;
		}
	};
};
mam.util.dom.getAbsoluteLeft = function(o) {
	// Get an object left position from the upper left viewport corner
	// o = document.getElementById(objectId)
	oLeft = o.offsetLeft;            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent;    // Get parent object reference
		oLeft += oParent.offsetLeft; // Add parent left position
		o = oParent;
	}
	return oLeft;
};
mam.util.dom.getAbsoluteTop = function(o) {
	// Get an object top position from the upper left viewport corner
	// o = document.getElementById(objectId)
	oTop = o.offsetTop;            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent;  // Get parent object reference
		oTop += oParent.offsetTop;// Add parent top position
		o = oParent;
	}
	return oTop;
};
mam.util.dom.getElementsByAttribute = function(inputElements, attributeName, attributeValue) {
	var elements = [];
	inputElements.each(function() {
		if(this.getAttribute(attributeName) == attributeValue) {
			elements[elements.length] = this;
		}
	});
	return $(elements);
};
mam.util.dom.initSelect = function(options, selectElement) {
	selectElement.options.length = 0;
	options.each(function(i) {
		selectElement.options[i] = new Option($(this).text(), this.getAttribute("id"), false, false);
	});
};

// mam.util.env
mam.util.env = new function() {
	var defaultHosts = {
		teamsite: "/iw-mount/default/main/mgi_suite/core",
		localhost: "localhost:8080",
		dev: "ebd01.lon.mellonbank.com:30339",
		test: "mgi.test.mellon.com",
		qa: "mgi.qa.mellon.com",
		prod: "www.bnymellonam.com"
	};
	this.getCurrentHost = function() {
		var host;
		host = window.location.host;
		// alert("getCurrentHost: "+host);
		return host;
	};
	this.getCurrentHostTeamsite = function() {
		var host;
		host = window.location.pathname;

		// strip /WORKAREA and everything after
		if(host.indexOf("/WORKAREA") > -1) host = host.substring(0,host.indexOf("/WORKAREA"));
		if(host.indexOf("/STAGING") > -1) host = host.substring(0,host.indexOf("/STAGING"));
		if(host.indexOf("/EDITION") > -1) host = host.substring(0,host.indexOf("/EDITION"));
		//alert("getCurrentHost: "+host);
		return host;
	};
	this.getDefaultHost = function(env) {
		if(env == undefined) env = this.getEnv();
		//alert("getDefaultHost: "+defaultHosts[env]);
		return defaultHosts[env];
	};
	this.getEnv = function() {
		var env;
		var hostname = window.location.hostname;
		if (hostname.indexOf("localhost") > -1 || hostname.indexOf("xpwmebs110328") > -1 ) {
			env = "localhost";
		} else if (hostname.indexOf("ebd01") > -1 || hostname.indexOf("snsebd02") > -1) {
			env = "dev";
		} else if (hostname.indexOf("test.mellon") > -1 ) {
			env = "test";
		} else if (hostname.indexOf("sn84") > -1 ) {
			env = "qa";
		} else if (hostname.indexOf("qa.mellon") > -1 ) {
			env = "qa";
		} else if (hostname.indexOf("sn83") > -1 ) {
			env = "prod";
		} else {
			env = "prod";
		}
		// alert("getEnv: "+env);
		return env;
	};
	this.getEnvTeamsite = function() {
		var env = "teamsite";
		// alert("getEnvTeamsite: "+env);
		return env;
	};
	this.isDefaultHost = function(host) {
		if(host == undefined) host = this.getCurrentHost();
		for(var n in defaultHosts) {
			if(host == defaultHosts[n]) return true;
		}
		return false;
	};
	this.isTeamsite = function(hostname, port) {

		if(hostname == undefined) {
			hostname = window.location.hostname;
			port = window.location.port;
		}
		// alert("hostname: "+hostname+"\nport: "+port);
		if ((hostname.indexOf("ebd01") > -1 && port == "91") || (hostname.indexOf("sn84") > -1 ) || (hostname.indexOf("sn83") > -1 )) {
			return true;
		} else {
			return false;
		}
	};
};

// mam.util.path
mam.util.path = {};
mam.util.path.getSitePath = function() {
	var host, sitePath = "/sites/default_site";
	host = mam.util.env.getCurrentHost();
	 //alert("host = " + host);
	$("site", mam.config.getControlXml()).each(function() {
		 //alert($("path", this).text());
		var currentSitePath = $("path", this).text();
		//alert("currentSitePath = " + currentSitePath);
		if(mam.util.dom.getElementsByAttribute($("host", this), "name", host).size() > 0) {
			sitePath = currentSitePath;
		}
	});
	if(document.location.href.indexOf('/private/marketing_hedge') > -1) {
		return '/private/marketing_hedge';
	}
	if(document.location.href.indexOf('/private/') > -1) {
		return '/private/funds/content';
	}
	// alert("sitePath= " + sitePath);
	return sitePath;
};
mam.util.path.getSiteConfigPath = function() {
	//return "/iw-mount/default/main/mcm_suite/sites/default_site/WORKAREA/default_site/" + mam.util.path.getSitePath()+"/site-config.xml";
	if(window.location.href.indexOf('/private/marketing_hedge') > -1) {
		return '/private/marketing_hedge/site-config.xml';
	} else if(window.location.href.indexOf('/private/') > -1) {
		return '/private/funds/content/site-config.xml';
	} else {
		return '/sites/default_site/site-config.xml';
	}
	
	/*
	var v = mam.util.path.getSitePath()+"/site-config.xml";
	if(v == '/sites/client_reporting/site-config.xml') {
		return '/private/funds/content/site-config.xml';
	}
	return mam.util.path.getSitePath()+"/site-config.xml"; */
	
};
mam.util.path.getSiteNavJson = function() {
	//return  "/iw-mount/default/main/mcm_suite/sites/default_site/WORKAREA/default_site/" +mam.util.path.getSitePath()+"/site-config.js";
	
	if(window.location.href.indexOf('/private/marketing_hedge') > -1) {
		if(window.location.href.indexOf('intl_alternative_investments/fund') > -1) {
			return '/private/marketing_hedge/intl_alternative_investments/site-config.js';
		} else if(window.location.href.indexOf('intl_alternative_investments') > -1) {
			return '/private/marketing_hedge/intl_alternative_investments/site-config-restricted.js';
		} 
		if(window.location.href.indexOf('alternative_investments/fund') > -1) {
			return "/private/marketing_hedge/alternative_investments/site-config.js";
		} else if(window.location.href.indexOf('alternative_investments') > -1) {
			return '/private/marketing_hedge/alternative_investments/site-config-restricted.js';
		}
		return '/private/marketing_hedge/site-config.js'; 
	} else if(window.location.href.indexOf('/private/') > -1) {
		return '/private/funds/content/site-config.js';
	} else {
		return '/sites/default_site/site-config.js';
	}
	/*
	var v =  mam.util.path.getSitePath()+"/site-config.js";
	if(v == '/sites/client_reporting/site-config.js') {
		return '/private/funds/content/site-config.js';
	}
	return  mam.util.path.getSitePath()+"/site-config.js";
	*/
	
};
mam.util.path.getSiteNavPath = function() {
	//return  "/iw-mount/default/main/mcm_suite/sites/default_site/WORKAREA/default_site/" +mam.util.path.getSitePath()+"/nav.html";
	return  mam.util.path.getSitePath()+"/nav.html";
};
mam.util.path.getSiteImportantInfoPath = function() {
	return mam.util.path.getSitePath()+"/important_information/important_information.html";
};


// mam.util.site
mam.util.site = {};
mam.util.site.getSiteId = function() {
	var host, siteId = "default";
	host = mam.util.env.getCurrentHost();
	$("site", mam.config.getControlXml()).each(function() {
		var currentSiteId = $(this).attr("id");
		if(mam.util.dom.getElementsByAttribute($("host", this), "name", host).size() > 0) siteId = currentSiteId;
	});
	//return "mellon-uk-wholesale";
	return siteId;
};


// mam.util.url
mam.util.url = {};
mam.util.url.getPath = function() {
	return document.location.pathname;
};
mam.util.url.getPathTeamsite = function() {
	var pathname = document.location.pathname;
	// strip everything up to the end of /WORKAREA
	if(pathname.indexOf("/WORKAREA") > -1) pathname = pathname.substring(pathname.indexOf("/WORKAREA") + 9);
	if(pathname.indexOf("/STAGING") > -1) pathname = pathname.substring(pathname.indexOf("/STAGING") + 9);
	if(pathname.indexOf("/EDITION") > -1) pathname = pathname.substring(pathname.indexOf("/EDITION") + 9);

	// remove everything preceding the second / (remove the workarea name)
	pathname = pathname.substring(pathname.indexOf("/") + 1);
	pathname = pathname.substring(pathname.indexOf("/"));
	
	return pathname;
};
mam.util.url.constructURL = function(p) {
	var url;
	var parms = {
			host: "",
			pathname: document.location.pathname
	}; // defaults 
	jQuery.extend(parms, p);
	// alert("constructURL: parms.host = "+parms.host);
	if(parms.host && parms.host != "") {
		url = document.location.protocol+"//"+parms.host + parms.pathname;
	} else {
		url = parms.pathname;
	}
	// append query string
	if(parms.query != undefined && parms.query != "") url += "?"+parms.query;
	// alert("constructURL: "+url);
	return url;
};
mam.util.url.constructURLTeamsite = function(p) {
	var branch, workarea, url;
	var parms = {
			host: "",
			pathname: document.location.pathname
	}; // defaults 
	jQuery.extend(parms, p);
	if(parms.workarea == undefined)	{
		workarea = parms.host.substr(parms.host.lastIndexOf("/"));
		if(document.location.pathname.indexOf("WORKAREA/import/") > -1) workarea = "/import" // check if we are in import
	} else {
		workarea = parms.workarea;
	}
	if(parms.branch == undefined) {
		if(document.location.pathname.indexOf("WORKAREA") > -1) branch = "/WORKAREA";
		if(document.location.pathname.indexOf("STAGING") > -1) branch = "/STAGING";
		if(document.location.pathname.indexOf("EDITION") > -1) branch = "/EDITION";
	} else {
		branch = parms.branch;
	}
	 alert("constructURL: parms.host = "+parms.host);
	if(parms.host && parms.host != "") {
		url = parms.host + branch + workarea + parms.pathname;
	} else {
		url = parms.pathname;
	}
	// If the url is external, do not prepend the teamsite stuff
	// alert(parms.external);
	if(parms.external == true) {
		url = document.location.protocol+"//"+parms.host + parms.pathname;
	}
	// append query string
	if(parms.query != undefined && parms.query != "") url += "?"+parms.query;
	 //alert("constructURL: "+url);
	 //alert("constructURL: "+url);
	return url;
};
mam.util.url.forward = function(p) {
	var parms = {
		url: document.location.href,
		preserveHistory: false,
		test: false
	}; // defaults 
	jQuery.extend(parms, p);
	if(parms.test) return parms.url;
	if(parms.preserveHistory) {
		document.location.href = parms.url;
	} else {
		document.location.replace(parms.url);
	}
};

// inject teamsite methods if the environment is teamsite
if(mam.util.env.isTeamsite()) {
	mam.util.url.getPath = mam.util.url.getPathTeamsite; // inject teamsite version of getPath method
	mam.util.url.constructURL = mam.util.url.constructURLTeamsite; // inject teamsite version of constructURL method
	mam.util.env.getCurrentHost = mam.util.env.getCurrentHostTeamsite; // inject teamsite version of getCurrentHost method
	mam.util.env.getEnv = mam.util.env.getEnvTeamsite; // inject teamsite version of getEnv method
};

/*******************************************************************************
* mam.popups
*******************************************************************************/
/*mam.popups = {};
mam.popups.pdflink = function() { 
	var l = $(this).attr('href');
	var ext = l.replace(/.*\.(.*)$/, "$1");
	if(ext == 'pdf' || ext == 'doc' ) {
		window.open(l);
		return false;
	}
};
$("document").ready(function(){
	$(".popup").click(function(){
		var theWindow = window.open(this.href);
		theWindow.focus();
		return false;
	});
	
	$("a").click(mam.popups.pdflink);

});
*/
/*******************************************************************************
* mtabbed list code
*******************************************************************************/

$("document").ready(function(){
		$('#tl-container').tabs({ fxAutoHeight: false });
	});
