// Harbuger menu
$("#hmenu a").on('click', function() {
	$('.bar').toggleClass('animate');
	if ( $("#oer_sidebar").hasClass("hide") ) {
		$("#oer_sidebar").removeClass("hide");
	} else {
		$("#oer_sidebar").addClass("hide");
	}
});


// Controls Show / Hide
$("#p1_toggle").on('click', function() {
	if ( $("#p1").hasClass("show") ) {
		$("#p1").removeClass("down");
		$("#p1_toggle i").removeClass("fa-chevron-circle-down");
		$("#p1_toggle i").addClass("fa-chevron-circle-up");
	} else {
		$("#p1").addClass("down");
		$("#p1_toggle i").removeClass("fa-chevron-circle-up");
		$("#p1_toggle i").addClass("fa-chevron-circle-down");
	}
});


$("#p2_toggle").on('click', function() {
	if ( $("#p2").hasClass("down") ) {
		$("#p2").removeClass("down");
		$("#p2_toggle i").removeClass("fa-chevron-circle-down");
		$("#p2_toggle i").addClass("fa-chevron-circle-up");
	} else {
		$("#p2").addClass("down");
		$("#p2_toggle i").removeClass("fa-chevron-circle-up");
		$("#p2_toggle i").addClass("fa-chevron-circle-down");
	}
});


// Theory Button
$("#btn_theory").on('click', function() {
	if ( $("#oer_theory").hasClass("hide") ) {
		$("#oer_theory").removeClass("hide");
		$("#btn_theory").addClass("active");
		$("#oer_application").addClass("hide");
		$("#btn_application").removeClass("active");
		$("#oer_investigation").addClass("hide");
		$("#btn_investigation").removeClass("active");
	} else {
		$("#oer_theory").addClass("hide");
		$("#btn_theory").removeClass("active");
	}
});


// Application Button
$("#btn_application").on('click', function() {
	if ( $("#oer_application").hasClass("hide") ) {
		$("#oer_theory").addClass("hide");
		$("#btn_theory").removeClass("active");
		$("#oer_application").removeClass("hide");
		$("#btn_application").addClass("active");
		$("#oer_investigation").addClass("hide");
		$("#btn_investigation").removeClass("active");
	} else {
		$("#oer_application").addClass("hide");
		$("#btn_application").removeClass("active");
	}
});


// Investigation Button
$("#btn_investigation").on('click', function() {
	if ( $("#oer_investigation").hasClass("hide") ) {
		$("#oer_theory").addClass("hide");
		$("#btn_theory").removeClass("active");
		$("#oer_application").addClass("hide");
		$("#btn_application").removeClass("active");
		$("#oer_investigation").removeClass("hide");
		$("#btn_investigation").addClass("active");
	} else {
		$("#oer_investigation").addClass("hide");
		$("#btn_investigation").removeClass("active");
	}
});



// Navigation to prev/next applications
$("#oer_app_next").on('click', function() {

});


$("#oer_app_prev").on('click', function() {

});


// Navigation to prev/next investigation
$("#oer_inv_next").on('click', function() {

});

$("#oer_inv_prev").on('click', function() {

});




// Label On/Off
$("#label_switch").on('change', function() {
	if ( $("#label_switch").prop("checked") ) {
		scissor_label.visible = true;
		gas_label.visible = true;
		mg_label.visible = true;
		plate_label.visible = true;
	} else {
		scissor_label.visible = false;
		gas_label.visible = false;
		mg_label.visible = false;
		plate_label.visible = false;
	}
	stage.update();
});


// Gas button
gas_btn.addEventListener("click", function() {
	if (config.current_step == 1 && !flame.visible) {
		flame.visible = true;
		config.current_step = 2;	// go to next step
		$("#help_step1").hide();
		$("#help_step2").show();
		return;
	}

	if (config.current_step == 4 && flame.visible) {
		flame.visible = false;
		config.current_step = 5;
		$("#help_step4").hide();
		$("#help_step5").show();		
		return;
	}

	if (config.current_step == 5) {
		flame.visible = false;
		return;
	}

	if (flame.visible) {
		// just stop flame
		flame.visible = false;
		return;
	}

	if (!flame.visible) {
		// just light flame
		flame.visible = true;
		return;
	}
});



