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


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

var keep_light = 0;

function tickHandler(e) {

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

	// fade out mg in fiali
	if (config.current_step >= 2) {
		if (mg.alpha > 0.5) {
			mg.alpha -= 0.01;
		}
	}

	// leave scissor on the table
	if (config.current_step == 2) {
		if (scissor.x < config.center_x - 280) {
			scissor.x = scissor.x + 10;
		}
		if (scissor.x > config.center_x - 280)  {
			scissor.x = scissor.x - 10;
		}
		if (scissor.y < config.center_y + 165) {
			scissor.y = scissor.y + 10;
		}
		if (scissor.y > config.center_y + 165) {
			scissor.y = scissor.y - 10;
		}
	}


	if (config.current_step == 3) {
		// rotate bottle front
		if (bottle2.visible && bottle2.rotation != 30) {
			bottle2.rotation++;
		}

		// fill fiali with HCl
		if (bottle2.rotation == 30) {
			bubbles.visible = true;

			// go to next step
			config.current_step = 4;
			$("#help_step3").hide();
        	$("#help_step4").show();
		}
	}


	if (config.current_step == 4) {

		// show fiali with HCl
		fiali.visible = false;
		fiali2.visible = true;

		// rotate bottle back
		if (bottle2.visible && bottle2.rotation != 0) {
			bottle2.rotation--;
		}

		// show empty bottle
		if (bottle2.visible && bottle2.rotation == 0) {
			bottle2.visible = false;
			bottle3.visible = true;
		}

		// animate bubbles
		if (bubbles.y < config.center_y - 20) {
			bubbles.y = config.center_y + 10;
			bubbles.alpha = 1;
		} else {
			bubbles.y -= 1;
			bubbles.alpha -= 0.05;
		}

		// leave empty bottle on the table
		if (bottle3.visible) {
			if (bottle3.x < config.center_x - 130) {
				bottle3.x = bottle3.x + 10;
			}
			if (bottle3.x > config.center_x - 130)  {
				bottle3.x = bottle3.x - 10;
			}
			if (bottle3.y < config.center_y - 20) {
				bottle3.y = bottle3.y + 10;
			}
			if (bottle3.y > config.center_y - 20) {
				bottle3.y = bottle3.y - 10;
			}
		
		}			
	}


	if (config.current_step == 5) {

		// animate light - scale between 0.9 to 1.4)
		lightscale = (Math.floor(Math.random() * (140 - 90 + 1) ) + 90) / 100;
		light.scaleX = light.scaleY = lightscale;

		
		// keep light for a few seconds
		keep_light++;

		if (keep_light > 40) {
			// leave spirto on the table
			if (spirto.x < config.center_x - 40) {
				spirto.x = spirto.x + 10;
			}
			if (spirto.x > config.center_x - 40)  {
				spirto.x = spirto.x - 10;
			}
			if (spirto.y < config.center_y + 190) {
				spirto.y = spirto.y + 10;
			}
			if (spirto.y > config.center_y + 190) {
				spirto.y = spirto.y - 10;
			}

			light.visible = false;
			bubbles.visible = false;
		}

		// Move light together with spirto
   		light.x = spirto.x + 105;
   		light.y = spirto.y + 10;
	}	

	stage.update();

}
