///////////////////////////////////////////////////////////////////////////////
//	Button.class.js
//		イメージボタン処理クラス
//			Copyright (c) 2007 mochiZ.
//
//	author: mochiZ.
//	create: 2007/03/08
//	update: 2007/05/24 IDチェックダイアログを無効とする
//	update: 2008/12/26 コンストラクタに引数optionsを追加、img_hiは廃止(options内に取り込み)
///////////////////////////////////////////////////////////////////////////////

//*************************
// オブジェクト取得関数
//*************************
function getObj(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;
}

//=============================================================================
// class Button
//=============================================================================

//*************************
// コンストラクタ
//		id: タグのID名
//		options:
//			{
//				img_hi    : ハイライトイメージ名指定
//				onMousOver: MouseOver時コールバック関数
//				onMousOut : MouseOut時コールバック関数
//			}
//*************************
function Button(id, options)
{
	var SUFFIX = "_H";		// ハイライトファイル名サフィックス
	// メンバ
	this.imgOrg = new Image();	// オリジナル画像
	this.imgHi  = new Image();	// ハイライト画像
	this.obj    = getObj(id);	// エレメントのオブジェクト
	
	
	if(!this.obj){
		//alert("Not found : " + id);
		return;
	}
	
	this.imgOrg.src = this.obj.src;
	
	var img_hi = !options ? null : options["img_hi"];
	// ハイライトイメージのロード
	if(!img_hi){
		// オリジナルファイル名にサフィックスを付加
		var pos = this.imgOrg.src.lastIndexOf(".");
		var ext = this.imgOrg.src.substr(pos);
		var img = this.imgOrg.src.substr(0,pos);
		img_hi = img + SUFFIX + ext;
	}
	this.imgHi.src = img_hi;
	
	// マウスイベント登録
	Button.eventHandler(this);
	
	
	// イベント登録
	this.funcMOver = !options ? null : options["onMouseOver"];
	this.funcMOut = !options ? null : options["onMouseOut"];
}


//*********************************************************
// イベントハンドラ(クラスメソッド)
//*********************************************************
Button.eventHandler = function(w)
{
	obj = w.getElement();
	obj.onmouseover = function(){
		w.MouseOver();
	}
	obj.onmouseout = function(){
		w.MouseOut();
	}
}
	
//*************************
// エレメント取得
//*************************
Button.prototype.getElement = function()
{
	return this.obj;
}

//*************************
//	MouseOver
//*************************
Button.prototype.MouseOver = function()
{
	this.obj.src = this.imgHi.src;
	if(this.funcMOver) this.funcMOver();
}

//*************************
//	MouseOut
//*************************
Button.prototype.MouseOut = function()
{
	this.obj.src = this.imgOrg.src;
	if(this.funcMOut) this.funcMOut();
}
