/* --------------------------------------------------
Author: Anastasios Pallas / tcpallas@gmail.com
Version 1.0
Last Update: 2024-07-08
-------------------------------------------------- */


// Update stage will render next frame
createjs.Ticker.framerate = 15;
createjs.Ticker.addEventListener("tick", tickHandler);

var anim_timer = 0;

function tickHandler(e) {
	stage.update();

	if (anim_step == 1) {

		beaker_ki[0].visible = false;
		beaker_ki[1].visible = true;

		if (orthostatis[0].visible) {
			beaker_ki[1].x = config.center_x + 155;
			beaker_ki[1].y = config.center_y - 60;

			// rotate beaker KI
			if (beaker_ki[1].visible && beaker_ki[1].rotation < 60) {
				beaker_ki[1].rotation += 5;
			}
		}

		// fill the tube
		if (beaker_ki[1].visible && beaker_ki[1].rotation == 60) {
			orthostatis[0].visible = false;
			orthostatis[1].visible = true;
		}

		// rotate beaker KI back
		if (orthostatis[1].visible && beaker_ki[1].visible && beaker_ki[1].rotation > 0) {
			beaker_ki[1].rotation -= 5;
		}

		// put beaker KI back to table
		if (orthostatis[1].visible && beaker_ki[1].rotation == 0) {
			beaker_ki[1].visible = false;
			beaker_ki[2].visible = true;
		}

		if (beaker_ki[2].visible) {
			if (beaker_ki[2].x < config.center_x - 190) {
				beaker_ki[2].x += 5;
			}
			if (beaker_ki[2].x > config.center_x - 190)  {
				beaker_ki[2].x -= 5;
			}
			if (beaker_ki[2].y < config.center_y + 142) {
				beaker_ki[2].y += 5;
			}
			if (beaker_ki[2].y > config.center_y + 142) {
				beaker_ki[2].y -= 5;
			}
		}

		config.current_step = 2;
		$("#help_step1").hide();
		$("#help_step2").show();

	}

	if (anim_step == 2) {

		anim_timer++;

		beaker_pb[0].visible = false;
		beaker_pb[1].visible = true;

		if (orthostatis[1].visible) {
			beaker_pb[1].x = config.center_x + 155;
			beaker_pb[1].y = config.center_y - 60;

			// rotate beaker Pb
			if (beaker_pb[1].visible && beaker_pb[1].rotation < 60) {
				beaker_pb[1].rotation += 5;
			}
		}

		// fill the tube
		if (beaker_pb[1].visible && beaker_pb[1].rotation == 60) {
			orthostatis[1].visible = false;
			orthostatis[2].visible = true;
		}

		// rotate beaker Pb back
		if (orthostatis[2].visible && beaker_pb[1].visible && beaker_pb[1].rotation > 0) {
			beaker_pb[1].rotation -= 5;
		}

		// put beaker Pb back to table
		if (orthostatis[2].visible && beaker_pb[1].rotation == 0) {
			beaker_pb[1].visible = false;
			beaker_pb[2].visible = true;
		}

		if (beaker_pb[2].visible) {
			if (beaker_pb[2].x < config.center_x - 50) {
				beaker_pb[2].x += 5;
			}
			if (beaker_pb[2].x > config.center_x - 50)  {
				beaker_pb[2].x -= 5;
			}
			if (beaker_pb[2].y < config.center_y + 142) {
				beaker_pb[2].y += 5;
			}
			if (beaker_pb[2].y > config.center_y + 142) {
				beaker_pb[2].y -= 5;
			}
		}

		if (beaker_pb[2].visible && anim_timer > 60) {
			orthostatis[2].visible = false;
			orthostatis[3].visible = true;
		}

		if (orthostatis[3].visible && anim_timer > 60) {
			anim_timer = 0;
			config.current_step = 3;
			$("#help_step1").hide();
			$("#help_step2").hide();
			$("#help_step3").show();
			anim_step = 3;
		}

	}

	if (anim_step == 3) {

		anim_timer++;

		if (anim_timer > 30) {
			orthostatis[3].visible = false;
			orthostatis[4].visible = true;
		}

		if (anim_timer > 60) {
			orthostatis[4].visible = false;
			orthostatis[5].visible = true;
		}

		if (anim_timer > 90) {
			orthostatis[5].visible = false;
			orthostatis[6].visible = true;
		}

		if (anim_timer > 120) {
			orthostatis[6].visible = false;
			orthostatis[7].visible = true;
		}

		if (orthostatis[7].visible) {
			// experiment is completed
			config.current_step = 4;
			$("#help_step1").hide();
			$("#help_step2").hide();
			$("#help_step3").hide();
			$("#help_step4").show();
			anim_timer = 0;
		}
	}


}
