﻿
var showindex = 1;
var image = new Image();
var animations = new Array();
var timerID1,timerID2;

// アニメーションオブジェクト
var Animation = function() {
    this.index = 1;
    this.images = generateImages();
    this.src = "";
}

function generateImages() {
	var images = new Array(716);
	for (i = 1; i <= 716; i++) {
		images[i - 1] = new Image();
	}

    return images;
}


function animateImage(id, fileName, count, title, delay) {
    delay = 50;
    
    var anim = new Animation();
    animations[0] = anim; // XXX: IE5 does not support Array.push()

    timerID1 = setInterval('loadimage()', 5);
    timerID2 = setInterval('animate()', delay);

}

function loadimage(){

	temp = animations[0].index;
	tempstr = temp + "";
	if (tempstr.length == 1){
		temp = "00" + temp;
		}
	else if(tempstr.length == 2){
		temp = "0" + temp;
		}
        srcstr = "./out_anime/" + temp + ".gif";
	image.src = srcstr;
	animations[0].images[animations[0].index - 1].src = image.src;

	animations[0].index ++;
	if(animations[0].index >= 716){
		clearInterval(timerID1);
		animations[0].index ++;
	}

}


function animate() {

	//読み込み確認
	if(animations[0].images[showindex - 1].complete && animations[0].index >= 3){
		showindex++;
		if(showindex >= 716 ){
		showindex = 1;
		}
	}
	else{
		return;
	}

	
	document.anime.src = animations[0].images[showindex - 1].src;
//window.status = animations[0].index + "," + showindex + "+" + showindex + "=" + animations[0].images[showindex].complete;
//window.status = animations[0].index + "," +animations[0].index + "," + showindex;

}









var Animation_vision = function() {
    this.index = 1;
    this.images = generateImages_vision();
    this.src = "";
}

function generateImages_vision() {
	var images = new Array(360);
	for (i = 1; i <= 360; i++) {
		images[i - 1] = new Image();
	}
    return images;
}

function animateImage_vision(id, fileName, count, title, delay) {
    delay = 30;
    
    var anim = new Animation_vision();
    animations[0] = anim; // XXX: IE5 does not support Array.push()

    timerID1 = setInterval('loadimage_vision()', 1);
    timerID2 = setInterval('animate_vision()', delay);
}

function loadimage_vision(){
	temp = animations[0].index - 1;
	tempstr = temp + "";
	if (tempstr.length == 1){
		temp = "00" + temp;
		}
	else if(tempstr.length == 2){
		temp = "0" + temp;
		}
        srcstr = "./vision_anime/" + temp + ".gif";
	image.src = srcstr;
	animations[0].images[animations[0].index - 1].src = image.src;

	animations[0].index = animations[0].index + 5;


	if(animations[0].index >= 360){
		clearInterval(timerID1);
		animations[0].index ++;
		animations[0].index ++;
	}
}

function animate_vision() {


	if(animations[0].images[showindex - 1].complete && animations[0].index >= 350){
		showindex = showindex + 20;


		if(showindex >= 360 ){
		showindex = 1;
		}
	}
	else{
		return;
	}
	
	document.anime_v.src = animations[0].images[showindex - 1].src;


	if(showindex == 1 ){
		document.anime_v2.src = animations[0].images[355].src;
		document.anime_v3.src = animations[0].images[350].src;
		document.anime_v4.src = animations[0].images[345].src;
		document.anime_v5.src = animations[0].images[340].src;
	}else{
	document.anime_v2.src = animations[0].images[showindex - 1 - 5].src;
	document.anime_v3.src = animations[0].images[showindex - 1 - 10].src;
	document.anime_v4.src = animations[0].images[showindex - 1 - 15].src;
	document.anime_v5.src = animations[0].images[showindex - 1 - 20].src;
	}
//window.status = animations[0].images[showindex - 1].src;
//window.status = (showindex - 1) + "," + (showindex -6) + "," + (showindex - 11);
//window.status = animations[0].index + "," + showindex + "+" + showindex + "=" + animations[0].images[showindex].complete;
//window.status = animations[0].index + "," +animations[0].index + "," + showindex;

}





var lightpos = 1; //ライトの位置
var target = 0; //移動したい位置
var temptarget = 0; //ひとまず移動する位置
var passtime = 0.0; //sec
var timerID_move;


