window.CustomLayoutProcessing = new function() {
	function for2each(col, func) {
		if (col) {
			var intLength = col.length;
			if (intLength) {
				for(var i = 0; i < intLength; i++) {
					if (func(col[i], i, col)) {
						return true;
					}
				}
			}
			else {
				var child = col.firstChild;
				var i = 0;
				while(child) {
					if (child.nodeType == 1 && func(child, i, col)) {
						return true;
					}
					child = child.nextSibling;
					i++;
				}
			}
		}
	}

	this.preLayoutProcessing = function()
	{
		var direntry = document.getElementById("entry");
		var oPageH1 = findFirstElementOfClass(document,"h1","page");
		var oPageHeadTitle = first(document.getElementsByTagName("title"));
		if (direntry)
		{
			var panel = findFirstElementOfClass(direntry,"div","panel");
			if (panel)
			{
				var info = findFirstElementOfClass(panel,"dl","Info");
				if (info)
				{
					var oProperties = [];
					var oCurrentPropertyName = "";
					var removeDD = true;
					var strPropertyName = "";
					for2each(map(info), function(node) {
						if (node.tagName)
						{
							if (node.tagName == "DD")
							{
								if (removeDD)
								{
									strPropertyName = RTrim(oCurrentPropertyName);
									oProperties[strPropertyName] = RTrim(node.innerHTML);
									node.parentNode.removeChild(node);
								} else {
									removeDD = true;
								}
							}
							if (node.tagName == "DT")
							{
								oCurrentPropertyName = node.innerHTML;
								if (oCurrentPropertyName.indexOf("Description") == 0)
								{
									removeDD = false;
								}
								node.parentNode.removeChild(node);								
							}
						}
					});
					var oPageTitle = "";
					if (oProperties["Name"]) {
						oPageTitle = oPageTitle + oProperties["Name"];
						oPageH1.innerHTML = oPageTitle;
						document.title = oPageTitle;
					}
				}
			}
		}
	}

	this.postLayoutProcessing = function()
	{
		var oSections, oSection, oLinks, oLink, oSectionClass = "", oCurrentLink;
		if (oSections = findFirstElementOfClass(document, "ul", "l-sections")) {
			if (oSection = getSection(oSections)) {
				if (oLink = findFirstElementOfClass(oSection, "a", "*")) {
					var body = document.body;
					if (oLink.innerHTML.indexOf("Services") >= 0) {
						oSectionClass = "l-patients";
					} else if (oLink.innerHTML.indexOf("Training") >= 0) {
						oSectionClass = "l-education";
					} else if (oLink.innerHTML.indexOf("Research") >= 0) {
						oSectionClass = "l-research";		
					} else if (oLink.innerHTML.indexOf("About") >= 0) {
						oSectionClass = "l-about";
					}
					if (oSectionClass != "") {
						body.className = body.className + " " +oSectionClass;
					}
					if (oLink.parentNode.className && (oLink.parentNode.className.indexOf("crumb") >= 0)) {
						oCurrentLink = oLink;
					}
				}
			}
			if (oCurrentLink) {
				var oSitetools = document.getElementById("sitetools");
				if (oSitetools) {
					var arrToolLinks = oSitetools.getElementsByTagName("a");
					if (arrToolLinks.length > 0) {
						foreach(map(arrToolLinks), function(oLink) {
							if (oCurrentLink.href == oLink.href) {
								oLink.className = oLink.className ? oLink.className + " l-current" : "l-current";
							}
						});
					}
				}
			}
		}
		if (document.body.className && (document.body.className.indexOf("Home") >= 0)) {
			var oMiddleRow = findFirstElementOfClass(document, "tr", "l-middle");
			if (oMiddleRow) {
				var oNewRow = oMiddleRow.parentNode.insertBefore(document.createElement("tr"), oMiddleRow);
				oNewRow.className = "l-home-banner";
				oNewRow.appendChild(document.createElement("td"));
			}
		}
		
		/* remove directory entries and document collection documents from navigation */
		var nav = document.getElementById("navigation");
		if (nav)
		{
			if (document.getElementById("entry") ||
				document.getElementById("documentCollection") ||
				document.getElementById("documentInfo"))
			{
				var nodelist = findFirstElementOfClass(nav,"ul","Children");
				if (nodelist)
				{
					nodelist.parentNode.removeChild(nodelist);
				} else if (nodelist = findFirstElementOfClass(nav,"ul","Peers")) {
					nodelist.parentNode.removeChild(nodelist);
				}
			}	
		}
	
	}
	function getSection(oElm) {
		var oSection = findFirstElementOfClass(oElm, "li", "crumb");
		if (!oSection) {
			oSection = findFirstElementOfClass(oElm, "li", "current");
		}
		return oSection;
	}	
	function findFirstElementOfClass(start, element, classname)
	{
		var foundElement = null;
		foreach(start.getElementsByTagName(element), function(el) {
			if ((el.className.indexOf(classname) >= 0) || (classname == "*"))
			{
				foundElement = el;
				return true;
			}
		});
		return foundElement;
	}
	function wrapElementContents(element, wrapclass)
	{
		var savHTML = element.innerHTML;
		element.innerHTML = "<div class=\"" + wrapclass + "\">" + savHTML + "</div>";
	}
	function RTrim( value ) {
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
	}

}