/**
 * DDNavi02.js Ver 1.0
 * Powered by hisato http://chibinowa.net/
 */

function DDNavi02(targetID)
{
	// マウスオーバーからアクションが発生するまでの時間1000→1秒
	this.open_sleep = 100;

	// マウスアウトからアクションが発生するまでの時間1000→1秒
	this.close_sleep = 300;

	// メインメニューとサブメニュー展開の同期
	// メニュー展開と同時メインメニューのクラスを変える場合は1
	// 展開を待たずにクラスを変える場合は0
	// (背景画像がメインとサブで繋がっている場合なんかは1に)
	this.menu_onmenu = 1;


	this.timer = null;

	this.active_oMainAnc = null;

	this.active_oSubUl = null;

	this.defClassName = null;

	this.movingLabel = null;

	if (document.getElementById(targetID)) {
		this.init(targetID);
	}
}

DDNavi02.prototype.init = function(targetID)
{
	var rn = document.getElementById(targetID);
	for (i=0; i < rn.childNodes.length; i++) {//>
		var oMain = rn.childNodes[i];
		if (oMain.nodeName == "LI") {
			for (r=0; r < oMain.childNodes.length; r++) {//>
				var oSub = oMain.childNodes[r];

				if (oSub.className == "onmenu")
					oMainAnc = oSub;

				if (oSub.nodeName == "UL")
					oSubUl = oSub;
			}
			EventListener.add(oMain, "mouseover", function(self, oSubUl, oMainAnc){ return function(){self.open(oSubUl, oMainAnc)} }(this, oSubUl, oMainAnc), false);
			EventListener.add(oMain, "mouseout", function(self, oSubUl, oMainAnc){ return function(){self.close(oSubUl, oMainAnc)} }(this, oSubUl, oMainAnc), false);
		}
	}
	EventListener.add(document.documentElement, "click", function(self){ return function(){self._close()} }(this), false);
}

DDNavi02.prototype.open = function(oSubUl, oMainAnc)
{
	// タイマーをリセット
	if (this.timer) {
		this.resetTimer();
	}

	// 既に開いてるものを終了or同じIDなら継続
	if (this.active_oSubUl) {
		if (this.active_oSubUl != oSubUl)
			this._close();
		else if (this.movingLabel == 'open')
			return;
	}

	this.active_oSubUl = oSubUl;
	this.active_oMainAnc = oMainAnc;
	if (!this.movingLabel) {
		this.defClassName = this.active_oMainAnc.className;
	}
	this.movingLabel = 'open';

	if (!this.menu_onmenu) {
		this.active_oMainAnc.className += ' hover';
	}

	// オープン用のタイマーをセット
	this.timer = setTimeout(function(self){ return function(){self._open()} }(this), this.open_sleep);
}

DDNavi02.prototype.close = function(oSubUl, oMainAnc)
{
	// タイマーをリセット
	if (this.timer) {
		this.resetTimer();
	}
	this.movingLabel = 'close';
	// クローズ用のタイマーをセット
	this.timer = setTimeout(function(self){ return function(){self._close()} }(this), this.close_sleep);
}

DDNavi02.prototype._open = function()
{
	this.active_oSubUl.style.visibility = "visible";

	if (this.menu_onmenu) {
		this.active_oMainAnc.className += ' hover';
	}
}

DDNavi02.prototype._close = function()
{
	if (this.active_oSubUl || this.active_oMainAnc) {
		this.active_oSubUl.style.visibility = "hidden";
		this.active_oMainAnc.className = this.defClassName;
		this.active_oSubUl = null;
		this.active_oMainAnc = null;
		this.movingLabel = null;
		this.resetTimer();
	}
}

DDNavi02.prototype.resetTimer = function()
{
	clearTimeout(this.timer);
	delete this.timer;
}


// プルダウン化したいリストIDを登録する

// sample1のオブジェクト 同期を非同期に設定
EventListener.add(window, "load", function(){ var obj = new DDNavi02('NaviMenu'); obj.menu_onmenu = 0; }, false);

// sample2のオブジェクト
EventListener.add(window, "load", function(){ new DDNavi02('NaviMenu2') }, false);

/**
 * 同じページ上にメニューを増やしたい場合は、以下のコードを実行する。
 * EventListener.add(window, "load", function(){ new DDNavi02('NaviMenu2') }, false);
 */