// Scissor
scissor.addEventListener("mousedown", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    scissor.dX = sX - scissor.x;
    scissor.dY = sY - scissor.y;
    scissor.cursor = "pointer";
});

scissor.addEventListener("pressmove", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    scissor.x = sX - scissor.dX;
    scissor.y = sY - scissor.dY;
    scissor.cursor = "pointer";

    // keep object inside canvas
    if (scissor.x < 0) scissor.x = 0;
    if (scissor.x > canvas.width - scissor.width) scissor.x = canvas.width - scissor.width;
    if (scissor.y < 0) scissor.y = 0;
    if (scissor.y > canvas.height - scissor.height) scissor.y = canvas.height - scissor.height;

    // Get mg ribbon
    if (config.current_step == 2) {
    	if (scissor.x > mg_area.x-config.sticky_px && scissor.x < mg_area.x+config.sticky_px && scissor.y > mg_area.y-config.sticky_px && scissor.y < mg_area.y+config.sticky_px) {
        	scissor.visible = false;
        	mg.visible = false;
        	mg_area.visible = false;
        	scissor_ribbon.visible = true;
    	}
    }

    stage.update();
});

scissor.addEventListener("pressup", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
});




// Scissor with ribbon
scissor_ribbon.addEventListener("mousedown", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    scissor_ribbon.dX = sX - scissor_ribbon.x;
    scissor_ribbon.dY = sY - scissor_ribbon.y;
    scissor_ribbon.cursor = "pointer";
});


scissor_ribbon.addEventListener("pressmove", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    scissor_ribbon.x = sX - scissor_ribbon.dX;
    scissor_ribbon.y = sY - scissor_ribbon.dY;
    scissor_ribbon.cursor = "pointer";

    // Move light and dust together with scissor
   	light.x = scissor_ribbon.x + 140;
   	light.y = scissor_ribbon.y - 10;
   	dust.x = scissor_ribbon.x + 150;
   	dust.y = scissor_ribbon.y + 30;

    // keep object inside canvas
    if (scissor_ribbon.x < 0) scissor_ribbon.x = 0;
    if (scissor_ribbon.x > canvas.width - scissor_ribbon.width) scissor_ribbon.x = canvas.width - scissor_ribbon.width;
    if (scissor_ribbon.y < 0) scissor_ribbon.y = 0;
    if (scissor_ribbon.y > canvas.height - scissor_ribbon.height) scissor_ribbon.y = canvas.height - scissor_ribbon.height;

    // Go to gas flame
    if (config.current_step == 2) {
    	if (scissor_ribbon.x > act_area.x-config.sticky_px && scissor_ribbon.x < act_area.x+config.sticky_px && scissor_ribbon.y > act_area.y-config.sticky_px && scissor_ribbon.y < act_area.y+config.sticky_px) {
        	act_area.visible = false;
        	light.visible = true;
        	dust.visible = true;
        	// go to next step
        	config.current_step = 3;
        	$("#help_step2").hide();
        	$("#help_step3").show();
    	}
    }

    // Go to plate
    if (config.current_step == 3) {
    	if (scissor_ribbon.x > plate_area.x-config.sticky_px && scissor_ribbon.x < plate_area.x+config.sticky_px && scissor_ribbon.y > plate_area.y-config.sticky_px && scissor_ribbon.y < plate_area.y+config.sticky_px) {
        	plate_area.visible = false;
        	light.visible = false;
        	dust.visible = false;
        	scissor_ribbon.visible = false;
        	// show scissor without ribbon
        	scissor.x = scissor_ribbon.x;
        	scissor.y = scissor_ribbon.y;
        	scissor.visible = true;
        	mg_dust.visible = true;
        	// go to next step
        	config.current_step = 4;
        	$("#help_step3").hide();
        	$("#help_step4").show();
    	}
    }

    stage.update();
});

scissor_ribbon.addEventListener("pressup", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
});
