/*
 * Auto Visual Switch version 2 - autoVisualSwitch.ver02.js
 * 
 * The author is Manabu Kushimoto.
 * And URL is http://web-park.org.
 * 
 * jQuery version 1.6.1 later is required.
 * 
 * Update is 2011.07.04.
 * 
 * License is GPL(GNU General Public License).
 * Copy right © WEB-PARK.ORG All rights reserved.
 * 
 */
 
jQuery(function($){
	$.fn.autoVisualSwitchVer02 = function(options){
		var $t=$(this),op=$.extend($.fn.autoVisualSwitchVer02.defaults,options),
		childrenW=$("img",$t).width(),active="active",activeIndex=0,
		$children=$t.children(),childLength=$children.length;
		switch(op.effect){
			case "slide":
				$("<div />").attr("id",op.frameID).append("<div id='"+op.moveID+"' />").appendTo($t);
				$("#"+op.moveID).css({width:op.visualW*childLength}).append($children);
			break;
			default:
				$("<div />").attr("id",op.frameID).appendTo($t);
				$children.css({position:"absolute"}).not($children.eq(0)).hide();
				$("#"+op.frameID).append($children);
			break;
		}
		
		var $visualChild=$("#"+op.frameID).children();
		$visualChild.eq(0).show();

		$("<ul />").appendTo($t);
		for(var j=0;j<childLength;j++){
			$("<li />").append("&nbsp;").appendTo($("ul",$t));
		}

		// ナビゲーション設定
		$("li",$t).each(function(i){
			// ナビゲーションのインデックス番号を取得
			var thisIndex=$("li",$t).index(this);
			$("li",$t).eq(0).addClass(active);
			$(this).click(function(){
				clearInterval(setSwitch);
				$("li",$t).not(this).removeClass(active);
				$(this).addClass(active);
				switch(op.effect){
					case "slide":
						slideEffect(thisIndex)
					break;
					default:
						fadeEffect(thisIndex);
					break;
				}
				activeIndex=thisIndex-1;
				autoSwitch();
				return false;
			});
		});

		// スライドエフェクト関数
		function slideEffect(index){
			$visualChild.stop(true,false).animate({
				left:-op.visualW*index
			},op.effectSpeed,"easeOutCubic");
		}

		// フェードエフェクト関数
		function fadeEffect(index){
			$visualChild.not($visualChild.eq(index)).stop(true,true).fadeOut(op.effectSpeed);
			$visualChild.eq(index).stop(true,true).fadeIn(op.effectSpeed);
		}

		// 自動切替関数
		function autoSwitch(){
			setSwitch=setInterval(function(){
				// インクリメント演算子で1ループごとに1づつ数値を増やす
				var j=activeIndex++,nextElem=j+1,lastNum=($("li",$t).length)-1;
				// ナビゲーションの最後の要素に到達した場合activeIndexを-1にする
				// nextElemは-1+1=0になり、最初の要素から自動切替が開始される
				if(lastNum<=nextElem){ activeIndex=-1 }
				$("li",$t).not($("li",$t).eq(nextElem)).removeClass(active);
				$("li",$t).eq(nextElem).addClass(active);
				switch(op.effect){
					case "slide":
						slideEffect(nextElem)
					break;
					default:
						fadeEffect(nextElem);
					break;
				}
			},op.interval);
		};
		autoSwitch();
	};

	$.fn.autoVisualSwitchVer02.defaults={
		frameID:"visualFrame",
		moveID:"visualMove",
		effect:"",
		effectSpeed:"normal",
		interval:6000,
		visualW:830
	};

});

