var ImageList = new Array(13);
var curTimeoutId = 0;
var curImageId = 0;

function initPage(year, month, day, hour)
{
	document.movieimage.moveend.disabled = true;
	loadMovie(year, month, day, hour);
}

function loadMovie(year, month, day, hour)
{
	var idx;
	var targetfile;
	var syear = year;
	var smonth = month;
	for(idx=0; idx<12; idx++){
		targetfile = makefilename(syear, smonth, month, day, hour);
		ImageList[idx] = new Image();
		ImageList[idx].src = targetfile;
		hour++;
		if(hour >= 24){
			hour = 0;
			day++;
			if(get_end_day(year, month) < day){
				day = 1;
				month++;
				if(month > 12){
					month = 1;
					year++;
				}
			}
		}
	}
}

function get_end_day(year,mon)
{
	if(mon != 2){
		var days = new Array(30,31,28,31,30,31,30,31,31,30,31,30,31);
		return days[mon];
	}
	if((year%4==0) && (year%100 !=0) || (year%400==0)){
		return 29;
	}
	return 28;
}

function makefilename(syear, smonth, month, day, hour)
{
	var filename;
	filename = 'shitsudo/'+syear;
	if(smonth < 10)
		filename += '0';
	filename += smonth+'/';
	if(month < 10)
		filename += '0';
	filename += month;
	if(day < 10)
		filename += '0';
	filename += day;
	if(hour < 10)
		filename += '0';
	filename += hour+'.jpg';
	return filename;
}

function startMovie()
{
	var idx;
	document.movieimage.moveend.disabled = false;
	document.movieimage.movestart.disabled = true;
	document.movieimage.moveback.disabled = true;
	document.movieimage.movetop.disabled = true;
	document.movieimage.movenext.disabled = true;

	curImageId = 0;
	document.images['mimage'].src = ImageList[0].src;
	curTimeoutId = setTimeout('ChangeImage(1,true)', 1000);
}

function stopMovie()
{
	if(curTimeoutId > 0)
		clearTimeout(curTimeoutId);
	document.movieimage.moveend.disabled = true;
	document.movieimage.movestart.disabled = false;
	document.movieimage.moveback.disabled = false;
	document.movieimage.movetop.disabled = false;
	document.movieimage.movenext.disabled = false;
}

function ChangeImage(idx,next)
{
	var nextidx = idx + 1;
	if(! ImageList[idx].complete){
		curTimeoutId = setTimeout('ChangeImage('+idx+','+next+')', 1000);
		return;
	}
	curImageId = idx;
	document.images['mimage'].src = ImageList[idx].src;
	if(next){
		if(nextidx < 11){
			curTimeoutId = setTimeout('ChangeImage('+nextidx+',true)', 1000);
		}
		else{
			curTimeoutId = setTimeout('ChangeImage(0,true)', 1000);
		}
	}
}

function startImage()
{
	ChangeImage(0, false);
}

function backImage()
{
	var idx = curImageId-1;
	if(idx < 0)
		idx = 11;
	ChangeImage(idx, false);
}

function nextImage()
{
	var idx = curImageId+1;
	if(idx >= 12)
		idx = 0;
	ChangeImage(idx, false);
}

