// Slide setting //

var slideMaxNum = 109;

var movingTime = 800 //イメージ移動の時間。ミリ秒で指定（1sec=1000msec）
var stoppingTime = 3000 //イメージを止める時間。ミリ秒で指定（1sec=1000msec）

// end of setting //




var randnum = Math.floor( Math.random() * slideMaxNum );
var errorCount = 0;

$(function(){
	//test
	//randnum=74
	
	//初期ロード
	callImage()

})

function callImage(){
	
	var imagefile="swf_img/"+randnum+".jpg"
	n=loadImage(imagefile);

}

function loadImage(imagefile){
	var img = new Image();
	var flg="aaaa"
	$(img).load(function() {
		$('.topSlideShow a').append('<img src="'+imagefile+'" style="left:291px;" />');
		var t = setTimeout(moveImage, stoppingTime);
		errorCount = 0;
			
	}).error(function() {
		errorCount +=1
		if (randnum<slideMaxNum){
			randnum += 1;
		}else{
			randnum=0;	
		};
		imagefile="swf_img/"+randnum+".jpg"
		if(errorCount>10){
			alert("イメージファイルが呼び出せませんでした")
		}else{
			loadImage(imagefile)
		}
	}).attr('src', imagefile).css("left","291px");   


/*	
	img.onload = function(){
		$('.topSlideShow').append('<img src="'+imagefile+'" style="left:291px;" />');
		var t = setTimeout(moveImage, stoppingTime);

		flg =  "a";
	}

	img.onerror = function() {
		if (randnum<slideMaxNum){
			randnum += 1;
		alert("koko")
		}else{
			randnum=0;	
		};

		flg =  "b";
	}
	img.src = imagefile;
	
	alert(flg)
*/
}

function moveImage(){
		$(".topSlideShow img").eq(0).animate({"left": "-=291px"}, movingTime,"linear", function () {
			$(this).remove();
			if (randnum<slideMaxNum){
				randnum += 1;
			}else{
				randnum=0;	
			};
			callImage()
	    }
);
		$(".topSlideShow img").eq(1).animate({"left": "-=291px"}, movingTime,"linear" );
}
