// =============================================================================
// メニュー土台クラス
// =============================================================================
/*
   NavigationBase クラス

   最終更新日:2003年8月4日(月)
   -----------------------------------------------------------------------------

   依存
	sniffer.js
	broadcaster.js
	changeimage.js
	window_onload.js

   -----------------------------------------------------------------------------

   コンストラクタの引数
	無し

   -----------------------------------------------------------------------------

   メソッド
	- init()
		初期化

	- addChild( navigationObject )
		子供のnavigationObjectを追加登録する。

	- setEvent( navigationObject )
		子供たちに、rootAnchor(根っこのAタグ)と自分自身にイベントを付加するよう要請する。

	- hideAllChilren( focusNavigation )
		引数で与えられたnavigationObject以外の子供に、すべて消えるよう要請する。

	- blurAllChilren( focusNavigatio )
		引数で与えられたnavigationObject以外の子供に、blur処理(ロールアウト等)をするよう要請する。
*/
// =============================================================================
if(sniffer.DOMable()) {



// -----------------------------------------------------------------------------
/*
   オブジェクト準備
*/
function NavigationBase () {
	this.children = null;
	this.parent = null;

	this.init();
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   initメソッド
	Null NavigationBase.init()
*/
NavigationBase.prototype.init = function() {
	this.children = new Array();
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   addChildメソッド
	Null NavigationBase.addChild( navigationObject )

		Navigation navigationObject	-	対応するNavigationオブジェクト
*/
NavigationBase.prototype.addChild = function(navigationObject) {
	navigationObject.setParent(this);
	navigationObject.setIndex(this.children.length);
	this.children.push(navigationObject);
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   setEventメソッド
	Null NavigationBase.setEvent()
*/
NavigationBase.prototype.setEventToChildren = function() {
	for(var i=0; i<this.children.length; i++)
	{
		this.children[i].setEvent();
	}
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   hideAllChilrenメソッド
	Null NavigationBase.hideAllChildren( focusNavigation )
*/
NavigationBase.prototype.hideAllChildren = function( focusNavigation ) {
//	window.status = focusNavigation.index;

	for(var i=0; i<this.children.length; i++)
	{
		if(this.children[i] != focusNavigation)
		{
			window.clearTimeout(this.children[i].hideTimer);
			window.clearTimeout(this.children[i].showTimer);
			this.children[i].hideTimer = this.children[i].showTimer = null;
			this.children[i].actualHide();
		}
	}
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   blurAllChilrenメソッド
	Null NavigationBase.blurAllChildren( focusNavigation  )
*/
NavigationBase.prototype.blurAllChildren = function( focusNavigation ) {
	for(var i=0; i<this.children.length; i++)
	{
		if(this.children[i] != focusNavigation)
		{
			this.children[i].blur();
		}
	}
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   ルート分インスタンス化
*/
var rootnaviBase = new NavigationBase();
// -----------------------------------------------------------------------------


}
// =============================================================================
// =============================================================================
// =============================================================================










// =============================================================================
// ナビゲーションクラス
// =============================================================================
/*
   Navigation クラス

   最終更新日:2003年8月4日(月)
   -----------------------------------------------------------------------------

   依存
	sniffer.js
	broadcaster.js
	changeimage.js
	window_onload.js

   -----------------------------------------------------------------------------

   スーパークラス
	NavigationBase

   -----------------------------------------------------------------------------

   コンストラクタの引数
	HTMLUListElement menuObject	- 出したり消したりするメニューエレメント(UL)
	HTMLAnchorElement rootAnchor 	- 根っこになるAタグ
	HTMLImageElement rootImage	- 根っこになるAタグの中身が画像の場合のImageオブジェクト

   -----------------------------------------------------------------------------

   メソッド
	- init( menuObject, rootAnchor, rootImage )
		初期化

	- setParent( parent )
		引数で与えられたnavigationBaseオブジェクトを親として登録する。

	- setIndex( index )
		親のchildren配列上の自分の位置を登録する。

	- setEvent()
		rootAnchorとmenuObjectにイベントを付加する。

	- show()
		menuObjectを表示させる際の初期動作。actualShow()へのタイマーをセットする。

	- actualShow()
		タイマー後実際にmenuObjectを表示させる動作。

	- focus()
		rootAnchorに対するロールオーバーの動作。
		(menuObjectのshow中はマウスが離れていてもロールオーバー状態になっていないといけない)

	- hide()
		menuObjectを非表示にする際の初期動作。actualHide()へのタイマーをセットする。

	- actualHide()
		タイマー後実際にmenuObjectを非表示にする動作。

	- blur()
		rootAnchorに対するロールアウトの動作。

	- scoopChildren()
		自らのmenuObjectの下から子供となるULタグを探し、子供として登録する。
*/
// =============================================================================
if(sniffer.DOMable()) {



// -----------------------------------------------------------------------------
/*
   オブジェクト準備
*/
function Navigation(menuObject, rootAnchor, rootImage) {
	this.rootImage = null;
	this.rootAnchor = null;

	this.container = null;
	this.children = null;
	this.parent = null;

	this.closed = null;

	this.showTimer = null;
	this.hideTimer = null;

	this.index = null;

	this.init(menuObject, rootAnchor, rootImage);
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   NavigationBaseをスーパークラスに
*/
Navigation.prototype = new NavigationBase();
Navigation.prototype.constructor = Navigation;
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   共通プロパティ定義
*/
Navigation.prototype.showWait = 200;
Navigation.prototype.hideWait = 250;
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   initメソッド
	Null Navigation.init( menuObject, rootAnchor, rootImage )

		HTMLUListElement menuObject	- 出したり消したりするメニューエレメント(UL)
		HTMLAnchorElement rootAnchor 	- 根っこになるAタグ
		HTMLImageElement rootImage	- 根っこになるAタグの中身が画像の場合のImageオブジェクト
*/
Navigation.prototype.init = function(menuObject, rootAnchor, rootImage) {
	this.rootAnchor = rootAnchor;
	this.rootImage = (typeof rootImage != "undefined") ? rootImage : null;

	this.container = menuObject;
	this.hasContainer = (this.container != null) ? true : false;

	this.children = new Array();
	this.parent = null;

	if(this.showTimer != null){ window.clearTimeout(this.showTimer); this.showTimer = null;}
	if(this.hideTimer != null){ window.clearTimeout(this.hideTimer); this.hideTimer = null;}

	this.index = null;
	this.closed = true;

	this.scoopChildren();
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   setParentメソッド
	Null Navigation.setParent( parent )

		NavigationBase parent		-	親となるnavigationBaseオブジェクト
*/
Navigation.prototype.setParent = function( parent ) {
	this.parent = parent;
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   setIndexメソッド
	Null Navigation.setIndex( index )

		Number index		-	親のchilredn配列の何番目か
*/
Navigation.prototype.setIndex = function( index ) {
	this.index = index;
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   setEventメソッド
	Null Navigation.setEvent()
*/
Navigation.prototype.setEvent = function() {
	var self = this;

	if(typeof this.rootAnchor.addEventListener != "undefined")
	{
		this.rootAnchor.addEventListener("mouseover", function(evt){self.show(evt);}, false);
		this.rootAnchor.addEventListener("click", function(evt){self.show(evt);}, false);
		this.rootAnchor.addEventListener("mouseout", function(evt){self.hide(evt);}, false);

		if(this.hasContainer)
		{
			this.container.addEventListener("mouseover", function(evt){self.show(evt);}, false);
			this.container.addEventListener("mouseout", function(evt){self.hide(evt);}, false);
		}
	}
	else if(typeof this.rootAnchor.attachEvent != "undefined")
	{
		this.rootAnchor.attachEvent("onmouseover", function(){self.show();});
		this.rootAnchor.attachEvent("onclick", function(){self.show();});
		this.rootAnchor.attachEvent("onmouseout", function(){self.hide();});

		if(this.hasContainer)
		{
			this.container.attachEvent("onmouseover", function(){self.show();});
			this.container.attachEvent("onmouseout", function(){self.hide();});
		}
	}
	else
	{
		var pastmv = this.rootAnchor["onmouseover"];
		this.rootAnchor["onmouseover"] = function() {if(pastmv != null) pastmv();self.show();};

		var pastcl = this.rootAnchor["onclick"];
		this.rootAnchor["onclick"] = function() {if(pastcl != null) pastcl();self.show();};

		var pastmu = this.rootAnchor["onmouseout"];
		this.rootAnchor["onmouseout"] = function() {if(pastmu != null) pastmu();self.hide();};

		if(this.hasContainer)
		{
			this.container.onmouseover = function(){self.show();};
			this.container.onmouseout = function(){self.hide();};
		}
	}
	this.setEventToChildren();
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   showメソッド
	Null Navigation.show()
*/
Navigation.prototype.show = function(evt) {
	var evt = (typeof window.event != "undefined") ? window.event : evt;
	var isClick = ( evt.type == "click" ) ? true : false;

	window.clearTimeout(this.hideTimer);
	this.hideTimer = null;

	this.parent.blurAllChildren(this);

	this.focus();

	var funcString = "children["+this.index+"].actualShow()";
	var parent = this.parent;

	while(parent.parent != null) {
		funcString = "children["+parent.index+"]." + funcString;
		parent = parent.parent;
	}
	funcString = "rootnaviBase." + funcString;

	var wait = ( isClick ) ? 0 : this.showWait;
	this.showTimer = window.setTimeout(funcString, wait);

	if( isClick && this.hasContainer && this.parent.parent == null )
	{
		if(typeof evt.preventDefault != "undefined")
		{
			evt.preventDefault();
		}
		else
		{
			evt.returnValue = false;
		}
	}
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   actualShowメソッド
	Null Navigation.actualShow()
*/
Navigation.prototype.actualShow = function() {
	window.clearTimeout(this.showTimer);
	this.showTimer = null;

	this.parent.hideAllChildren(this);

	if(this.hasContainer)
	{
		if(document.getElementById("flash") != null) {
			document.getElementById("flash").style.display = "none";
			document.getElementById("flashalternate").style.display = "block";
		}
		this.container.style.visibility = "visible";

		if(this.parent.parent != null)
		{
			//[padding:5] hard coded
			this.container.style.top = this.rootAnchor.offsetTop - 5 + "px";
		}
	}
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   focusメソッド
	Null Navigation.focus()
*/
Navigation.prototype.focus = function() {
	if(this.rootImage != null) this.rootImage.changeTo("on");
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   hideメソッド
	Null Navigation.hide()
*/
Navigation.prototype.hide = function(evt) {
	window.clearTimeout(this.showTimer);
	this.showTimer = null;

	var evt = (typeof window.event != "undefined") ? window.event : evt;

	var funcString = "children["+this.index+"].actualHide()";
	var parent = this.parent;
	while(parent.parent != null) {
		funcString = "children["+parent.index+"]." + funcString;
		parent = parent.parent;
	}
	funcString = "rootnaviBase." + funcString;

	this.hideTimer = window.setTimeout(funcString, this.hideWait);
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   actualHideメソッド
	Null Navigation.actualHide()
*/
Navigation.prototype.actualHide = function() {
	window.clearTimeout(this.hideTimer);
	this.hideTimer = null;

	this.blur();

	if(this.hasContainer)
	{
		if(document.getElementById("flash") != null) {
			document.getElementById("flash").style.display = "block";
			document.getElementById("flashalternate").style.display = "none";
		}
		this.container.style.visibility = "hidden";
	}
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   blurメソッド
	Null Navigation.blur()
*/
Navigation.prototype.blur = function() {
	if(this.rootImage != null) this.rootImage.changeTo("off");
}
// -----------------------------------------------------------------------------



// -----------------------------------------------------------------------------
/*
   scoopChildrenメソッド
	Null Navigation.scoopChildren()
*/
Navigation.prototype.scoopChildren = function() {
	if(this.hasContainer)
	{
		for(var i=0; i<this.container.childNodes.length; i++)
		{
			var child = this.container.childNodes[i];
			var tagname = (typeof child.tagName != "undefined") ? child.tagName : null;
			if(tagname != null && tagname.toLowerCase() == "li")
			{
				var ul_array = child.getElementsByTagName("UL");
				if(ul_array.length > 0)
				{
					this.addChild(new Navigation(ul_array[0], child.firstChild))
				}
				else
				{
					this.addChild(new Navigation(null, child.firstChild));
				}
			}
		}
	}
}
// -----------------------------------------------------------------------------

}