//画像移動
function moveIMG(x,y)
{
	document.redlight.style.left = x; //x
	document.redlight.style.top = y; //y
}

//経過時間によって座標変化
function changePos(){

	if(temptarget >= 3 && lightpos >= 3){
	passtime += 0.33;
	}else{
	passtime += 0.2;
	}

	//現在の座標を取得
	pos = new Array(2);
	pos = CalcPos(lightpos, temptarget, passtime);

	//その座標に移動
	moveIMG(pos[0],pos[1])

	//移動が完了したか確認
	if (passtime >= 0.99){
		//完了していたらlightposとターゲット更新
		lightpos = temptarget;
		passtime = 0;

		sa = target - lightpos;

		if(Math.abs(sa) >= 2){ //差が2以上なら隣を対称にする
			if(sa > 0){
				temptarget = lightpos + 1;
			}else {
				temptarget = lightpos - 1;
			}
		}else{
			temptarget = target;
		}

	}


	//完了確認
	//完了していたら終わる
	//clearInterval(timerID_move);
	if(target == lightpos){
		clearInterval(timerID_move);
	}
}


//座標を内挿で取得(元の場所、対象の場所、passtime)
//targetはいける場所に変更する必要がある
function CalcPos(lightpos, temptarget, passtime){
	var x0,x1,y0,y1;


	switch(lightpos){
		case 1:
			x0 = 368;
			y0 = 171;
			break;
		
		case 2:
			x0 = 346;
			y0 = 263;
			break;

		case 3:
			x0 = 335;
			y0 = 356;
			break;

		case 4:
			x0 = 333;
			y0 = 403;
			break;

		case 6:
			x0 = 344;
			y0 = 497;
			break;

		case 5:
			x0 = 338;
			y0 = 449;
			break;

		case 7:
			x0 = 359;
			y0 = 544;
			break;


	}

	switch(temptarget){
		case 1:
			x1 = 368;
			y1 = 171;
			break;
		
		case 2:
			x1 = 346;
			y1 = 263;
			break;

		case 3:
			x1 = 335;
			y1 = 356;
			break;

		case 4:
			x1 = 333;
			y1 = 403;
			break;

		case 6:
			x1 = 344;
			y1 = 497;
			break;

		case 5:
			x1 = 338;
			y1 = 449;
			break;

		case 7:
			x1 = 359;
			y1 = 544;
			break;

	}

	x = Math.round(((x1 - x0) * passtime) + x0);
	y = Math.round(((y1 - y0) * passtime) + y0);
//window.status = x + "," + y;

	temp_r = new Array(x,y);
	return temp_r;

}


//ターゲットと現在地を設定してループ作成
function startLightAnime(gettarget){

	document.flow_str.src = "flow/" + gettarget + ".jpg?110524"

//画像のトップ位置を修正





	//元に戻す
	document.flow_entry.src = "flow/f_entry_off.gif?110524";
	document.flow_screening.src = "flow/f_screening_off.gif?110524";
	document.flow_exam.src = "flow/f_exam_off.gif?110524";
	document.flow_inter.src = "flow/f_inter_off.gif?110524";
	document.flow_empl.src = "flow/f_empl_off.gif?110524";

	document.flow_link.style.visibility="hidden";


	switch(gettarget){
		case 1:
			document.flow_entry.src = "flow/f_entry_w.gif?110524";	
			document.flow_link.style.visibility="visible";

			document.flow_str.style.top=294;

			break;
			
		case 2:
			document.flow_screening.src = "flow/f_screening_w.gif?110524";

			document.flow_str.style.top=321;
			break;
		case 3:
			document.flow_exam.src = "flow/f_exam_w.gif?110524";

			document.flow_str.style.top=313;
			break;
		case 5:
			document.flow_inter.src = "flow/f_inter_w.gif?110524";

			document.flow_str.style.top=330;
			break;
		case 7:
			document.flow_empl.src = "flow/f_empl_w.gif?110524";

			document.flow_str.style.top=330;
			break;
	}


	//初期化
	passtime = 0.0;
	clearInterval(timerID_move);

	target = gettarget;

	sa = target - lightpos;

	if(Math.abs(sa) >= 2){ //差が2以上なら隣を対称にする
		if(sa > 0){
			temptarget = lightpos + 1;
		}else {
			temptarget = lightpos - 1;
		}
	}else{
		temptarget = target;
	}

	timerID_move = setInterval("changePos()",50);  

}



