// ukFunc.menuBar ver 0.11

var ukFunc;

ukFunc = {
};

// コンストラクタ
ukFunc.menuBar = function(element, category) {
	var ua = navigator.userAgent.toString();
	var msie = /MSIE.([0-9\.]*)/i;
	var r = false;
	if ((r = ua.match(msie))) {
		this.ie = true;
		this.ver = parseFloat(r[1]);
	}
	if(typeof(document.getElementById) == "undefined" || (navigator.vendor == "Apple Computer, Inc." && typeof(window.XMLHttpRequest) == "undefined") || (this.ie && typeof(document.uniqueID) == "undefined")) {
		return;
	}
	if (this.ie && this.ver < 7) {
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {
		}
	}
	this.openDelay = 200;
	this.openDelayS = 0;
	this.hideDelay = 600;
	this.mHoverClass = "c-ma-h";
	openTimer = hideTimer = null;
	this.currentM = null;
	this.currentSM = null;
	this.currentBC = null;
	this.nowLiHover = null;
	this.topNode = this.getElement(element);
	var divNodes = this.topNode.getElementsByTagName("div");
	if (category) {
		var rootClass = "c-bgimg-" + category;
		this.addClass(this.topNode, rootClass);
		var initClass = "c-mb-" + category + "d";
		var aNode = divNodes[category - 1].getElementsByTagName("a")[0];
		this.addClass(aNode, initClass);
	}
	var liNodes = new Array();
	for (var i = 0; i < divNodes.length; i++) {
		this.initDiv(divNodes[i], i + 1);
		if (divNodes[i].getElementsByTagName("li")) {
			liNodes[i] = divNodes[i].getElementsByTagName("li");
		}
	}
	for (var j = 0; j < divNodes.length; j++) {
		for (var m = 0; m < liNodes[j].length; m++) {
			this.initLi(liNodes[j][m]);
		}
	}
};

// divにイベントリスナーを追加
ukFunc.menuBar.prototype.initDiv = function(divNode, num) {
	var cn;
	cn = "c-mb-" + num + "h";
	var self = this;
	this.addEvent(divNode, "mouseover", function(e){self.divOver(divNode, cn, e);}, false);
	this.addEvent(divNode, "mouseout", function(e){self.divOut(divNode, cn, e);}, false);
};

// liにイベントリスナーを追加
ukFunc.menuBar.prototype.initLi = function(liNode) {
	var self = this;
	this.addEvent(liNode, "mouseover", function(e){self.liOver(liNode, e);}, false);
	this.addEvent(liNode, "mouseout", function(e){self.liOut(liNode, e);}, false);
};

// divマウスオーバー時の関数
ukFunc.menuBar.prototype.divOver = function(divNode, className) {
	if (hideTimer) {
		clearTimeout(hideTimer);
	}
	var aNode = divNode.getElementsByTagName("a")[0];
	this.addClass(aNode, className);
	var ulNode;
	if (divNode.getElementsByTagName("ul")[0]) {
		ulNode = divNode.getElementsByTagName("ul")[0];
		if (this.currentM == ulNode) {
			if (!this.nowLiHover && this.currentSM) {
				var smParentLi = this.currentSM.parentNode;
				var nowHoverASM = smParentLi.getElementsByTagName("a")[0];
				this.removeClass(nowHoverASM, this.mHoverClass);
				this.currentSM.style.visibility = "hidden";
				this.currentSM = null;
			}
			return;
		}
	}
	if (this.currentM) {
		if (this.currentSM) {
			var smParentLi = this.currentSM.parentNode;
			var nowHoverASM = smParentLi.getElementsByTagName("a")[0];
			this.removeClass(nowHoverASM, this.mHoverClass);
			this.currentSM.style.visibility = "hidden";
			this.currentSM = null;
		}
		var mParentDiv = this.currentM.parentNode;
		var nowHoverAM = mParentDiv.getElementsByTagName("a")[0];
		this.removeClass(nowHoverAM, this.currentBC);
		this.currentBC = null;
		this.currentM.style.visibility = "hidden";
		this.currentM = null;
	}
	var self = this;
	if (ulNode) {
		if (openTimer) {
			clearTimeout(openTimer);
		}
		openTimer = setTimeout(function(){self.showMenu(ulNode, className);}, this.openDelay);
	}
};

// divマウスアウト時の関数
ukFunc.menuBar.prototype.divOut = function(divNode, className) {
	if (openTimer) {
		clearTimeout(openTimer);
	}
	var aNode = divNode.getElementsByTagName("a")[0];
	if (divNode.getElementsByTagName("ul")[0]) {
		if (this.currentM != divNode.getElementsByTagName("ul")[0]) {
			this.removeClass(aNode, className);
		}
	} else {
		this.removeClass(aNode, className);
	}
	var self = this;
	if (hideTimer) {
		clearTimeout(hideTimer);
	}
	hideTimer = setTimeout(function(){self.hideMenu(divNode, className);}, this.hideDelay);
};

