(function() {
	var $slidePhoto		= "";
	var $sec_s			= "";
	var $				= jQuery;
	var index			= 0;
	var max				= 0;
	var timer			= 0;
	var delay			= 7000;
	var pagerHtml		= "";
	var $pager			= "";
	var isOriginalPager = true;
	var isLoop			= true;	// trueの場合、最後の要素の次は最初に戻る
	var isAuto			= true;	// trueの場合、自動的（数秒毎）に写真が切り替わる
	var effect			= "rotate";	// 写真切り替えエフェクト
	
	$(function() {
		$slidePhoto = $("#slidePhoto");
		if(effect == "rotate") {
			$slidePhoto.addClass("rotate");
			$(".group", $slidePhoto).width(
				$slidePhoto.width() * $(".section", $slidePhoto).size()
			);
			
			$sec_s = $(".section", $slidePhoto);
			$sec_s.bind("mouseover", function() {
				$(".text", this).addClass("over");
			});
			$sec_s.bind("mouseout", function() {
				$(".text", this).removeClass("over");
			});
			max = $sec_s.length;
			
			$pager = $("#slidePhoto .pager");
			$(".prev a",$pager).bind("click", function() {
				showPhoto(index - 1);
			});
			$(".next a",$pager).bind("click", function() {
				showPhoto(index + 1);
			});
			
			$pagerLink = $("#slidePhoto .pager .link a");
		}
	});

	$(window).load(function() {
		// 初期画像表示
		showPhoto(index);
	});

	// 写真を表示する
	function showPhoto(indexValue) {
		index = Number(indexValue);
		
		// ループする場合
		if(isLoop) {
			if(index >= max) {
				index = 0;
			} else if(index < 0) {
				index = max - 1;
			}
		} else {
			if(index >= max) {
				index = max -1;
			} else if(index < 0) {
				index = 0;
			}
		}
		
		if(effect == "normal") {
			// 全部非表示
			$sec_s.hide();
			$("a", $pager).removeClass("selected");

			// 対象のみ表示
			$($sec_s[index]).show();
			$("a", $pager).each(function() {
				if($(this).attr("page") == index) {
					$(this).addClass("selected");
					return false;
				}
			});
		} else if(effect == "rotate") {
			var movePosition = $("#slidePhoto").width() * index;
			$("#slidePhoto .group")
			.animate({
				"left"	: - movePosition
			},{"duration":500});
			var href = $("a", $sec_s[index]).attr("href");
			$pagerLink.attr("href", href);
		}
		
		
		// 自動タイマーオン
		if(isAuto) {
			startTimerShowPhoto();
		}
	}

	// タイマー開始
	function startTimerShowPhoto() {
		clearInterval(timer);
		timer = setInterval(function() {
			clearInterval(timer);
			showPhoto(Number(index) + 1);
		}, delay);
	}
	
	
	// pager作成
	function getPagerHtml() {
		pagerHtml += '<ul class="pager">';
		pagerHtml += 	'<li class="prev"><a href="javascript:void(0);">前</a></li>';
		for(var i = 0; i < max; i++) {
			pagerHtml += '<li><a href="javascript:void(0);" page="'+ i +'">'+ (i+1) +'</a></li>';
		}
		pagerHtml += 	'<li class="next"><a href="javascript:void(0);">次</a></li>';
		pagerHtml += '</ul>';
		$pager = $(pagerHtml);
		
		$(":not(.prev , .next) a", $pager).bind("click", function() {
			showPhoto($(this).attr("page"));
		});
		$(".prev a",$pager).bind("click", function() {
			showPhoto(index - 1);
		});
		$(".next a",$pager).bind("click", function() {
			showPhoto(index + 1);
		});
	}

})();
