/* --------------------------------------------------
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();


	// animate light - scale between 0.6 to 0.9
	if (light.visible) {
		lightscale = (Math.floor(Math.random() * (90 - 60 + 1) ) + 60) / 100;
		light.scaleX = light.scaleY = lightscale;
	}

	// rotate bottle
	if (anim_step == 1) {
   		bottle.x = config.center_x + 180;
		bottle.y = config.center_y - 15;

		if (bottle.rotation < 30) {
			bottle.rotation++;
		}

		if (bottle.rotation == 30) {
			bottle.visible = false;
			orthostatis[0].visible = false;
			tube_area1.visible = false;
			bottle2.visible = true;
			orthostatis[1].visible = true;
			anim_step = 2;
		}
	}

	// put liquid to tube
	if (anim_step == 2) {
		anim_timer++;
	}

	if (anim_step == 2 && anim_timer > 30) {
		// stop running liquid
		bottle2.visible = false;
		bottle.visible = true;
		anim_timer = 0;
		anim_step = 3;
	}

	if (anim_step == 3) {
		
		// rotate bottle back
		if (bottle.rotation != 0) {
			bottle.rotation--;
		}

		if (bottle.rotation == 0) {
			// put bottle back to table
			if (bottle.x < config.center_x - 160) {
				bottle.x += 10;
			}
			if (bottle.x > config.center_x - 160)  {
				bottle.x -= 10;
			}
			if (bottle.y < config.center_y + 80) {
				bottle.y += 10;
			}
			if (bottle.y > config.center_y + 80) {
				bottle.y -= 10;
			}
		// }

		// if (bottle.x == config.center_x - 160 && bottle.y == config.center_y + 80) {
			config.current_step = 2;
			$("#help_step1").hide();
			$("#help_step2").show();
			plate_area.visible = true;
			tube_area2.visible = true;
		}

	}

	if (anim_step == 4) {
		plate_area.visible = false;

		// rotate full spoon to drop dust
		spoon[1].x = config.center_x + 205;
		spoon[1].y = config.center_y - 70;

		if (spoon[1].rotation < 30) {
			spoon[1].rotation++;
		}

		if (spoon[1].rotation == 30) {
			spoon[1].visible = false;
			spoon[0].x = spoon[1].x;
			spoon[0].y = spoon[1].y;
			spoon[0].visible = true;
			orthostatis[0].visible = false;
			orthostatis[1].visible = true;
			anim_step = 5;
		}

	}

	if (anim_step == 5) {
		// tube2_area.visible = false;
		
		// rotate empty spoon back
		if (spoon[0].rotation != 0) {
			spoon[0].rotation--;
		}

		if (spoon[0].rotation == 0) {
			// put spoon back to table
			if (spoon[0].x < config.center_x - 110) {
				spoon[0].x += 10;
			}
			if (spoon[0].x > config.center_x - 110)  {
				spoon[0].x -= 10;
			}
			if (spoon[0].y < config.center_y + 175) {
				spoon[0].y += 10;
			}
			if (spoon[0].y > config.center_y + 175) {
				spoon[0].y -= 10;
			}
		// }

		// if (spoon[0].x == config.center_x - 110 && spoon[0].y == config.center_y + 175) {			
			if (config.current_step == 2) {
				config.current_step = 3;
				$("#help_step1").hide();
				$("#help_step2").hide();
				$("#help_step3").show();

				light_area.visible = true;	// now is possible to light spirto
			}
		}

	}

	if (anim_step >= 5 && anim_step <= 7) {
		// animate bubles
		anim_timer++;
		
		if (anim_timer < 10) {
			orthostatis[1].visible = true;
			orthostatis[2].visible = false;
			orthostatis[3].visible = false;
			orthostatis[4].visible = false;
		} else if (anim_timer < 30) {
			orthostatis[1].visible = false;
			orthostatis[2].visible = true;
			orthostatis[3].visible = false;
			orthostatis[4].visible = false;
		} else if (anim_timer < 50) {
			orthostatis[1].visible = false;
			orthostatis[2].visible = false;
			orthostatis[3].visible = true;
			orthostatis[4].visible = false;
		} else if (anim_timer < 70) {
			orthostatis[1].visible = false;
			orthostatis[2].visible = false;
			orthostatis[3].visible = false;
			orthostatis[4].visible = true;
		} else if (anim_timer < 90) {
			anim_timer = 0;
		}
	}

	if (anim_step == 6) {
		tube_area2.visible = false;
		light_area.visible = false;
		spirto[1].x = config.center_x + 240;
		
		// rotate spirto
		if (spirto[1].rotation < 90) {
			spirto[1].rotation += 5;
			spirto[1].y = config.center_y - 80;
		}

		// put spirto into the tube
		if (spirto[1].rotation == 90 && spirto[1].y != config.center_y - 30) {
			spirto[1].y += 5;
		}

		// light it again!
		if (spirto[1].rotation == 90 && spirto[1].y == config.center_y - 30) {
			anim_timer = 0;
			anim_step = 7;
		}

	}

	if (anim_step == 7) {
		anim_timer++;

		if (anim_timer < 60) {
			light.x = spirto[1].x + 0;
			light.y = spirto[1].y + 40;
			light.visible = true;
		} else {
			// light off spirto
			spirto[0].x = spirto[1].x;
			spirto[0].y = spirto[1].y;
			spirto[0].rotation = spirto[1].rotation;
			spirto[1].visible = false;
			spirto[0].visible = true;
			light.visible = false;
			anim_timer = 0;
			anim_step = 8;
		}
	}

	
	// experiment is completed
	if (anim_step == 8) {

		// put spirto out of the tube
		if (spirto[0].rotation == 90 && spirto[0].y != config.center_y - 80) {
			spirto[0].y -= 5;
		}
		
		// rotate spirto back
		if (spirto[0].y == config.center_y - 80 && spirto[0].rotation != 0) {
			spirto[0].rotation -= 5;
		}

		// put spirto back to table
		if (spirto[0].rotation == 0) {
			// put spoon back to table
			if (spirto[0].x < config.center_x - 0) {
				spirto[0].x += 5;
			}
			if (spirto[0].x > config.center_x - 0)  {
				spirto[0].x -= 5;
			}
			if (spirto[0].y < config.center_y + 175) {
				spirto[0].y += 5;
			}
			if (spirto[0].y > config.center_y + 175) {
				spirto[0].y -= 5;
			}

			orthostatis[0].visible = false;
			orthostatis[1].visible = true;
			orthostatis[2].visible = false;
			orthostatis[3].visible = false;
			orthostatis[4].visible = false;
		}

		config.current_step = 5;
		$("#help_step1").hide();
		$("#help_step2").hide();
		$("#help_step3").hide();
		$("#help_step4").hide();
		$("#help_step5").show();

	}

}