// liマウスオーバー時の関数
ukFunc.menuBar.prototype.liOver = function(liNode) {
	if (hideTimer) {
		clearTimeout(hideTimer);
	}
	this.nowLiHover = true;
	var aNode = liNode.getElementsByTagName("a")[0];
	this.addClass(aNode, this.mHoverClass);
	var ulNode;
	if (liNode.getElementsByTagName("ul")[0]) {
		ulNode = liNode.getElementsByTagName("ul")[0];
		if (this.currentSM == ulNode) {
			return;
		}
		if (this.currentSM) {
			var smParentLi = this.currentSM.parentNode;
			var nowHoverASM = smParentLi.getElementsByTagName("a")[0];
			this.removeClass(nowHoverASM, this.mHoverClass);
			this.currentSM.style.visibility = "hidden";
			this.currentSM = null;
		}
		var self = this;
		if (openTimer) {
			clearTimeout(openTimer);
		}
		openTimer = setTimeout(function(){self.showSubMenu(ulNode);}, this.openDelayS);
	}
};

// liマウスアウト時の関数
ukFunc.menuBar.prototype.liOut = function(liNode) {
	if (openTimer) {
		clearTimeout(openTimer);
	}
	this.nowLiHover = false;
	var aNode = liNode.getElementsByTagName("a")[0];
	if (liNode.getElementsByTagName("ul")[0]) {
		if (this.currentSM != liNode.getElementsByTagName("ul")[0]) {
			this.removeClass(aNode, this.mHoverClass);
		}
	} else {
		this.removeClass(aNode, this.mHoverClass);
	}
};

// 一階層目のメニューを表示させる関数
ukFunc.menuBar.prototype.showMenu = function(ulNode, className) {
	ulNode.style.visibility = "inherit";
	this.currentM = ulNode;
	this.currentBC = className;
};

// 一階層目のメニューを非表示にする関数
ukFunc.menuBar.prototype.hideMenu = function(divNode, className) {
	if (this.currentSM) {
		var smParentLi = this.currentSM.parentNode;
		var nowHoverASM = smParentLi.getElementsByTagName("a")[0];
		this.removeClass(nowHoverASM, this.mHoverClass);
		this.currentSM.style.visibility = "hidden";
		this.currentSM = null;
	}
	if (this.currentM) {
		var mParentDiv = this.currentM.parentNode;
		var nowHoverAM = mParentDiv.getElementsByTagName("a")[0];
		this.removeClass(nowHoverAM, this.currentBC);
		this.currentBC = null;
		this.currentM.style.visibility = "hidden";
		this.currentM = null;
	}
	var aNode;
	aNode = divNode.getElementsByTagName("a")[0];
	this.removeClass(aNode, className);
};

// 二階層目のメニューを表示させる関数
ukFunc.menuBar.prototype.showSubMenu = function(ulNode) {
	ulNode.style.visibility = "inherit";
	this.currentSM = ulNode;
};

// 渡された引数を持つIDのノードを戻す関数
ukFunc.menuBar.prototype.getElement = function(elem) {
	if (elem && (typeof(elem) == "string")) {
		return document.getElementById(elem);
	}
	return elem;
};

// 引数1のノードが、引数2のクラスを持っているかを返す関数
ukFunc.menuBar.prototype.hasClass = function(elem, className) {
	if (!elem || !className || !elem.className || elem.className.search(new RegExp("\\b" + className + "\\b")) == -1) {
		return false;
	}
	return true;
};

// 引数1のノードに引数2のクラス名を追加する関数
ukFunc.menuBar.prototype.addClass = function(elem, className) {
	if (!elem || !className || this.hasClass(elem, className)) {
		return;
	}
	elem.className += (elem.className ? " " : "") + className;
};

// 引数1のノードから引数2のクラス名を消す関数
ukFunc.menuBar.prototype.removeClass = function(elem, className) {
	if (!elem || !className || !this.hasClass(elem, className)) {
		return;
	}
	elem.className = elem.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
};

// 引数として渡されたノードにイベントリスナーを追加する関数
ukFunc.menuBar.prototype.addEvent = function(elem, evt, func, cap) {
	if (elem.addEventListener) {
		elem.addEventListener(evt, func, cap);
	} else if (elem.attachEvent) {
		elem.attachEvent("on" + evt, func);
	}
};

// 引数として渡されたノードのイベントリスナーを削除する関数
ukFunc.menuBar.prototype.removeEvent = function(elem, evt, func, cap) {
	if (elem.removeEventListener) {
		elem.removeEventListener(evt, func, cap);
	} else if (elem.detachEvent) {
		elem.detachEvent("on" + evt, func);
	}
};

// イベントが起こった要素を得る関数
ukFunc.menuBar.prototype.getTarget = function(e) {
	if (e.target) {
		return e.target;
	} else if (e.srcElement) {
		return e.srcElement;
	}
};

// バブリングを止める関数
ukFunc.menuBar.prototype.stopBubble = function(e) {
	if (e.stopPropagation) {
		e.stopPropagation();
	} else if (window.event) {
		window.event.cancelBubble = true;
	}
};

// 要素のデフォルト処理を止める関数
ukFunc.menuBar.prototype.stopDefault = function(e) {
	if (e.preventDefault) {
		e.preventDefault();
	} else if (window.event) {
		window.event.returnValue = false;
	}
};

