// 終了時間定義
// 年
var END_TIME_Year = 2008;

// 月
var END_TIME_Month = 5;

// 日
var END_TIME_Day = 31;

// 時
var END_TIME_Hour = 23;

// 分
var END_TIME_Minutes = 36;

// 秒
var END_TIME_Seconds = 0;

// タイマーの間隔(ミリ秒)・・・適正は50～10。小さいほど早く回転しますが、スペックが必要になります。
var TIMER_span_ms = 50;

// 画像ファイル定義
var preLoadImagesURL = new Array(
	"./img/0.gif",
	"./img/1.gif",
	"./img/2.gif",
	"./img/3.gif",
	"./img/4.gif",
	"./img/5.gif",
	"./img/6.gif",
	"./img/7.gif",
	"./img/8.gif",
	"./img/9.gif"
);

// 開始時画像ファイル定義
var START_IMG = "./img/start.gif";

// 終了時画像ファイル定義
var END_IMG = "./img/end.gif";

// 終了時文字表示定義
var END_MOJI = "終～了～！！";

// 終了時表示内容設定（0:文字のみ　1:画像のみ　2:両方）
// 初期設定は文字のみ
var DISP_END = '1';

// カウントダウン開始処理
function start_countdown() {

	preLoadImages = new Array(preLoadImagesURL.length);

	for(i = 0; i < preLoadImages; i++){
		preLoadImages[i] = new Image();
		preLoadImages[i].src = preLoadImagesURL[i];
	}

	// 初期化
	document.st.src = START_IMG;
	// 初期値は全て０
	document.dd1.src = preLoadImagesURL[0];
	document.dd2.src = preLoadImagesURL[0];
	document.hh1.src = preLoadImagesURL[0];
	document.hh2.src = preLoadImagesURL[0];
	document.mm1.src = preLoadImagesURL[0];
	document.mm2.src = preLoadImagesURL[0];
	document.ss1.src = preLoadImagesURL[0];
	document.ss2.src = preLoadImagesURL[0];
	//document.ms1.src = preLoadImagesURL[0];
	//document.ms2.src = preLoadImagesURL[0];

	// デバッグ用。これは本番時は外す
	test_change_span(TIMER_span_ms);

	// ワンクッション置く事で、画像が初期値に切り替わって正しく表示される
	setTimeout("disp_countdown_kick()", 1000);
}

// カウントダウンループ呼び出し専用
function disp_countdown_kick() {
	// 表示
	document.getElementById("timer").style.visibility = 'visible';
	// カウントダウン関数の呼び出し
	disp_countdown();
}


// カウントダウンループ処理
function disp_countdown() {

	// 現在日時を取得
	today = new Date();

	// 終了日時を取得
	XDay = new Date(END_TIME_Year,END_TIME_Month - 1,END_TIME_Day,END_TIME_Hour,END_TIME_Minutes,END_TIME_Seconds);

	// 残り日数取得
	if((d = XDay.getTime() - today.getTime()) < 0){
		if(DISP_END == 0) {
			document.getElementById("timer").innerHTML = END_MOJI;
		}else{
			if(DISP_END == 1) {
				document.getElementById("timer").innerHTML = '<img src="' + END_IMG + '" alt="終了" />';
			}else{
				document.getElementById("timer").innerHTML = '<img src="' + END_IMG + '" alt="終了" />' + END_MOJI;
			}
		}
	}else{
		DC = d / (24 * 60 * 60 * 1000)
		d1 = Math.floor(DC / 10);
		d2 = DC % 10;

		// 残り時間取得（ 時 ）
		HC = (d / (60 * 60 *1000)) - (Math.floor(DC) * 24)
		h1 = Math.floor(HC / 10);
		h2 = HC % 10;

		// 残り時間取得（ 分 ）
		MC = (d / (60 * 1000)) - (Math.floor(DC) * 24 * 60) - (Math.floor(HC) * 60);
		m1 = Math.floor(MC / 10);
		m2 = MC % 10;

		// 残り時間取得（ 秒 ）
		SC = (d / 1000) - (Math.floor(DC) * 24 * 60 * 60) - (Math.floor(HC) * 60 * 60) - (Math.floor(MC) * 60);
		s1 = Math.floor(SC / 10);
		s2 = SC % 10;

		// 残り時間取得（ ミリ秒*10 ）
		//MM = (d / 10) - (Math.floor(DC) * 24 * 60 * 60 * 100) - (Math.floor(HC) * 60 * 60 * 100) - (Math.floor(MC) * 60 * 100) - (Math.floor(SC) * 100);
		//mili1 = Math.floor(MM / 10);
		//mili2 = MM % 10;

		// 画像ファイルあてこみ
		document.dd1.src = preLoadImagesURL[d1];
		document.dd2.src = preLoadImagesURL[Math.floor(d2)];
		document.hh1.src = preLoadImagesURL[h1];
		document.hh2.src = preLoadImagesURL[Math.floor(h2)];
		document.mm1.src = preLoadImagesURL[m1];
		document.mm2.src = preLoadImagesURL[Math.floor(m2)];
		document.ss1.src = preLoadImagesURL[s1];
		document.ss2.src = preLoadImagesURL[Math.floor(s2)];
		//document.ms1.src = preLoadImagesURL[mili1];
		//document.ms2.src = preLoadImagesURL[Math.floor(mili2)];

		// ループ処理
		setTimeout("disp_countdown()", TIMER_span_ms);
	}
}

// テスト用。タイマーのスピード調整
function test_change_span(ms){}


