$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	var totWidth=0;
	var positions = new Array();
	// スライドの幅の合計をtotWidthに設定(画像にwidth指定要)
	$('#slides .slide').each(function(i){
		positions[i]= totWidth;
		totWidth += $(this).width();
		if(!$(this).width()){
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});
	// 幅合計を指定
	$('#slides').width(totWidth);

	// ページの読み込みで、最初のサムネイルにアクティブを、兄弟にインアクティブをマーク
	$('#mItems ul li:first').removeClass('inact').addClass('act');
	$('#mItems ul li a').prepend('<img src="img/slide-menu-bg_active.png" class="pngfix point" />');
	//$('#mItems ul li a').prepend('<img src="img/slide-menu-bg_hover.png" class="pngfix hover" />');
	$('li img.point').hide();
	//$('li img.hover').hide();
	$('li.act img.point').show();


	//クリック時
	$('#mItems ul li a').click(function(e,keepScroll){
		// クラス名の付替
		$('li.act').removeClass('act').addClass('inact');
		$('.point').hide();
		$(this).parent().removeClass('inact').addClass('act');
		$(this).children('img.point').show();
		// 画像サイズ分をアニメーション
		var pos = $(this).parent().prevAll('#mItems ul li').length;
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
		// ブラウザが最初から持っているアクションを中断させる場合に用います。
		e.preventDefault();
		// リンクの初期動作防止?
		// アイコンをクリックされている場合、自動前進を停止
		if(!keepScroll) clearInterval(itvl);
	});
	//ホバー時
	/*$('li.inact a').hover(function(){
		$(this).children('img.hover').show();
		return false;
	},
	function(){
		$(this).children('img.hover').hide();
		return false;
	});*/


	/*****
	 * 自動前進を有効に
	 ****/
	var current=1;
	function autoAdvance(){
		if(current==-1) return false;
		$('#slideMenu ul li a').eq(current % $('#slideMenu ul li a').length).trigger('click',[true]);
		current++;
	}

	// 自動前進の秒数
	var changeEvery = 5;
	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);



	/* ←←←←←
	　 矢印の挙動
	 　→→→→→ */
	$("#slidePrev").hide();
	$("#slideNext").hide();
	$('#slideMenu').hover(function(){
		$('#slidePrev').show();
		$('#slideNext').show();
		return false;
	},
	function(){
		$('#slidePrev').hide();
		$('#slideNext').hide();
		return false;
	});

	$('#slidePrev').click(function(e,keepScroll){
		var $active = $('li.act');
		var lindex = $('li.act').index();
//		console.log(listNo);
//		console.log(lindex);
		if(lindex != 0){
			//リストの先頭でないときは前の要素をアクティブに
			$active.prev().removeClass('inact').addClass('act');
		}else{
			//リストの先頭のときはリストの最後をアクティブに
			$('#mItems ul li:last').removeClass('inact').addClass('act');
		}
		$active.removeClass('act').addClass('inact');
		$('li img.point').hide();
		$('li.act img.point').show();
		// 画像サイズ分をアニメーション
		var pos = $('li.act').prevAll('#mItems ul li').length;
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
		e.preventDefault();
		if(!keepScroll) clearInterval(itvl);
		return false;
	});

	$('#slideNext').click(function(e,keepScroll){
		var $active = $('li.act');
		var listNo = $('#mItems ul li').length-1;
		var lindex = $('li.act').index();
//		console.log(listNo);
//		console.log(lindex);
		if(lindex != listNo){
			//リストの最後でないときは次の要素をアクティブに
			$active.next().removeClass('inact').addClass('act');
		}else{
			//リストの最後のときはリストの先頭をアクティブに
			$('#mItems ul li:first').removeClass('inact').addClass('act');
		}
		$active.removeClass('act').addClass('inact');
		$('li img.point').hide();
		$('li.act img.point').show();
		// 画像サイズ分をアニメーション
		var pos = $('li.act').prevAll('#mItems ul li').length;
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
		e.preventDefault();
		if(!keepScroll) clearInterval(itvl);
		return false;
	});

});


//IE6背景画像ちらつき対応
try { 
    document.execCommand('BackgroundImageCache', false, true); 
} catch(e) {} 
