/* SVN FILE: $Id$ */
/**
/**
 * 選択ボタンオブジェクト
 *
 * PHP versions 4 and 5
 *
 * Baser :  Basic Creating Support Project <http://baser.e-catchup.jp>
 * Copyright 2008, Catchup, Inc.
 *								9-5 nagao3-chome, fukuoka-shi 
 *								fukuoka, Japan 814-0123
 *
 * @filesource
 * @copyright		Copyright 2008, Catchup, Inc.
 * @link			http://baser.e-catchup.jp Baser Project
 * @package		cake
 * @subpackage		cake.app.controllers
 * @since			Baser v 0.1.0
 * @version		$Revision$
 * @modifiedby		$LastChangedBy$
 * @lastmodified	$Date$
 * @license		http://baser.e-catchup.jp/license/
 */
/**
 * イベント処理用の変数
 *
 * GurumedoSearchオブジェクトにより複数のSelectButtonが生成されるので、
 * Windowsオブジェクトよりたどるには、GurumedoSearchオブジェクトを通らなければならない。
 * このクラスだけでも動作するようにするには、グローバル変数を作るしかなかった。
 */
var selectbuttons = Array();
/**
 * 選択ボタンオブジェクト
 */
function SelectButton(id,buttonPrefix,checkboxPrefix) {this.loadButton(id,buttonPrefix,checkboxPrefix)}
SelectButton.prototype = {
	id:null,
	button:null,
	checkbox:null,
	buttonPrefix:null,
	checkboxPrefix:null,
	srcImgPath:null,
	overImgPath:null,
	onImgPath:null,
	selected:false,
/**
 * ボタンをロード
 */
	loadButton: function(id,buttonPrefix,checkboxPrefix){
		
		this.id = id;
		this.button = jQuery("#"+buttonPrefix+id);
		this.checkbox = jQuery("#"+checkboxPrefix+id);

		// ロールオーバー画像のパスを取得
		this.srcImgPath = this.button.attr("src");
		var imgType = this.srcImgPath.substring(this.srcImgPath.lastIndexOf('.'), this.srcImgPath.length);
		this.overImgPath = this.srcImgPath.replace(imgType, "_over" + imgType);
		this.onImgPath = this.srcImgPath.replace(imgType, "_on" + imgType);

		// ロールオーバー画像をプリロード
		new Image().src = this.overImgPath;
		new Image().src = this.onImgPath;

		// 画面をチェックボックスの状態に応じて更新
		this.loadView();
		
		// ホバーイベントを登録
		this.button.hover(this.overAction,this.outAction);
	
		// クリック時のイベントを登録
		this.button.click(this.clickAction);
		
		// イベント処理でこのクラスを参照できるようにグローバルな配列に代入
		// TODO 他に方法はないのか。。
		selectbuttons[buttonPrefix+id] = this;
		this.button.attr({selectbutton:buttonPrefix+id});

	},
/**
 * 画面をチェックボックスの状態に応じて更新
 */
	loadView:function(){

		if(this.checkbox.attr("checked")){
			this.selected = false;
			this.button.attr({src:this.onImgPath});
		}else{
			this.selected = true;
			this.button.attr({src:this.srcImgPath});
		}

	},
/**
 * マウスオーバー時のアクション
 * イベント用の関数（thisでこのクラスは参照できない）
 */
	overAction: function(){

		selectbutton = selectbuttons[jQuery(this).attr("selectbutton")];

		if(!selectbutton.checkbox.attr("checked")){
			selectbutton.selected = false;
			jQuery(this).attr({src:selectbutton.overImgPath});
		}else{
			selectbutton.selected = true;
		}
		
	},
/**
 * マウスが離れた時のアクション
 * イベント用の関数（thisでこのクラスは参照できない）
 */
	outAction: function(){
		
		selectbutton = selectbuttons[jQuery(this).attr("selectbutton")];
		
		if(!selectbutton.selected){
			jQuery(this).attr({src:selectbutton.srcImgPath});
		}
	},
/**
 * マウスクリック時のアクション
 * イベント用の関数（thisでこのクラスは参照できない）
 */
	clickAction: function(){
		
		selectbutton = selectbuttons[jQuery(this).attr("selectbutton")];
		
		if(selectbutton.selected){
			selectbutton.selected = false;
			selectbutton.checkbox.attr("checked",false);
			jQuery(this).attr({src:selectbutton.overImgPath});
		}else{
			selectbutton.selected = true;
			selectbutton.checkbox.attr("checked",true);
			jQuery(this).attr({src:selectbutton.onImgPath});
		}
		
	}
	
}
