/* Copyright (C) 2007 Valerio Proietti ===========================================*
 *	
 *	スクロール関数
 *	
 *	Create Date		: 2008/4/17
 *	Creator			: luistar15, <leo020588 [at] gmail.com>
 *	
 *	Translate Date 	: 2008/5/20
 *	Translator		: K.Murakami
 *	Note			:
 *	
 *	[コンストラクタ引数]
 *		box				->	Javascript変数
 *							表示用ボックス(必須)
 *		items			->	アイテム数(必須、[]内にカンマで区切った数だけスライドする)
 *							例：[1,2,3,4]
 *		size			->	数値(スライド幅(px指定)、デフォルト値240)
 *		mode			->	スライド方向
 *			+ 'horizontal'	->	水平方向(デフォルト値)
 *			+ 'vertical'	->	垂直方向
 *		interval		->	数値
 *							待機時間(1000で1秒、デフォルト値5000)
 *		buttons			->	配列型。動作ボタンID指定(Javascript配列として扱うため、下記値を任意で{}内で指定する必要がある)
 *							記述例：buttons{previous: $('pre'),next; $('next')}
 *			+ previous		->	逆方向スライドボタン
 *			+ next			->	正方向スライドボタン
 *			+ play			->	自動再生ボタン
 *			+ playback		->	自動逆再生ボタン
 *			+ stop			->	停止ボタン
 *		button_event	->	文字列型			//要調査
 *			+ 'click'		->	デフォルト値
 *		handles			->	配列				//要調査
 *		handle_event	->	文字列型			//要調査
 *			+ 'click'		->	デフォルト値
 *		fxOptions		->	mootoolsにて提供されているモジュール
 *			+ duration		->	デフォルト値：500
 *			+ wait			->	デフォルト値：false
 *		autoPlay		->	自動再生
 *			+ true			->	自動再生して開始
 *			+ false			->	自動再生しないで開始(デフォルト)
 *		onWalk			->	スライド時に動作させたい関数(引数に下記値を両方指定することも可)
 *			+ currentItem	->					//要調査
 *			+ currentHandle	->					//要調査
 *		startItem		->	開始位置をitemsの数値範囲内で指定
 *		
 * =============================================================================*/

var noobSlide = new Class({

	//イニシャライザ
	initialize: function(params){
		this.items = params.items;
		this.mode = params.mode || 'horizontal';
		this.modes = {horizontal:['left','width'], vertical:['top','height']};
		this.size = params.size || 240;
		this.box = params.box.setStyle(this.modes[this.mode][1],(this.size*this.items.length)+'px');
		this.button_event = params.button_event || 'click';
		this.handle_event = params.handle_event || 'click';
		this.interval = params.interval || 5000;
		this.buttons = {previous: [], next: [], play: [], playback: [], stop: []};
		if(params.buttons){
			for(var action in params.buttons){
				this.addActionButtons(action, $type(params.buttons[action])=='array' ? params.buttons[action] : [params.buttons[action]]);
			}
		}
		this.handles = params.handles || null;
		if(this.handles){
			this.addHandleButtons(this.handles);
		}
		this.fx = new Fx.Style(this.box,this.modes[this.mode][0],params.fxOptions||{duration:500,wait:false});
		this.onWalk = params.onWalk || null;
		this.currentIndex = params.startItem || 0;
		this.previousIndex = null;
		this.nextIndex = null;
		this.autoPlay = params.autoPlay || false;
		this._auto = null;
		this.box.setStyle(this.modes[this.mode][0],(-this.currentIndex*this.size)+'px');
		if(params.autoPlay) this.play(this.interval,'next',true);
	},
	
	/* 逆方向にスライド -------------------------------------------------------*
	 *	[引数]
	 *		manual	->	boolean
	 *			+ true	->	スライドして一旦停止する
	 *			+ false	->	スライドして一旦停止しない(デフォルト)
	 *-------------------------------------------------------------------------*/
	previous: function(manual){
		this.currentIndex += this.currentIndex>0 ? -1 : this.items.length-1;
		this.walk(null,manual);
	},
	
	/* 正方向に1回スライド -------------------------------------------------------*
	 *	[引数]
	 *		manual	->	boolean
	 *			+ true	->	スライドして一旦停止する
	 *			+ false	->	スライドして一旦停止しない(デフォルト)
	 *-------------------------------------------------------------------------*/
	next: function(manual){
		this.currentIndex += this.currentIndex<this.items.length-1 ? 1 : 1-this.items.length;
		this.walk(null,manual);
	},
	
	/* 自動再生 ---------------------------------------------------------------*
	 *	[引数]
	 *		delay		->	待機時間
	 *		direction	->	進行方向
	 *			+ 'next'		->	正方向
	 *			+ 'previous'	->	逆方向
	 *		wait		->	開始時の待機時間
	 *			+ true			->	有効
	 *			+ false			->	無効
	 *-------------------------------------------------------------------------*/
	play: function(delay,direction,wait){
		this.stop();
		if(!wait){
			this[direction](false);
		}
		this._auto = this[direction].periodical(delay,this,false);
	},
	
	/* スライド停止 -----------------------------------------------------------*
	 *	[引数]
	 *		なし
	 *-------------------------------------------------------------------------*/
	stop: function(){
		$clear(this._auto);
	},
	
	/* 指定位置までスライド ---------------------------------------------------------------*
	 *	[引数]
	 *		item	->	数値
	 *		manual	->	boolean
	 *			+ true	->	スライド後停止する
	 *			+ false	->	スライド後停止しない(デフォルト)
	 *-------------------------------------------------------------------------*/
	walk: function(item,manual){
		if($defined(item)){
			if(item==this.currentIndex) return;
			this.currentIndex=item;
		}
		this.previousIndex = this.currentIndex + (this.currentIndex>0 ? -1 : this.items.length-1);
		this.nextIndex = this.currentIndex + (this.currentIndex<this.items.length-1 ? 1 : 1-this.items.length);
		if(manual){ this.stop(); }
		this.fx.start(-this.currentIndex*this.size);
		if(this.onWalk){ this.onWalk(this.items[this.currentIndex],(this.handles?this.handles[this.currentIndex]:null)); }
		if(manual && this.autoPlay){ this.play(this.interval,'next',true); }
	},
	
	/* ハンドルボタンの追加 -------------------------------------------------------*
	 *	[引数]
	 *		handles	->	配列
	 *-------------------------------------------------------------------------*/
	addHandleButtons: function(handles){
		for(var i=0;i<handles.length;i++){
			handles[i].addEvent(this.handle_event,this.walk.bind(this,[i,true]));
		}
	},
	/* アクションボタンの追加 -------------------------------------------------------*
	 *	[引数]
	 *		action	->	スクロールの動き
				+ 'previous'	->逆方向にスライド
				+ 'next'		->正方向にスライド
				+ 'play'		->正方向に自動再生
				+ 'playback'	->逆方向に自動再生
				+ 'stop'		->停止
	 *		buttons	->	配列
	 *-------------------------------------------------------------------------*/
	addActionButtons: function(action,buttons){
		for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': buttons[i].addEvent(this.button_event,this.previous.bind(this,true)); break;
				case 'next': buttons[i].addEvent(this.button_event,this.next.bind(this,true)); break;
				case 'play': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'next',false])); break;
				case 'playback': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'previous',false])); break;
				case 'stop': buttons[i].addEvent(this.button_event,this.stop.bind(this)); break;
			}
			this.buttons[action].push(buttons[i]);
		}
	}
});
