window.CustomLayoutProcessing = new function() {
	var navigation;
	
	this.preLayoutProcessing = function()
	{
		var forms = document.getElementsByTagName("form");
		var form = forms[0];
		if (form) {
			var titling = document.getElementById("titling");
			if (titling) {
				var classbag = new ElementClassBag(titling, ["Header1", "Flash", "Logo"]);
				titling.appendChild(classbag.Elements["Header1"]);
				/*
				var flash = classbag.Elements["Flash"];
				if (flash.getAttribute("src").indexOf(".swf.") < 0) {
					var div = document.createElement("div");
					div.className = "Flash";
					flash.className = "";
					div.appendChild(flash);
					titling.appendChild(div);
				}
				else {
					titling.appendChild(flash);
				}
				*/
				titling.appendChild(classbag.Elements["Logo"]);		
			}
			navigation = document.getElementById("navigation");
			if (navigation) {
				/*
				* Clone navigation as sitetools
				*/
				var el = navigation;
				var idx = 0;
				while (el && (idx < 2)) {
					el = getNextChild(el, ["ul", "li"]);
					if (el.tagName.toLowerCase() == "ul") {
						++idx;
					}
				}
				if (idx == 2) {
					var sitetools = document.createElement("div");
					sitetools.id = "sitetools";
					sitetools.appendChild(el.cloneNode(true));
					var extra = findFirstElementOfClass(sitetools, "a", "extra");
					if (extra) {
						extra.parentNode.className += " extra";
					}
					form.appendChild(sitetools);
				}
			}
		}
	}

	this.postLayoutProcessing = function()
	{
		
		var el;
		if (navigation) {
			/*
			* Move quicklinks to within the div.l-navigation
			*/
			var parent = navigation.parentNode;
			var lcdiv = document.createElement("div");
			lcdiv.className = "lc-navigation";
			lcdiv.appendChild(navigation);
			el = parent.nextSibling;
			if (el) {
				if (el.className.indexOf("quicklinks") >= 0) {
					lcdiv.appendChild(el);
				}
			}
			parent.appendChild(lcdiv);
			
			
			/*
			* Set class = "Home" for li that contains home link
			*/
			el = navigation;
			while (el && (el.tagName.toLowerCase() != "li")) {
				el = getNextChild(el);
			}
			if (el) {
				el.className += " Home";
				
			}
		}
		var oFlash = findFirstElementOfClass(document, "div", "Flash");
		if (oFlash) {
			var oLogo = findFirstElementOfClass(document, "a", "Logo");
			if (oLogo) {
				oLogo.parentNode.insertBefore(oFlash,oLogo);
			}
		}
		var flashdiv = findFirstElementOfClass(document, "div", "Flash");
		if (flashdiv) {
			var flashobj = getNextChild(flashdiv, ["embed", "object"]);
			if (flashobj) {
				switch (flashobj.tagName.toLowerCase()) {
					case "embed":
						flashobj.setAttribute("wmode", "opaque");
						break;
					case "object":
						flashdiv.innerHTML = flashdiv.innerHTML.replace("<PARAM NAME=\"WMode\" VALUE=\"Window\">", "<PARAM NAME=\"WMode\" VALUE=\"Opaque\">");
						break
				}
			}
		}
	}
	
	function getNextChild(node, validTypes) {
		var el = node.firstChild;
		while (el) {
			if ((el.nodeType == 1) && isValidType(el, validTypes)) {
				return(el);
			} 
			else {
				el = el.nextSibling;
			}
		}
		return(null);
	}
	
	function isValidType(el, validTypes) {
		if (!validTypes || validTypes.length == 0) {
			return true;
		}
		var tag = el.tagName.toLowerCase();
		for (var i = 0; i < validTypes.length; i++) {
			if (tag == validTypes[i]) {
				return true;
			}
		}
		return false;
	}
	
	function findFirstElementOfClass(start, element, classname)
	{
		var foundElement = null;
		foreach(start.getElementsByTagName(element), function(el) {
			if (el.className.indexOf(classname) >= 0)
			{
				foundElement = el;
				return true;
			}
		});
		return foundElement;
	}
	
	function ElementClassBag(node, classes)
	{
		var Classes = classes;
		this.Elements = [];
		
		this.contains = function()
		{
			for(var i = 0; i < Classes.length; i++)
			{
				if (!this.Elements[Classes[i]])
				{
					return false;
				}
			}
			return true;
		}
		
		for(var i = 0; i < Classes.length; i++)
		{
			var className = Classes[i];
			var element = traverse(node, className, 0);
			if (element)
			{
				this.Elements[className] = element;
			}
		}
		
		function traverse(node, className, level) {
			var el = null;
			++level;
			foreach(node, function(child) {
				//document.write(level + ":" + child + ", " + child.className + "<br>");
				if (hasClass(child, className)) {
					el = child;
					return true;
				}
				else {
					el = traverse(child, className, level);
					if (el) return true;
				}
			})
			return el;
		}
	}
	
	function hasClass(node, className)
	{
		var rv = false;
		var classNames = node.className.split(" ");
		if (classNames && classNames.length > 0)
		{
			for (var y=0; (y < classNames.length) && (rv == false); ++y)
			{
				if (classNames[y] == className)
				{
					rv = true;
				}
			}
		}
		return rv;	
	}
}

