$(document).ready(function()
{
	// Photo gallery
	var max = $(".photos .photo").length-1;
	var current = 0;
	var noList = $(".photoNavi li[class*='no']");
	
	$(".photos .photo").css("display","none");
	$(".photos .photo:eq(0)").css("display","block");
	$(".photoNavi li:eq(1)").addClass("current");

	$(".photoNavi li.prev").click(function(){
		var hide = $(".photos .photo").eq(current);
		var show = (current==0) ? $(".photos .photo").eq(max) : $(".photos .photo").eq(current-1);
		hide.fadeOut("fast");
		show.fadeIn("fast");
		current = (current==0) ? max : current-1;
		noList.removeClass("current");
		noList.eq(current).addClass("current");
	});
	$(".photoNavi li.next").click(function(){
		var hide = $(".photos .photo").eq(current);
		var show = (current==max) ? $(".photos .photo").eq(0) : $(".photos .photo").eq(current+1);
		hide.fadeOut("fast");
		show.fadeIn("fast");
		current = (current==max) ? 0 : current+1;
		noList.removeClass("current");
		noList.eq(current).addClass("current");
	});

	noList.click(function(){
		var clickNo;
		for( var i = 1; i <= max+1; i++) {
			if($(this).hasClass("no"+i)) {
				clickNo = i-1;
				break;
			}
		}
		if(!$(this).hasClass("current")) {
			var hide = $(".photos .photo").eq(current);
			var show = $(".photos .photo").eq(clickNo);
			hide.fadeOut("fast");
			show.fadeIn("fast");
			noList.removeClass("current");
			$(this).addClass("current");
			current = clickNo;
		}
	});

	// Thumbnail
	var thumbSize = 105;
	var thumbPerPage = 8;
	var pageMax = Math.ceil($(".thumbContainer li").length / 8);
	var currentPage = 1;
	
	// パラメーターの取得
	var ret = getParameter(location.href);
	var currentPage = 1;
	if (ret.tp) {
		currentPage = ret.tp;
	}
	
	$(".thumbContainer .thumb").hide();
	$(".thumbContainer .thumb").show();
	
	// サイズの取得とセット
	$(".thumbContainer ul").css("width", pageMax * (thumbSize*thumbPerPage) );
	
	// prevボタンの制御
	if (currentPage > 1) {
		$(".thumbContainer a.prev").removeClass("disable");
	} else {
		$(".thumbContainer a.prev").addClass("disable");
	}
	// nextボタンの制御
	if (pageMax == 1 || pageMax == currentPage) {
		$(".thumbContainer a.next").addClass("disable");
	} else {
		$(".thumbContainer a.next").removeClass("disable");
	}
	
	// 表示エリア（スクロール）のセット
	$(".thumbContainer .thumb").scrollLeft((currentPage-1)*thumbSize*thumbPerPage);
	
	$(".thumbContainer a.next").click(function(){
		if ( !$(this).hasClass("disable") ) {
			currentPage++;
			$(".thumbContainer .thumb").animate({scrollLeft: (currentPage-1)*thumbSize*thumbPerPage}, "normal", "swing", function(){$(".thumbContainer a.next").blur();});
			if(currentPage == pageMax) $(this).addClass("disable");
			if($(".thumbContainer a.prev").hasClass("disable")) $(".thumbContainer a.prev").removeClass("disable");
		} /*else {
			if(pageMax > 1){
				currentPage = 1;
				if(pageMax == 2) $(".thumbContainer .thumb").animate({scrollLeft: (currentPage-1)*thumbSize*thumbPerPage}, "normal", "swing", function(){$(".thumbContainer a.next").blur();});
				else $(".thumbContainer .thumb").animate({scrollLeft: (currentPage-1)*thumbSize*thumbPerPage}, 1000, "swing", function(){$(".thumbContainer a.next").blur();});
				$(this).removeClass("disable");
				$(".thumbContainer a.prev").addClass("disable");
			}
		}*/
	});
	$(".thumbContainer a.prev").click(function(){
		if ( !$(this).hasClass("disable") ) {
			currentPage--;
			$(".thumbContainer .thumb").animate({scrollLeft: (currentPage-1)*thumbSize*thumbPerPage}, "normal", "swing", function(){$(".thumbContainer a.prev").blur();});
			if(currentPage == 1) $(this).addClass("disable");
			if($(".thumbContainer a.next").hasClass("disable")) $(".thumbContainer a.next").removeClass("disable");
		}
	});
});

/*
 *  クエリストリングからパラメーターの取得
 */
function getParameter(str)
{
	var dec = decodeURIComponent;
	var par = new Array, itm;
	if(typeof(str) == 'undefined') return par;
	if(str.indexOf('?', 0) > -1) str = str.split('?')[1];
	str = str.split('&');
	for(var i = 0; str.length > i; i++){
		itm = str[i].split("=");
		if(itm[0] != ''){
			par[itm[0]] = typeof(itm[1]) == 'undefined' ? true : dec(itm[1]);
		}
	}
	return par;
}

