// 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") ) {
		labels.visible = true;
	} else {
		labels.visible = false;
	}
	stage.update();
});


// Move beaker KI to tube
beaker_ki[0].addEventListener("mousedown", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    beaker_ki[0].dX = sX - beaker_ki[0].x;
    beaker_ki[0].dY = sY - beaker_ki[0].y;
    beaker_ki[0].cursor = "pointer";
    beaker_ki[1].x = beaker_ki[0].x;
    beaker_ki[1].y = beaker_ki[0].y;
    beaker_ki[2].x = beaker_ki[0].x;
    beaker_ki[2].y = beaker_ki[0].y;
});

beaker_ki[0].addEventListener("pressmove", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    beaker_ki[0].x = sX - beaker_ki[0].dX;
    beaker_ki[0].y = sY - beaker_ki[0].dY;
    beaker_ki[0].cursor = "pointer";
    beaker_ki[1].x = beaker_ki[0].x;
    beaker_ki[1].y = beaker_ki[0].y;
    beaker_ki[2].x = beaker_ki[0].x;
    beaker_ki[2].y = beaker_ki[0].y;

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

    // Go to tube area
    if (config.current_step == 1) {
    	if (beaker_ki[0].x > tube_area.x-config.sticky_px && beaker_ki[0].x < tube_area.x+tube_area.width+config.sticky_px && beaker_ki[0].y > tube_area.y-config.sticky_px && beaker_ki[0].y < tube_area.y+tube_area.height+config.sticky_px) {
			anim_step = 1;
    	}
    }

    stage.update();
});



// Move beaker Pb(NO3)2 to tube
beaker_pb[0].addEventListener("mousedown", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    beaker_pb[0].dX = sX - beaker_pb[0].x;
    beaker_pb[0].dY = sY - beaker_pb[0].y;
    beaker_pb[0].cursor = "pointer";
    beaker_pb[1].x = beaker_pb[0].x;
    beaker_pb[1].y = beaker_pb[0].y;
    beaker_pb[2].x = beaker_pb[0].x;
    beaker_pb[2].y = beaker_pb[0].y;
});

beaker_pb[0].addEventListener("pressmove", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    beaker_pb[0].x = sX - beaker_pb[0].dX;
    beaker_pb[0].y = sY - beaker_pb[0].dY;
    beaker_pb[0].cursor = "pointer";
    beaker_pb[1].x = beaker_pb[0].x;
    beaker_pb[1].y = beaker_pb[0].y;
    beaker_pb[2].x = beaker_pb[0].x;
    beaker_pb[2].y = beaker_pb[0].y;

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

    // Go to tube area
    if (config.current_step == 2) {
    	if (beaker_pb[0].x > tube_area.x-config.sticky_px && beaker_pb[0].x < tube_area.x+tube_area.width+config.sticky_px && beaker_pb[0].y > tube_area.y-config.sticky_px && beaker_pb[0].y < tube_area.y+tube_area.height+config.sticky_px) {
			anim_step = 2;
    	}
    }

    stage.update();
});
