// 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;

		},

		getSiteSelectorXml: function() {

			return siteSelectorXml;

		},

		initialize: function(callback, parms) {

			if(!parms) {

				parms = {

					controlXml: true,

					siteConfigXml: true,

					siteSelectorXml: false

				}

			}

			if(controlXml == undefined && parms.controlXml && loading["controlXml"] == false) initControlXml();

			if(siteConfigXml == undefined && parms.siteConfigXml) initSiteConfigXml();

			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 siteSelectorXml = undefined; // internal variable to hold the site selector xml

	

	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}; // 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 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

					$("#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();

			});

		},

		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;

			});

		},

		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

			$("#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) {

		$("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("/"));

		//alert(path);

		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(isActive($("> 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) {}; // 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

	};

};



// Boutiques nav initialisation

$(document).ready(function(){

	var getFiletype = function(src) {

		var filetype = src.substring(src.lastIndexOf(".")+1); // get file type

		return filetype;

	};

	var setOn = function(o) { // Function to set the "ON" nav image

		o = $(o);

		var src = $("img", o).get(0).src;

		var filetype = getFiletype(src);

		var re = new RegExp("\\."+filetype+"$", "g");

		$("img", o).get(0).src = src.replace(re, "_ON."+filetype);

	};

	var setOff = function(o) { // Function to set the "OFF" nav image

		o = $(o);

		var src = $("img", o).get(0).src;

		var filetype = getFiletype(src);

		var re = new RegExp("_ON\\."+filetype+"$", "g");

		$("img", o).get(0).src = src.replace(re, "."+filetype);

	};

	// Set active nav element

	$("ul.boutiques a").each(function(){

		if(mam.nav.isActive(this.href)) $(this).addClass("active");

	});

	// Set hover functionality for all nav elements except for the "active"

	$("ul.boutiques a").not(".active").jQIR("jpg", "/core/library/images/boutiques/");

	$("ul.boutiques a.active").jQIR("jpg", "/core/library/images/boutiques/", true);

	$("ul.boutiques a").not(".active").hover(function() {

		setOn(this);

	}, function() {

		setOff(this);

	});

});

// Core and Utility nav initialisation

$(document).ready(function(){

	// Set active nav element

	$("#core-nav ul a").each(function(){

		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: ""

		};

		jQuery.extend(o, parms);

		mam.config.initialize(function(){

			mam.page.loadStyleSheet();

			$(document).ready(function(){

				mam.page.loadLeftColumn(o.leftColumnImg);

				mam.page.setTitle(o.title);

				mam.page.setLogo();

				//mam.page.loadHeadings();

				mam.link.initialize();

				//$("*").hover(function(){ $(this).addClass("over"); },function(){ $(this).removeClass("over"); }); // fix ie hover bug

			});

		},{controlXml: true,siteConfigXml: 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(pageName) {

		var title;

		if(mam.config.getSiteConfigXml() != undefined) {

			title = $("site-name", mam.config.getSiteConfigXml()).text() + " - " + pageName;

			document.title = title;

		}

	}

};

/*******************************************************************************

* 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: "snsebd02.lon.mellonbank.com:30339",

		test: "mgi.test.mellon.com",

		qa: "mgi.qa.mellon.com",

		prod: "www.mellonglobalinvestments.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();

	$("site", mam.config.getControlXml()).each(function() {

		var currentSitePath = $("path", this).text();

		if(mam.util.dom.getElementsByAttribute($("host", this), "name", host).size() > 0) sitePath = currentSitePath;

	});

	return sitePath;

};

mam.util.path.getSiteConfigPath = function() {

	return mam.util.path.getSitePath()+"/site-config.xml";

};

mam.util.path.getSiteNavPath = function() {

	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;

	}

	// append query string

	if(parms.query != undefined && parms.query != "") url += "?"+parms.query;

	// alert("constructURL: "+url);

	return url;

};

mam.util.url.forward = function(parms) {

	if(!parms) {

		parms = {

			url: document.location.href,

			preserveHistory: false,

			test: false

		}

	}

	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

};


/*******************************************************************************

* Login clear fields code

*******************************************************************************/


 $(document).ready(function () {

$("#username").click(function(){
	
	this.value = ""
})

$("#password").focus(function(){
	
	this.value = ""
	
	//this.type = "password";
	
})
});



<!--
//This credit must stay intact
//Script by http://www.java-Scripts.net and http://wsabstract.com
 //function doClear(theText) {
   //  if (theText.value == theText.defaultValue) {
   //      theText.type = "password"
//     }
 //}
//-->



/*******************************************************************************

* Pop Up Code

*******************************************************************************/



function forwardPage(newLoc,type) {

  if ( type == "popup" ) {

	var w = 450 ; var h = 420 ;

	var winprops = "toolbar=no,width=" + w +",height=" + h +

	",directories=no,status=no,scrollbars=yes,resize=yes,menubar=no";

	var winl = (screen.width - w) / 2;

	var wint = (screen.height - h) / 2;

	winprops = winprops + ",top=" + wint + ",left= " + winl ;

	var newWin = window.open(newLoc,"myWin", winprops)                  	

  } else if ( type == "new" ){

	var newWin = window.open(newLoc,"myWin2")

  } else {

   if (parseInt(navigator.appVersion) >= 3) {

    if (type == "parent") {

      parent.location.replace(newLoc);

    } else if (type == "opener") {

      opener.location.replace(newLoc);

    } else {

      location.href = newLoc;

      location.replace(newLoc);

    }

   } else {

    if (type == "parent") {

      parent.location.href = newLoc;

    } else if (type == "opener") {

      opener.location.href = newLoc;

    } else {

      location.href = newLoc;

    }

   }

  }

}

/*******************************************************************************
* mam.popups
*******************************************************************************/


 mam.popups = {};
mam.popups.pdflink = function() {
	var linkURL = $(this).attr('href');
	var ext = linkURL.replace(/.*\.(.*)$/, "$1");
	
	var bnymellon = (linkURL.indexOf("bnymellon"));
	var dreyfus = (linkURL.indexOf("dreyfus"));
	var standish = (linkURL.indexOf("standish"));
	
	if((ext == 'pdf' || ext == 'doc') && ( bnymellon == -1 && dreyfus == -1 && standish == -1) ) {
		window.open(linkURL);
		return false;
	}	
};


$("document").ready(function(){
	$(".popup").click(function(){
		var theWindow = window.open(this.href);
		theWindow.focus();
		return false;
	});
	
	$("a").click(mam.popups.pdflink);

});
