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";
		}
		
		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.url
mam.util.url = {};
mam.util.url.constructURL = function(parms) {
	var url;
	if(!parms) { // default parms
		parms = {
			host: "",
			pathname: document.location.pathname
		}
	}
	// alert("constructURL: parms.host = "+parms.host);
	if(parms.host && parms.host != "") {
		url = document.location.protocol+"//"+parms.host + parms.pathname;
	} else {
		url = parms.pathname;
	}
	// alert("constructURL: "+url);
	return url;
};
mam.util.url.constructURLTeamsite = function(parms) {
	var branch, workarea, url;
	if(!parms) { // default parms
		parms = {
			host: "",
			pathname: document.location.pathname
		}
	}
	if(parms.workarea == undefined)	{
		workarea = parms.host.substr(parms.host.lastIndexOf("/"));
		if(document.location.pathname.indexOf("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;
	}
	// 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.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
}
