/**
 * Sub Content Top Slide-show Controller
 * Date: 03/19/2010
 * @author xe-que design works
 * @version 1.0
 *
 */

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

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

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

// 画像枚数情報
var _arrayType = {"organic":6, "tailormade":6};

// オーガニックトップとテイラーメイドトップの画像をスライド表示
$(window).load(function(){

	// 画像表示用 img タグを生成して、すでに存在している img の後ろに追加（１番目の画像は、テンプレート側にハードコーディング）
	$.each(_arrayType, function(pKey){

		// 当初 1 枚目の画像はハードコーディングされた img を使用していたが、
		// safari のみ最初の FadeIn で画像のちらつきが起きるので、１枚目の画像も一旦消した上で、１枚目の画像は親 div の背景として初期表示にあてた
		temp_bg = "url(" + $("#container .content #choice-" + pKey + " .house-image img:eq(0)").attr('src') + ") no-repeat scroll 0 0 transparent";
		$("#container .content #choice-" + pKey + " .house-image").html('').css('background',temp_bg);

		for (var i=1 ; i<=_arrayType[pKey] ; i++) {
			temp_id = 'catch-top-slide-' + pKey + "-" + i;
			var temp = new Image();
			$(temp).attr('id',temp_id);
			$("#container .content #choice-" + pKey + " .house-image").append(temp);

			temp_img = $("#container .content #choice-" + pKey + " .house-image #" + temp_id);
			temp_img.fadeIn().css('display','none').load(function(){
				$(this).data('loaded','true');
			}).attr('src','./wp-content/themes/alphahome/images/img-catch-top-' + pKey + '-' + i + '.jpg');

		}
	});

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

});


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


	// 画像読込み済みかをチェック
	var checkloaded = true;
	$("#container .content .house-image 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);

	// 表示対象画像番号セット
	temp_target = $("#container").get(0);
	$.each(_arrayType, function(pKey){
		$.data(temp_target,'current_index_' + pKey, 1);
	});
	$.data(temp_target,'current_type' ,'organic');

	// ループ開始
	catch_timerEvent_tmp = setTimeout("changeCatchSlide()", CATCH_NEXT_INTERVAL);

}

// 画像変更
function changeCatchSlide() {

	temp_target = $("#container").get(0);
	current_type = $.data(temp_target,'current_type');
	itemcount    = _arrayType[current_type];
	current_idx  = next_index = $.data(temp_target,'current_index_' + current_type);

	next_index++;
	if (next_index > itemcount) next_index = 1;

	current_target = "#container .content #choice-" + current_type + " .house-image img#catch-top-slide-" + current_type + "-" + current_idx;
	next_target    = "#container .content #choice-" + current_type + " .house-image img#catch-top-slide-" + current_type + "-" + 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_' + current_type, next_index);
		if (current_type == 'organic') current_type = 'tailormade'; else current_type = 'organic';
		$.data(temp_target,'current_type', current_type);

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

	});

}




