///////////////////////////////////////////////////////////////////////////////
//	DropDownText.class.js
//		ドロップダウンメニュー処理クラス
//			Copyright (c) 2007 mochiZ.
//
//	author: mochiZ.
//	create: 2007/07/03
//	update: 2007/07/25	クラス名を指定できるように追加修正
//	update: 2007/12/28	DropDownのテキスト版として修正
//	update: 2008/01/08	ドキュメントルートを指定できるように修正
//	update: 2008/01/08	Buttonオブジェクトの状態をキープできるように修正
//	update: 2008/01/17	navi配列の空要素を許可するように修正
//	update: 2008/01/17	メニューの非表示処理スタイルを変更(display→visibility)
//	update: 2008/06/24	外部リンク用に"http://"のURLはclass='extarnal'を追加するように修正
//						(外部リンク切替は common.js内スクリプトで処理)
///////////////////////////////////////////////////////////////////////////////

//=============================================================================
// class DropDownText
//=============================================================================

/*

	[使用例]
	
	var navi = new Array();
	navi[0] = new Array("ナビ0", "url1.php");
	navi[1] = new Array("ナビ1", "url2.php");
	navi[2] = new Array("ナビ2", "url3.php");
	new DropDown('ownerID' ,navi ,'menu');
	
	それぞれのnavi[x]には id="{ownerID}{menu}x" が設定される
*/


//*************************
//	クラス変数
//*************************
DropDownText.prototype.timerID = null;
DropDownText.prototype.menuObj = null;
DropDownText.prototype.func = null;

//*************************
//	コンストラクタ
//		ownerID		: オーナーとなるエレメントID
//		navi		: ナビゲーション配列(代替文字列, イメージパス, リンクURL)
//		documentRoot: 絶対パス指定のためのドキュメントルート
//		className	: 生成されるナビにつけるclass名(IDのプレフィックスも兼ねる)
//*************************
function DropDownText(ownerID, navi, documentRoot, className)
{
	// デフォルト
	if(!className) className = "item";
	if(!documentRoot) documentRoot = "";

	// メンバ
	this.owner = this.getObj(ownerID);		// オーナとなるエレメントオブジェクト
	this.menu;								// メニューのエレメントオブジェクト
	this.tag = "";
	
	if(!this.owner){
		alert("Not found Owner ID : " + ownerID);
		return;
	}
	
//alert(this.owner.id);

	menuID = ownerID + className;
	html = "\n<div>" + this.owner.innerHTML + "</div>\n";
	this.tag = html + "\n<ul class='" + className + "' id='" + menuID + "'>\n";
	for(i=0; i<navi.length; ++i){
		n = navi[i];
		if(!n) continue;
		txt = n[0];
		url = n[1];
		imgID = menuID + i;
		
		if(url.search("http://")){
			url = documentRoot + url;
			cls = "";
		}else{
			cls = " class='external'";
		}
		
		this.tag += "<li><a " + cls + "id='" + imgID + "' href='" + url + "'>" + txt + "</a></li>\n";
					
	}
	this.tag += "</ul>";
	
//alert(this.tag);
	
	// ページに書き込む
	this.owner.innerHTML = this.tag;
	
	// スタイル設定
//	this.owner.style.position = "relative";
	this.owner.style.zIndex = 10;
	
	this.menu = this.getObj(menuID);
	this.menu.style.position = "absolute";
//	this.menu.style.display = "none";
	this.menu.style.visibility = "hidden";
	

//alert(this.owner.innerHTML);

	// マウスイベント登録
	this.eventHandler(this, this.owner);
	this.eventHandler(this, this.menu);
}

//*********************************************************
// イベントハンドラ(クラスメソッド)
//*********************************************************
DropDownText.prototype.eventHandler = function(w, obj)
{
	obj.onmouseover = function(){
		w.MouseOver();
	}
	obj.onmouseout = function(){
		w.MouseOut();
	}
	obj.onmousedown = function(){
		w.MouseOut();
	}
}

//*************************
// オブジェクト取得
//*************************
DropDownText.prototype.getObj = function(name)
{
	if(document.getElementById){
		return document.getElementById(name);
	}else if(document.all){
		return document.all(name);
	}else if(document.layers){
		return document.layers[layName];
	}
	return null;
}

//*************************
//	MouseOver
//*************************
DropDownText.prototype.MouseOver = function()
{
//	this.menu.style.display = 'block';

	this.DelayDisplay(this.menu, true, 1);
	Button.prototype.set();
}

//*************************
//	MouseOut
//*************************
DropDownText.prototype.MouseOut = function()
{
//	this.menu.style.display = 'none';
	
	this.DelayDisplay(this.menu, false, 300);
}

//*************************
//	表示遅延処理
//************************
DropDownText.prototype.DelayDisplay = function(obj, visible, msec)
{
	// 待機中の処理を実行
	clearTimeout(DropDownText.prototype.timerID);
	if(visible && DropDownText.prototype.timerID){
		eval(DropDownText.prototype.func);
	}
	
	// 実行関数の登録
//	display = visible ? 'block' : 'none';
	display = visible ? 'visible' : 'hidden';
	DropDownText.prototype.menuObj = obj;
	DropDownText.prototype.func = "DropDownText.prototype.display('" + display + "')";
	DropDownText.prototype.timerID = setTimeout(DropDownText.prototype.func, msec);
	
//window.status += DropDownText.prototype.func;
}

DropDownText.prototype.display = function(display)
{
//	if(display == 'none') Button.prototype.reset();
//	DropDownText.prototype.menuObj.style.display = display;
	if(display == 'hidden') Button.prototype.reset();
	DropDownText.prototype.menuObj.style.visibility = display;
}

