var photo_cnt;
var photo_index;
var ua;

$(function(){

/*初期設定
******************************************************************************/

/*写真とサムネールの数を取得
************************************************/
photo_cnt = $(".photo").size();
var photos_cnt = $(".photos").size();

/*belt（th_belt）の横幅を設定
************************************************/
var photo_margin = 20;
var belt_width = (765 + photo_margin)  * photo_cnt;
$("#belt").css("width",belt_width);

var photos_margin = 6;
var th_belt_width = (64 + photos_margin)  * photos_cnt;
$("#th_belt").css("width",th_belt_width);

/*初期写真IDを指定
************************************************/
photo_index = 1;
displayNavigateButton(photo_index);

/*IE用初期設定
************************************************/
ua = $.browser;
if(ua.msie){
	$("#th_belt").css("width","765px");
	$(".photo_caption").hide();
	$("#main1").show();
	$(".caption").css("top","0px");
	$(".mouse_target").css("top","-384px");
	$("#thumbnail_list, #ss_all").css("height","auto");
	$(".th_photos").css("margin","0px 4px 4px 0px");
}else{
	$("#belt").css("position","relative");
	$("#th_belt").css("position","relative");
}

/*前へをクリックして大きい写真を移動
******************************************************************************/
	$("a#prev").click(function(){
		photo_index = (photo_index - 1 > 0) ? photo_index - 1 : 1;
		slideToIndex(photo_index);
		displayNavigateButton(photo_index);
	});
/*次へをクリックして大きい写真を移動
******************************************************************************/
	$("a#next").click(function(){
		photo_index = (photo_index + 1 < photo_cnt + 1) ? photo_index + 1 : photo_cnt;
		slideToIndex(photo_index);
		displayNavigateButton(photo_index);
	});
	

/*サムネールをクリックして大きい写真を移動
******************************************************************************/
	$(".th_photos a").click(function(){
		var id_num = $(this).attr("id");
		id_num = id_num.replace("p_id","");
		photo_index = new Number(id_num);
		slideToIndex(photo_index);
		timerStop();
		$(this).blur();
		displayNavigateButton(photo_index);
	});

/*サムネールをマウスオーバーアクション
******************************************************************************/
	$(".th_photos a").hover(function(){
		var now_th_id = $(this).attr("id");
		$("#"+now_th_id).css("filter","alpha(opacity=70)");
		$("#"+now_th_id).css("-moz-opacity","0.7");
		$("#"+now_th_id).css("opacity","0.7");
	},function(){
		var now_th_id = $(this).attr("id");
		$("#"+now_th_id).css("filter","alpha(opacity=100)");
		$("#"+now_th_id).css("-moz-opacity","1");
		$("#"+now_th_id).css("opacity","1");
	});

/*ボタンのマウスオーバー
******************************************************************************/
$("#list_back img").hover(function(){
	$(this).attr("src","img/back_on.gif");
},function(){
	$(this).attr("src","img/back.gif");
});

$("#list_next img").hover(function(){
	$(this).attr("src","img/next_on.gif");
},function(){
	$(this).attr("src","img/next.gif");
});

$("#first img").hover(function(){
	$(this).attr("src","img/first_on.gif");
},function(){
	$(this).attr("src","img/first.gif");
});

$("#end img").hover(function(){
	$(this).attr("src","img/end_on.gif");
},function(){
	$(this).attr("src","img/end.gif");
});

$("#autoplay img").hover(function(){
	$(this).attr("src","img/autoplay_on.gif");
},function(){
	var ap_status = $("#autoplay").attr("class");
	(ap_status == "ap_on")? $("#autoplay img").attr("src","img/autoplay_on.gif") : $("#autoplay img").attr("src","img/autoplay.gif");
});


});
/* jqueryはここでおしまい */

function slideToIndex(index){
	if(ua.msie){
		var now_prev_id = $("#list_back img").attr("class");
		var now_next_id = $("#list_next img").attr("class");
		$(".photo_caption").hide();
		$("#main"+index).fadeIn(800);
		$("#list_back img").removeClass(new String(now_prev_id));
		$("#list_next img").removeClass(new String(now_next_id));
		var and_prev_id =new Number(index) -1;
		var and_next_id =new Number(index) +1;
		$("#list_back img").addClass(new String(and_prev_id));
		$("#list_next img").addClass(new String(and_next_id));
	}else{
		var move_amount_def = -785;
		var move_amount = move_amount_def * (new Number(index)-1);
		$("#belt").animate({left:move_amount},800,"easeInOutCubic");
	}
}

function displayNavigateButton(index){
	if(index == 1){
		$("a#prev").css({opacity:0.5,cursor:"default"});
	}else{
		$("a#prev").css({opacity:1,cursor:"pointer"});
	}
	if(index == photo_cnt){
		$("a#next").css({opacity:0.5,cursor:"default"});
	}else{
		$("a#next").css({opacity:1,cursor:"pointer"});
	}
}

function autoPlay(){
	var ua =	$.browser;
	if(ua.msie){
		var photos_cnt = $(".photos").size();
		var now_th_id = $(".th_on").attr("id");
	
		now_th_id = new Number(now_th_id.replace("p_id", ""));
		$(".photos").css("filter","alpha(opacity=70)");
		$(".photos").css("-moz-opacity","0.7");
		$(".photos").css("opacity","0.7");
		$("#p_id" + now_th_id + " .photos").css("filter","alpha(opacity=100)");
	
		var next_th_id = now_th_id +1;
		if(now_th_id <= photos_cnt){
			$("#p_id" + now_th_id).removeClass("th_on");
			$("#p_id" + next_th_id).addClass("th_on");
			if(now_th_id != 1){
				$(".photo_caption").hide();
				$("#main"+now_th_id).fadeIn(1500);
			}
			if(now_th_id == photos_cnt) timerStop();
		}
	
	}else{

		var now_belt_w = $("#belt").css("width");
		now_belt_w = new Number(now_belt_w.replace("px", ""));
		var scroll_end = 532 - now_belt_w;
		var belt_posi = $("#belt").css("left");
		belt_posi = new Number(belt_posi.replace("px", ""));
		var belt_move = belt_posi - 532;
		
		if(belt_move < scroll_end){
			belt_move = scroll_end;
			timerStop();
		}
		
		$("#belt").animate({left:belt_move},1500,"easeInOutCubic");
	}
}


function timerStop(){
	var ap_flg = $("#autoplay").attr("class");
	if(ap_flg == "ap_on"){
  	clearInterval(apTimer);
		$("#autoplay img").attr("src","img/autoplay.gif");
		$(".photos").css("filter","alpha(opacity=100)");
		$(".photos").css("-moz-opacity","1");
		$(".photos").css("opacity","1");
		$("#autoplay").removeClass("ap_on");
  }
}
