function NavigationObject() {
	this.stay = {
		main : "",
		sub  : ""
	}
	this.config = {
		main : {
			normal : "main-navigation-item",
			stay   : "main-navigation-item-stay",
			self   : "sub-navigation-stay"
		},
		sub : {
			normal : "sub-navigation-item",
			stay   : "sub-navigation-item-stay",
			self   : "sub-navigation-stay"
		}
	}
	this.cssTitle = "css-sub-navigation-stay";
	this.indexFileNames = [
		"index.htm",
		"index.html",
		"index.shtm",
		"index.shtml",
		"index.cgi",
		"index.cfm",
		"index.php",
		"default.asp",
		"Default.asp"
	]
}
NavigationObject.prototype = {
	getSharedDir : function() {
		var s = document.getElementsByTagName("link");
		var ptn = /(.*\/?css\/).+$/;
		for(var i=0,l=s.length;i<l;i++){
			if(ptn.test(s[i].href)){
				return RegExp.$1;
			}
		}
	},
	init : function() {
		document.write('<link rel="stylesheet" type="text/css" href="' + this.getSharedDir() + 'sidenavi_closing.css" media="all" id="' + this.cssTitle + '">');
		var _this = this;
		if (typeof(window.addEventListener) == "function" || typeof(window.addEventListener) == "object") {
			window.addEventListener("load", function(){ _this.setStay(); }, false);
		} else if (typeof(window.attachEvent) == "function" || typeof(window.attachEvent) == "object") {
			window.attachEvent("onload", function(){ _this.setStay(); });
		} else {
			if ((typeof(window.onload) == "function" || typeof(window.onload) == "object") && window.onload) {
				var _func = window.onload;
				window.onload = function() {
					_func();
					_this.setStay();
				}
			} else {
				window.onload = function(){ _this.setStay(); };
			}
		}
	},
	setStay : function() {
		var subnavigation = this.getElementById("sub-navigation");
		if (!subnavigation || (" " + subnavigation.className + " ").indexOf("autoClosing") == -1) {
			var css = this.getElementById(this.cssTitle);
			if (css) css.disabled = true;
		}
		for (var i in this.stay) {
			var id = this.stay[i];
			if (!id) continue;

			var node = this.getElementById(id);
			if (!node) continue;

			var config = this.config[i];
			if (!config || !config.normal || !config.stay) continue;

			this.removeLink(node);

			var currentNode = node;
			var classNames = this.getClassNames(node);
			if (config.self) {
				classNames[classNames.length] = config.self;
			}
			this.setClassName(node, classNames.join(" "));

			for (var i = 0, n = node.childNodes.length; i < n; i++) {
				if (node.childNodes[i].tagName && node.childNodes[i].tagName.toLowerCase() == "ul") {
					node.childNodes[i].style.display = "block";
				}
			}
			var pN = node.parentNode;
			while (pN) {
				if (pN.tagName && pN.tagName.toLowerCase() == "ul") {
					pN.style.display = "block";
				} else if (pN.tagName && pN.tagName.toLowerCase() == "div") {
					break;
				}
				pN = pN.parentNode;
			}

			if (document.body.parentNode) {
				do {
					this.convertClassName(currentNode, config.normal, config.stay);
				} while ((currentNode = currentNode.parentNode));
			} else if (document.body.parentElement) {
				do {
					this.convertClassName(currentNode, config.normal, config.stay);
				} while ((currentNode = currentNode.parentElement));
			}
		}
		return true;
	},
	removeLink : function(node) {
		if (!node) return false;
		var link = null;
		if (typeof(node.getElementsByTagName) == "function" || typeof(node.getElementsByTagName) == "object") {
			link = node.getElementsByTagName("a")[0];
		}
		if (!link) return false;
		if (typeof(link.removeAttribute) == "function" || typeof(link.removeAttribute) == "object") {
			var _indexFileNames = this.indexFileNames;
			function chopIndexFileName(url) {
				for (var i = 0, n = _indexFileNames.length; i < n; i++) {
					var offset = url.indexOf("/" + _indexFileNames[i]);
					if (offset != -1 && offset == (url.length - _indexFileNames[i].length - 1)) {
						url = url.replace(_indexFileNames[i], "");
					}
				}
				return url;
			}
			var pageurl = chopIndexFileName(location.protocol + "//" + location.hostname + ((location.port) ? ":" + location.port : "") + location.pathname);
			var href    = chopIndexFileName(link.href);
			if (pageurl == href) {
				link.removeAttribute("href");
				return true;
			} else {
				return false;
			}
		}
		return false;
	},
	convertClassName : function(node, before, after) {
		if (!node || !before || !after) return false;
		var classNames = this.getClassNames(node);
		for (var i = 0, n = classNames.length; i < n; i++) {
			if (classNames[i] == before) {
				classNames[i] = after;
			}
		}
		this.setClassName(node, classNames.join(" "));
		return true;
	},
	getClassNames : function(node) {
		if (!node) return [];
		var classNames = [];
		if (typeof(node.getAttribute) == "function" || typeof(node.getAttribute) == "object") {
			if (document.all && !window.opera) {
				var className = node.getAttribute("className") || "";
				classNames = className.split(/ +/);
			} else {
				var className = node.getAttribute("class") || "";
				classNames = className.split(/ +/);
			}
		} else if (typeof(node.className) == "string") {
			classNames = node.className.split(/ +/);
		}
		return classNames;
	},
	setClassName : function(node, className) {
		if (!node) return false;
		if (typeof(node.setAttribute) == "function" || typeof(node.setAttribute) == "object") {
			if (document.all && !window.opera) {
				node.setAttribute("className", className);
			} else {
				node.setAttribute("class", className);
			}
		} else if (typeof(node.className) == "string") {
			node.className = className;
		}
		return true;
	},
	getElementById : function(id) {
		if (!id) return null;
		var node = null;
		if (typeof(document.getElementById) == "function" || typeof(document.getElementById) == "object") {
			node = document.getElementById(id);
		} else if (typeof(document.all) == "function" || typeof(document.all) == "object") {
			var temp = document.all(id);
			if (temp.length) {
				node = temp[0];
			} else {
				node = temp;
			}
		}
		return node;
	}
}

var Navigation = new NavigationObject;
Navigation.init();
