/**
 * Web Site Interface Controller for Top Page
 * Date: 05/15/2010
 * @author xe-que design works
 * @version 1.0
 *
 */
 
// 画像ロールオーバー等のエフェクト時間(ms)
var ROLLOVER_EFFECT_TIME = 300;

// キャッチ画像インターバル時間(ms)
var CATCH_NEXT_INTERVAL  = 3000;

// キャッチ画像トランジション時間(ms)
var CATCH_EFFECT_TIME    = 3000;

// インターバルタイマー
var _loadchecktimer = 0;

var _itemcount = 5; // 画像枚数

// 要素情報
var SLIDE_IMAGE_PARENT_ID = "#eyecatch #catch-slide";   // スライド img を格納している div の ID
var SLIDE_IMAGE_CURRENT_ID  = "#content";				// 表示中スライドのインデックスを保持する div の ID

jQuery(function( $ ){

	// リンク内画像ロールオーバーハンドラ
	$('#eyecatch #catch-menu img').hover(
		function(){
			$(this).fadeTo(ROLLOVER_EFFECT_TIME, 0.1);
		},	
		function(){
			$(this).fadeTo(ROLLOVER_EFFECT_TIME, 1);
		}
	);

	// 枠の背景に１枚目の画像を配置
	temp_bg = "url(" + $(SLIDE_IMAGE_PARENT_ID + " img:eq(0)").attr('src') + ") no-repeat scroll 0 0 transparent";
	$("#catch-slide").html('').css('background',temp_bg);
			
	// 画像表示用 img タグを生成して、すでに存在している img の後ろに追加（１番目の画像は、テンプレート側にハードコーディング）
	for (i=1 ; i<=_itemcount ; i++) {
		temp_id = 'catch-slide-' + i;
		temp = new Image();
		$(temp).attr('id',temp_id);
//		$(SLIDE_IMAGE_PARENT_ID + " img:last").after(temp);
		$(SLIDE_IMAGE_PARENT_ID).append(temp);

		temp_img = $(SLIDE_IMAGE_PARENT_ID + " #" + temp_id);	
		temp_img.fadeIn().css('display','none').css('z-index','0').load(function(){ // fadeIn は safari の初回ちらつき対策s
			$(this).data('loaded','true');
		}).attr('src','./wp-content/themes/joetsu_hp/images/img-catch-slide-' + i + '.jpg');
	}

	// スライド開始処理をインターバルタイマー設定
	_loadchecktimer = setInterval('startCatchSlide()',200);
});


// スライドスタート
function startCatchSlide() {

	// 画像読込み済みかをチェック
	var checkloaded = true;
	$("#eyectach #catch-slide img").each(function(pIndex){
		temp = $(this);
		temp = temp.attr('id').split('-');
		temp = temp[temp.length-1];
		if (!$(this).data('loaded')) {
			checkloaded = false;
		}
	});

	// まだ読込まれてない項目があれば終了
	if (!checkloaded) {
		return ;
	}

	// インターバルタイマー解除
	clearInterval(_loadchecktimer);

	// 表示対象画像番号セット
	$.data($(SLIDE_IMAGE_CURRENT_ID).get(0),'current_index',1);

//$('#catch-slide').fadeIn();
	
	// ループ開始
	catch_timerEvent_tmp = setTimeout("changeCatchSlide()", CATCH_NEXT_INTERVAL);

}


// 画像変更
function changeCatchSlide() {

	temp_target = $(SLIDE_IMAGE_CURRENT_ID).get(0);
	current_idx = next_index = $.data(temp_target,'current_index');
	next_index++;
	if (next_index > _itemcount) next_index = 1;

	current_target = SLIDE_IMAGE_PARENT_ID + " img#catch-slide-" + current_idx;
	next_target    = SLIDE_IMAGE_PARENT_ID + " img#catch-slide-" + next_index;

	$(next_target).css('z-index',50).stop().fadeIn(CATCH_EFFECT_TIME, function(){

		$(current_target).css('display','none');
		$(next_target).css('z-index', 1);
		$.data(temp_target,'current_index', next_index);

		catch_timerEvent_tmp = setTimeout("changeCatchSlide()", CATCH_NEXT_INTERVAL);
	});

/*
	$(next_target).css('z-index',50).stop().animate(
    {opacity: "toggle"},
    {duration: CATCH_EFFECT_TIME,
     complete: function(){
     	$(current_target).css('display','none');
		$(next_target).css('z-index', 1);
		$.data(temp_target,'current_index', next_index);
		catch_timerEvent_tmp = setTimeout("changeCatchSlide()", CATCH_NEXT_INTERVAL);
		}
    }
  );
*/


}


