/* このコードはHTMLが構築された後自動的に実行されます */
$(document).ready(function(){
    /**
	 * スライドショーの動作設定
	 */
	var current            = 1;               //自動切り替えの有効無効(有効:1, 無効:-1)
	var slideTime          = 5;	              //自動的にスライドが切り替わる時間(秒)
	var slidesID           = 'slides';        //結合されたスライドのID名
	var slideClass         = 'slide';         //個々のスライド要素のClass名
    var slideNaviID        = 'slideNavi';     //スライドナビのメニューのID
	var slideNaviItemClass = 'slideNaviItem'; //スライドナビの要素のClass名

    /**
	 * すべての要素の個数と幅を取得し、結合されたスライドの幅を設定します
	 */
	var totWidth=0;
	var positions = new Array();
	$('#'+slidesID+' .'+slideClass).each(function(i){
		positions[i]= totWidth;
		totWidth += $(this).width();
		if(!$(this).width()){
			alert("すべての画像にwidthとheightを設定してください");
			return false;
		}
	});
	$('#'+slidesID).width(totWidth);


    /**
	 * ボタンアイコンにクリックイベントを割り当てる
	 */
	$('#'+slideNaviID+' .'+slideNaviItemClass).click(function(e,keepScroll){
		//リンクのデフォルトイベントを無効にする
		e.preventDefault();
		//スライドアニメーション開始
		var pos = $(this).prevAll('.'+slideNaviItemClass).length;
		$('#'+slidesID).stop().animate({marginLeft:-positions[pos]+'px'},450);
		//もしアイコンがクリックされたらオートスライド停止
		if(!keepScroll) clearInterval(itvl);
	});


    /**
	 * オートスライド
	 */
	function autoSlide(){
		if(current==-1) return false;
		$(' .'+slideNaviItemClass).eq(current%$(' .'+slideNaviItemClass).length).trigger('click',[true]);
		current++;
    }
	var itvl = setInterval(function(){autoSlide()},slideTime*1000);
});
