// JavaScript Document

var totalNum=0;
var nowNum=0;
var countUp=1;
var nowChangeFlg=true;
var nowOverFlg=false;

var timersID;

$(document).ready(function(){
  
	$('p#bn_225_img a').width(0).css('opacity',0.5);
	
	//init
	totalNum=$('p#bn_225_img a img').length;
	
	
	if(totalNum>1){
		
		// click
		$('li.bn_225_li1').click(function(){ count(-1); });
		$('li.bn_225_li2').click(function(){ count(1); });
		
		
		// hover
		$("#bn_225_img").hover(
			function () {
				nowOverFlg=true;
			},
			function () {
				nowOverFlg=false;
			}
		);
		$("#bn_225 ul li").hover(
			function () {
				nowOverFlg=true;
				$(this).fadeTo(100, 0.5);
			},
			function () {
				nowOverFlg=false;
				$(this).fadeTo(70, 1);
			}
		);
	
	}else{
		//１枚しか無い場合
		
		$('div#bn_225 ul').css('display','none');
		$('p#bn_225_nu').css('display','none');
	
	}
	
	
	// show first
	
	timersID = setInterval(function(){ showTarget(0); }, 500);
	
	
});


function count(add){
	
	if(!nowChangeFlg){
		
		var nextNum=nowNum+add;
		if(nextNum<0){
			nextNum=totalNum-1;
		}else if(nextNum>=totalNum){
			nextNum=0;
		}
		
		if(add>0){
			showTarget(nextNum);
		}else{
			showPrev(nextNum);
		}
		
	}
	
	return false;
	
}


/* change photo */
function showTarget(indx){
	
	
	nowChangeFlg=true;
	
	
	nowNum=indx;
	
	clearInterval(timersID);
	
	countUp++;
	
	var targetA=$('p#bn_225_img a:eq('+indx+')');
	
	targetA.stop(true, false).width(0).css('opacity',0.5);
	
	targetA.css('z-index',countUp).animate({ 
    width: 640,
    opacity: 1
  },600 ,$.easie(0.174,0.928,0.259,1));
	
	
	changeNums();
	
	if(totalNum>1){
		startTimer();
	}
}

/* change photo prev */
function showPrev(indx){
	
	nowChangeFlg=true;
	
	var prevNum=indx;
	
	clearInterval(timersID);
	
	countUp++;
	
	var targetA=$('p#bn_225_img a:eq('+nowNum+')');	
	var targetPrev=$('p#bn_225_img a:eq('+prevNum+')');
	
	nowNum=prevNum;
	
	targetA.css('z-index',countUp);
	
	var aIndex=targetA.css('z-index');
	
	targetPrev.css({zIndex:aIndex-1, opacity:0.5}).width(640).stop(true, false).animate({ 
    opacity: 1
  },600 ,$.easie(0.174,0.928,0.259,1));
	
	targetA.stop(true, false).animate({ 
    width: 0
  },600 ,$.easie(0.174,0.928,0.259,1));
	
	
	changeNums();
	
	if(totalNum>1){
		startTimer();
	}
	
}





/* change counter */
function changeNums(){
	
	var str=(nowNum+1)+' / '+totalNum;
	$('p#bn_225_nu').html(str);
	
	nowChangeFlg=false;
	
}


/* auto timer */
function startTimer(){
	
	timersID = setInterval(function(){ timerCountUp(); }, 5000);
	
}

function timerCountUp(){
	
	if(!nowOverFlg){
		
		var nextNum=nowNum+1;
		if(nextNum>=totalNum){
			nextNum=0;
		}
		
		showTarget(nextNum);
		
	}
	
}


