// 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 empty pipette to HCl
pipette[0].addEventListener("mousedown", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    pipette[0].dX = sX - pipette[0].x;
    pipette[0].dY = sY - pipette[0].y;
    pipette[0].cursor = "pointer";
});

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

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

    if (config.current_step == 2) {
    	if (pipette[0].x > beaker_area.x-config.sticky_px && pipette[0].x < beaker_area.x+beaker_area.width+config.sticky_px && pipette[0].y > beaker_area.y-config.sticky_px && pipette[0].y < beaker_area.y+beaker_area.height+config.sticky_px) {
			anim_step = 2.1;
    	}
    }

    if (config.current_step == 3) {
    	if (pipette[0].x > beaker_area.x-config.sticky_px && pipette[0].x < beaker_area.x+beaker_area.width+config.sticky_px && pipette[0].y > beaker_area.y-config.sticky_px && pipette[0].y < beaker_area.y+beaker_area.height+config.sticky_px) {
			anim_step = 3.1;
    	}
    }

    if (config.current_step == 4) {
    	if (pipette[0].x > beaker_area.x-config.sticky_px && pipette[0].x < beaker_area.x+beaker_area.width+config.sticky_px && pipette[0].y > beaker_area.y-config.sticky_px && pipette[0].y < beaker_area.y+beaker_area.height+config.sticky_px) {
			anim_step = 4.1;
    	}
    }  

    stage.update();
});



// Move full pipette to tubes
pipette[1].addEventListener("mousedown", function(event) {
    var sX = Math.floor(event.stageX);
    var sY = Math.floor(event.stageY);
    pipette[1].dX = sX - pipette[1].x;
    pipette[1].dY = sY - pipette[1].y;
    pipette[1].cursor = "pointer";
});

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


    // Go to tube1 area
    if (config.current_step == 1) {
    	if (pipette[1].x > tube1_area.x-config.sticky_px && pipette[1].x < tube1_area.x+tube1_area.width+config.sticky_px && pipette[1].y > tube1_area.y-config.sticky_px && pipette[1].y < tube1_area.y+tube1_area.height+config.sticky_px) {
			anim_step = 1.2;
    	}
    }

    // Go to tube2 area
    if (config.current_step == 2) {
    	if (pipette[1].x > tube2_area.x-config.sticky_px && pipette[1].x < tube2_area.x+tube2_area.width+config.sticky_px && pipette[1].y > tube2_area.y-config.sticky_px && pipette[1].y < tube2_area.y+tube2_area.height+config.sticky_px) {
			anim_step = 2.2;
    	}
    }

    // Go to tube3 area
    if (config.current_step == 3) {
    	if (pipette[1].x > tube3_area.x-config.sticky_px && pipette[1].x < tube3_area.x+tube3_area.width+config.sticky_px && pipette[1].y > tube3_area.y-config.sticky_px && pipette[1].y < tube3_area.y+tube3_area.height+config.sticky_px) {
			anim_step = 3.2;
    	}
    }

    // Go to tube4 area
    if (config.current_step == 4) {
    	if (pipette[1].x > tube4_area.x-config.sticky_px && pipette[1].x < tube4_area.x+tube4_area.width+config.sticky_px && pipette[1].y > tube4_area.y-config.sticky_px && pipette[1].y < tube4_area.y+tube4_area.height+config.sticky_px) {
			anim_step = 4.2;
    	}
    }

    stage.update();
});

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

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


    // Go to tube5 area
    if (config.current_step == 5) {
    	if (ribbon.x > tube5_area.x-config.sticky_px && ribbon.x < tube5_area.x+tube1_area.width+config.sticky_px && ribbon.y > tube5_area.y-config.sticky_px && ribbon.y < tube5_area.y+tube5_area.height+config.sticky_px) {
			anim_step = 5.2;
    	}
    }

    stage.update();
});

