/////////////////////////////////////////////////////////////////////////

// オートプレイのある切り替えスクリプト「ナーラーjs（仮）」
// jqueryが必要。jqueryのバージョンは1.4.2しか試してないが1.2以降ならたぶん動く
// 対象ブラウザ IE6+, Firefox2+, Safari3+, Chorome, Opera9.5+
//
// 【作成日】2010.04.14
// 【更新日】2010.05.12
// 【バージョン】1.1C
//  通常切り替えボタンはクリックだが、mouseoverに変更

// 【こんな感じで引数を設定して、オンロードで実行】
// nr.run({
// 	contents:"#naaraaDisplay > a.naaraaContent",（切り替えるコンテンツをCSSセレクタで指定。必須）
// 	buttons:"#naaraaMenu > a",（メニューボタンをCSSセレクタで指定）
// 	activeClass:"active",（メニューボタンがアクティヴな時のクラス名）
//	playStop:"#naaraaMenu a[href='#playStop']",（再生停止ボタンをCSSセレクタで指定）
// 	fadeSpeed:4000,（切り替わる時のフェードのスピードをミリ秒で。デフォルトは500.）
// 	autoplay:true,（自動再生するかいなか true か false ）
// 	interval:2000,（再生時の切り替わる時間をミリ秒で。デフォルトでは4000。）
// 	playFadeSpeed:（自動再生のフェードのスピードをミリ秒で。デフォルトは1500.）
// });

/////////////////////////////////////////////////////////////////////////


// 名前はnr
nr = {};

// 発動する関数（オンロードで実行してね）
nr.run = function(option){

	// contentsが空ならbreak
	if(!option.contents){return false;}

	// オプションの設定（ここも微妙。オプションを適切に入れれば問題ないが。。。）
	nr.contents      = $(option.contents);
	nr.buttons       = (option.buttons)? $(option.buttons) : false;
	nr.activeClass   = option.activeClass  || "active";
	nr.playStop      = (option.playStop)? $(option.playStop) : false;
	nr.playClass     = option.playClass || "playing";
	nr.stopClass     = option.stopClass || "stopping";
	nr.fadeSpeed     = option.fadeSpeed || 500;
	nr.autoplay      = option.autoplay || false;
	nr.interval      = parseInt(option.interval) || 4000;
	nr.playFadeSpeed = option.playFade || 1500;

	// 再生用のタイマーIDを定義
	nr.timer = false;

	// 一発目を表示
	nr.contents.eq(0).show();
	nr.buttons.filter("[href='#" + nr.contents.eq(0).attr("id") + "']").addClass(nr.activeClass);

	// 切り替えボタンはマウスオーバーで
	if(nr.buttons){
		nr.buttons
			.mouseover(function(){
				// ボタンがアクティヴのときは実行しない
				if($(this).attr("class").indexOf(nr.activeClass)===-1){
					nr.show($($(this).attr("href")));
				}
				return false;
			})
		.end();
	}

	// 再生停止ボタンにクリックイベント
	if(nr.playStop){
		nr.playStop
			.click(function(){
				// ボタンがアクティヴのときは実行しない
				if(nr.timer){
					nr.stop();
				}else{
					nr.play();
				}
				return false;
			})
		.end();
	}

	// オートプレイtrueなら再生
	if(nr.autoplay){
		nr.play();
	}
}

// 表示されてる要素を非表示、指定した要素を表示する関数。引数はjqueryオプジェクトで
nr.show = function(jObj,isAutoplay){
	// 再生中ならタイマーを解除
	if(nr.timer){
		clearTimeout(nr.timer);
	}

	// 表示されてる要素を非表示
	nr.contents
		.filter(":visible")
			.fadeOut((isAutoplay)? nr.playFadeSpeed : nr.fadeSpeed)
		.end()
	.end();

	// 表示されてる要素を表示
	jObj
		.fadeIn((isAutoplay)? nr.playFadeSpeed : nr.fadeSpeed)
	.end();

	// activeが付いてるボタンからactiveをとって、表示している要素のボタンにactiveを付ける
	if(nr.buttons!==""){
		nr.buttons
			.filter("." + nr.activeClass)
				.removeClass(nr.activeClass)
			.end()
			.filter("[href='#" + jObj.attr("id") + "']").addClass(nr.activeClass)
				.addClass(nr.activeClass)
			.end()
		.end();
	}

	// 再生中ならタイマーを復帰
	if(nr.timer){
		nr.timer = setTimeout("nr.next(true)", nr.interval + nr.fadeSpeed);
	}
}

// 次のコンテンツへ（次がなければ最初へ）
nr.next = function(isAutoplay){
	var currentIndex = nr.contents.index(nr.contents.filter(":visible").eq(0));
	var nextIndex    = (currentIndex === nr.contents.length-1)? 0 : currentIndex + 1;
	nr.show(nr.contents.eq(nextIndex),isAutoplay);
}

// 前のコンテンツへ（前がなければ最後へ）
nr.prev = function(){
	var currentIndex = nr.contents.index(nr.contents.filter(":visible").eq(0));
	var prevIndex    = (currentIndex === 0)? nr.contents.length-1 : currentIndex - 1;
	nr.show(nr.contents.eq(prevIndex));
}

// 再生
nr.play = function(){
	if(!nr.timer){
		nr.timer = setTimeout("nr.next(true)", nr.interval);
	}
}

// 停止
nr.stop = function(){
	if(nr.timer){
		clearTimeout(nr.timer);
		nr.timer = false;
	}
}
