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


function show_step(val) {

	$("#help_step1").hide();
	$("#help_step2").hide();
	$("#help_step3").hide();
	$("#help_step4").hide();
	$("#help_step5").hide();
	$("#help_step6").hide();
	$("#help_step7").hide();
	$("#help_step8").hide();
	$("#help_step9").hide();
	$("#help_step10").hide();
	$("#help_step11").hide();
	$("#help_step12").hide();
	$("#help_step13").hide();

	if (val != 12) {
		$("#lab_question1").hide();
	}

	$("#help_step" + val.toString()).show();
	config.current_step = val;
	return true;

}

function show_flame(color = '') {
	
	hide_flame();
	
	flame_area.visible = true;
	flame_area.width = 150;
	flame_area.height = 50;

	if (color == 'Y') {flameY.visible = true; return true;}
	if (color == 'R') {flameR.visible = true; return true;}
	if (color == 'P') {flameP.visible = true; return true;}
	if (color == 'G') {flameG.visible = true; return true;}
	if (color == 'L') {flameL.visible = true; return true;}
	flame.visible = true;
	
}

function hide_flame() {
	flameY.visible = false;
	flameR.visible = false;
	flameP.visible = false;
	flameG.visible = false;
	flameL.visible = false;
	flame.visible = false;
	flame_area.visible = false;
	flame_area.width = 0;
	flame_area.height = 0;
	return true;
}

function check_qst1(val) {
	if (val == 3) {
		$("#qst1_alert").hide();
		show_step(12);
	} else {
		$("#qst1_alert").show();
	}
	return true;
}


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

let anim_step = 0;
let anim_timer = 0;

function tickHandler(e) {

	// switch to normal flame after a while
	if (flameY.visible || flameR.visible || flameP.visible || flameG.visible || flameL.visible) {
		anim_timer++;

		if (anim_timer > 120) {
			anim_timer = 0;
			anim_step = 99;
		}
	}

	if (anim_step == 1.1 && scissor[0].visible) {
		scissor[0].x = config.center_x - 280;
		scissor[0].y = config.center_y + 165;
		scissor[1].x = config.center_x - 280;
		scissor[1].y = config.center_y + 160;
		scissor[0].visible = false;
		mg.visible = false;
		mg_area.visible = false;
		scissor[1].visible = true;		
	}

	if (anim_step == 2.1 && scissor[1].visible) {
		scissor[1].visible = false;
		scissor[2].visible = true;
		plate_area1.visible = true;
		plate_area1.width = 200;
		plate_area1.height = 40;
		show_step(3);
	}

	if (anim_step == 3.1 && scissor[2].visible) {
		scissor[2].visible = false;
		scissor[3].visible = true;
		plate_area1.visible = false;
		plate_area1.width = plate_area1.height = 0;
	}

	if (anim_step == 3.2 && flame.visible) {
		show_flame('Y');
		scissor[2].visible = false;
		scissor[3].visible = false;
		scissor[4].visible = false;
		scissor[1].visible = true;	// return to clean mg ribbon
		show_step(4);
	}

	if (anim_step == 4.1 && scissor[1].visible) {
		scissor[1].visible = false;
		scissor[2].visible = true;
		plate_area2.visible = true;
		plate_area2.width = 200;
		plate_area2.height = 40;
		show_step(5);
	}

	if (anim_step == 5.1 && scissor[2].visible) {
		scissor[2].visible = false;
		scissor[3].visible = true;
		plate_area2.visible = false;
		plate_area2.width = plate_area2.height = 0;
	}

	if (anim_step == 5.2 && flame.visible) {
		show_flame('G');
		scissor[2].visible = false;
		scissor[3].visible = false;
		scissor[4].visible = false;
		scissor[1].visible = true;	// return to clean mg ribbon
		show_step(6);
	}

	if (anim_step == 6.1 && scissor[1].visible) {
		scissor[1].visible = false;
		scissor[2].visible = true;
		plate_area3.visible = true;
		plate_area3.width = 200;
		plate_area3.height = 40;
		show_step(7);
	}

	if (anim_step == 7.1 && scissor[2].visible) {
		scissor[2].visible = false;
		scissor[3].visible = true;
		plate_area3.visible = false;
		plate_area3.width = plate_area3.height = 0;
	}

	if (anim_step == 7.2 && flame.visible) {
		show_flame('R');
		scissor[2].visible = false;
		scissor[3].visible = false;
		scissor[4].visible = false;
		scissor[1].visible = true;	// return to clean mg ribbon
		show_step(8);
	}

	if (anim_step == 8.1 && scissor[1].visible) {
		scissor[1].visible = false;
		scissor[2].visible = true;
		plate_area4.visible = true;
		plate_area4.width = 200;
		plate_area4.height = 40;
		show_step(9);
	}

	if (anim_step == 9.1 && scissor[2].visible) {
		scissor[2].visible = false;
		scissor[4].visible = true;
		plate_area4.visible = false;
		plate_area4.width = plate_area4.height = 0;
	}

	if (anim_step == 9.2 && flame.visible) {
		show_flame('P');
		scissor[2].visible = false;
		scissor[3].visible = false;
		scissor[4].visible = false;
		scissor[1].visible = true;	// return to clean mg ribbon
		show_step(10);
	}

	if (anim_step == 10.1 && scissor[1].visible) {
		scissor[1].visible = false;
		scissor[2].visible = true;
		plate_area5.visible = true;
		plate_area5.width = 200;
		plate_area5.height = 40;
		show_step(11);
	}

	if (anim_step == 11.1 && scissor[2].visible) {
		scissor[2].visible = false;
		scissor[3].visible = true;
		plate_area5.visible = false;
		plate_area5.width = plate_area5.height = 0;
	}

	if (anim_step == 11.2 && flame.visible) {
		show_flame('L');
		scissor[2].visible = false;
		scissor[3].visible = false;
		scissor[4].visible = false;
		scissor[1].visible = true;	// return to clean mg ribbon
		$("#lab_question1").show();
	}

	if (anim_step == 13.1) {

		anim_timer++;
		
		// move scissor1 back table
		if (anim_timer < 60) {
			if (scissor[1].x < config.center_x - 320) {
				scissor[1].x += 10;
			}
			if (scissor[1].x > config.center_x - 320)  {
				scissor[1].x -= 10;
			}
			if (scissor[1].y < config.center_y + 170) {
				scissor[1].y += 10;
			}
			if (scissor[1].y > config.center_y + 170) {
				scissor[1].y -= 10;
			}
		} else {
			anim_timer = 0;
			anim_step = 13.2;
		}
	}

	if (anim_step == 13.2) {
		// separate scissor from ribon
		scissor[1].visible = false;
		scissor[0].x = config.center_x - 320;
		scissor[0].y = config.center_y + 170;
		scissor[0].visible = true;
		mg.visible = true;
		show_step(13);
	}

	if (anim_step == 99) {
		show_flame();
	}

	stage.update();

}
