function show_k1() {
    stage.removeAllChildren();
    //stage.addChild(glow1);
    stage.addChild(lamp_on1);
    stage.addChild(lamp_off1);
    stage.addChild(oudet);
    stage.addChild(oudet_text);
    stage.addChild(fasi_text);
    stage.addChild(switch1_text);
    stage.addChild(switch2_text);
    stage.addChild(switch1_rect);

    switch1_rect.off("click", listener1);

    hit1 = new createjs.Shape();
	hit1.graphics.beginFill("#000").drawRect(59.5,370.5,70,70);
	switch1_rect.hitArea = hit1;
    listener1 = switch1_rect.on("click", toggle_k1_1);


    stage.addChild(fasi1);
    stage.addChild(switch2_rect);
    switch2_rect.off("click", listener2);
    hit2 = new createjs.Shape();
	hit2.graphics.beginFill("#000").drawRect(630.5, 370.5, 70, 70);
    switch2_rect.hitArea = hit2;
    listener2 = switch2_rect.on("click", toggle_k1_2);
    
    
    stage.addChild(fasi2);
    stage.addChild(fasi3);
    stage.addChild(fasi4);
   
    stage.addChild(electron1);
    stage.addChild(electron2);
    stage.addChild(electron3);

    for (var i = 4; i <= 35; i++) {
        stage.addChild(window['electron' + i]);
    }

    


    stage.removeChild(electron31);

    
    
    //stage.addChild(switch2);

    stage.addChild(switchk1_1_l);
    stage.addChild(switchk1_1_r);
    
    stage.addChild(switchk1_2_l);
    stage.addChild(switchk1_2_r);

    if(kyk1_res){
        $("#oer_results p").text("Η λάμπα ανάβει!");
    }
    else{
        $("#oer_results p").text("Η λάμπα δεν ανάβει");

    }
    
    stage.update();
}

function show_initial_k2() {

    console.log("inside  show_initial_k2 !!!")

    //Δ1, Δ3, Δ2: αριστερα-δεξια-δεξιά (αντιστοίχως)-->lamp2 OFF
    stage.removeAllChildren();
    //stage.addChild(glow1);
    stage.addChild(lamp_on2);
    lamp_on2.visible=false;
    stage.addChild(lamp_off2);
    lamp_off2.visible=true;
    stage.addChild(oudet_text);
    stage.addChild(fasi_text);
    stage.addChild(switch1_text);
    stage.addChild(switch2_text);
    stage.addChild(switch3_text);
    stage.addChild(switch1_rect);

    switch1_rect.off("click", listener1);

    hit1 = new createjs.Shape();
	hit1.graphics.beginFill("#000").drawRect(59.5,370.5,70,70);
	switch1_rect.hitArea = hit1;
    listener1 = switch1_rect.on("click", toggle_k2_1);

    stage.addChild(switch2_rect);
    switch2_rect.off("click", listener2);
    hit2 = new createjs.Shape();
	hit2.graphics.beginFill("#000").drawRect(630.5, 370.5, 70, 70);
    switch2_rect.hitArea = hit2;
    listener2 = switch2_rect.on("click", toggle_k2_2);

    stage.addChild(switch3_rect);
    switch3_rect.off("click", listener3);
    hit3 = new createjs.Shape();
	hit3.graphics.beginFill("#000").drawRect(0,0, 80, 75);
    switch3_rect.hitArea = hit3;
    listener3 = switch3_rect.on("click", toggle_k2_3);

    stage.addChild(switchk2_1_l);
    switchk2_1_l.visible=true;

    stage.addChild(switchk2_1_r);
    switchk2_1_r.visible=false;
    

    stage.addChild(switchk2_2_l);
    switchk2_2_l.visible=false;
    stage.addChild(switchk2_2_r);
    switchk2_2_r.visible=true;
    

    stage.addChild(switchk2_3_r);
    stage.addChild(switchk2_3_l);
    
    switchk2_3_l.visible=false;
    
    switchk2_3_r.visible=true;

    stage.addChild(oudet);
    //stage.addChild(fasi4);
    stage.addChild(fasi1);
    stage.addChild(fasi5);
    stage.addChild(fasi6);
    stage.addChild(fasi7);
    stage.addChild(fasi8);
    stage.addChild(fasi9);

    stage.addChild(electronk2_1);
    stage.addChild(electronk2_2);
    stage.addChild(electronk2_3);

    for (i=24;i<=30;i++){
        stage.addChild(window['electronk2_'+i]);
        window['electronk2_'+i].visible=false;
    }

    for (i=32;i<=35;i++){
        stage.addChild(window['electronk2_'+i]);
        window['electronk2_'+i].visible=false;
    }



    for (i=36;i<=61;i++){
        stage.addChild(window['electron'+i]);
        window['electron'+i].visible=false;
    }

    electronk2_1.visible=false;
    electronk2_2.visible=false;
    electronk2_3.visible=false;

    $("#kyk2_switch1").prop("checked", false);
    $("#kyk2_switch2").prop("checked", true);
    $("#kyk2_switch3").prop("checked", true);

    $("#oer_results p").text("Η λάμπα δεν ανάβει");


    stage.update();

}


function show_k2() {
    stage.removeAllChildren();
    //stage.addChild(glow1);
    stage.addChild(lamp_on2);
    stage.addChild(lamp_off2);
    stage.addChild(oudet_text);
    stage.addChild(fasi_text);
    stage.addChild(switch1_text);
    stage.addChild(switch2_text);
    stage.addChild(switch3_text);
    stage.addChild(switch1_rect);
    switch1_rect.off("click", listener1);

    hit1 = new createjs.Shape();
	hit1.graphics.beginFill("#000").drawRect(59.5,370.5,70,70);
	switch1_rect.hitArea = hit1;
    listener1 = switch1_rect.on("click", toggle_k2_1);

    stage.addChild(switch2_rect);
    switch2_rect.off("click", listener2);
    hit2 = new createjs.Shape();
	hit2.graphics.beginFill("#000").drawRect(630.5, 370.5, 70, 70);
    switch2_rect.hitArea = hit2;
    listener2 = switch2_rect.on("click", toggle_k2_2);

    stage.addChild(switch3_rect);
    switch3_rect.off("click", listener3);
    hit3 = new createjs.Shape();
	hit3.graphics.beginFill("#000").drawRect(0,0, 80, 75);
    switch3_rect.hitArea = hit3;
    listener3 = switch3_rect.on("click", toggle_k2_3);

    stage.addChild(switchk2_1_l);
    stage.addChild(switchk2_1_r);
    

    stage.addChild(switchk2_2_l);
    stage.addChild(switchk2_2_r);

    stage.addChild(switchk2_3_l);
    stage.addChild(switchk2_3_r);
    

    stage.addChild(oudet);
    stage.addChild(fasi9);
    stage.addChild(fasi1);
    stage.addChild(fasi5);
    stage.addChild(fasi6);
    stage.addChild(fasi7);
    stage.addChild(fasi8);

    


    stage.addChild(electronk2_1);
    stage.addChild(electronk2_2);
    stage.addChild(electronk2_3);

    for (i=24;i<=30;i++){
        stage.addChild(window['electronk2_'+i]);
    }

    for (i=32;i<=35;i++){
        stage.addChild(window['electronk2_'+i]);
    }



    for (i=36;i<=61;i++){
        stage.addChild(window['electron'+i]);
    }
    
    $("#kyk2_switch1").prop("checked", switchk2_1_r.visible);
    $("#kyk2_switch2").prop("checked", switchk2_2_r.visible);
    $("#kyk2_switch3").prop("checked", switchk2_3_r.visible);

    if(kyk2_res){
        $("#oer_results p").text("Η λάμπα ανάβει!");
    }
    else{
        $("#oer_results p").text("Η λάμπα δεν ανάβει");

    }
    

    stage.update();
}


function toggle_kyklwma(){
    console.log("new!!!");
    if($("#kyk_switch").prop("checked")){

        kyklwma2_click();

    }
    else{
        kyklwma1_click();
    }

}


function toggle_k1_1() {
    //console.log("clicked");
    if (!switchk1_1_r.visible) {

        switchk1_1_r.visible = true;
        switchk1_1_l.visible = false;
        $("#kyk1_switch1").prop("checked", true);

    }
    else {
        switchk1_1_r.visible = false;
        switchk1_1_l.visible = true;
        $("#kyk1_switch1").prop("checked", false);

    }
    handle_k1_lamp();
    stage.update();
};



function kyk1_switch1_changed() {
    if ($("#kyk1_switch1").prop("checked")) {
        switchk1_1_r.visible = true;
        switchk1_1_l.visible = false;
        
        stage.update();

    }
    else {
        switchk1_1_r.visible = false;
        switchk1_1_l.visible = true;
        stage.update();

    }
    handle_k1_lamp();

}

function kyk1_switch2_changed() {
    if ($("#kyk1_switch2").prop("checked")) {
        switchk1_2_r.visible = true;
        switchk1_2_l.visible = false;
        //kyk1_2_right = true;
        stage.update();

    }
    else {
        switchk1_2_r.visible = false;
        switchk1_2_l.visible = true;
        //kyk1_2_right = false;
        stage.update();

    }


    handle_k1_lamp();

}


function kyk2_switch1_changed() {
    k2_start=false;
    if ($("#kyk2_switch1").prop("checked")) {
        switchk2_1_r.visible = true;
        switchk2_1_l.visible = false;
        //kyk1_2_right = true;
        stage.update();

    }
    else {
        switchk2_1_r.visible = false;
        switchk2_1_l.visible = true;
        //kyk1_2_right = false;
        stage.update();

    }


    handle_k2_lamp();

}

function kyk2_switch2_changed() {
    k2_start=false;

    if ($("#kyk2_switch2").prop("checked")) {
        switchk2_2_r.visible = true;
        switchk2_2_l.visible = false;
        //kyk1_2_right = true;
        stage.update();

    }
    else {
        switchk2_2_r.visible = false;
        switchk2_2_l.visible = true;
        //kyk1_2_right = false;
        stage.update();

    }


    handle_k2_lamp();

}

function kyk2_switch3_changed() {

    k2_start=false;
    if ($("#kyk2_switch3").prop("checked")) {
        switchk2_3_r.visible = true;
        switchk2_3_l.visible = false;
        //kyk1_2_right = true;
        stage.update();

    }
    else {
        switchk2_3_r.visible = false;
        switchk2_3_l.visible = true;
        //kyk1_2_right = false;
        stage.update();

    }


    handle_k2_lamp();

}



function toggle_k1_2() {
    console.log("toggle_k1_2 executed");
    if (!switchk1_2_r.visible) {
        console.log("goes right");

        switchk1_2_r.visible = true;
        switchk1_2_l.visible = false;
        $("#kyk1_switch2").prop("checked", true);

    }
    else {
        console.log("goes left");
        switchk1_2_r.visible = false;
        switchk1_2_l.visible = true;
        $("#kyk1_switch2").prop("checked", false);


    }
    handle_k1_lamp();

    stage.update();
};

function toggle_k2_2() {
    //console.log("inside toggle_k2_2")
    k2_start=false;
    if (!switchk2_2_r.visible) {

        switchk2_2_r.visible = true;
        switchk2_2_l.visible = false;
        $("#kyk2_switch2").prop("checked", true);

    }
    else {
        switchk2_2_r.visible = false;
        switchk2_2_l.visible = true;
        $("#kyk2_switch2").prop("checked", false);


    }
    handle_k2_lamp();

    stage.update();
};


function toggle_k2_3() {
    //console.log("inside toggle_k2_3")
    k2_start=false;
    if (!switchk2_3_r.visible) {

        switchk2_3_r.visible = true;
        switchk2_3_l.visible = false;
        $("#kyk2_switch3").prop("checked", true);

    }
    else {
        switchk2_3_r.visible = false;
        switchk2_3_l.visible = true;
        $("#kyk2_switch3").prop("checked", false);


    }
    handle_k2_lamp();

    stage.update();
};





function toggle_k2_1() {
    //console.log("inside toggle_k2_1");
    k2_start=false;
    if (!switchk2_1_r.visible) {

        switchk2_1_r.visible = true;
        switchk2_1_l.visible = false;
        $("#kyk2_switch1").prop("checked", true);

    }
    else {
        switchk2_1_r.visible = false;
        switchk2_1_l.visible = true;
        $("#kyk2_switch1").prop("checked", false);


    }
    handle_k2_lamp();

    stage.update();
};






function handle_k1_lamp() {

    if ((switchk1_1_l.visible && switchk1_2_r.visible) || (switchk1_1_r.visible && switchk1_2_l.visible)) {

        console.log("hi?");


        lamp_on1.visible = true;
        lamp_off1.visible = false;
        kyk1_res=true;
        $("#oer_results p").text("Η λάμπα ανάβει!");
        if(switchk1_1_l.visible){
            for (i=1;i<=14;i++){
                window['electron'+i].visible=true;
            }
            for (i=24;i<=30;i++){
                window['electron'+i].visible=true;
            }

            for (i=32;i<=35;i++){
                window['electron'+i].visible=true;
            }


        }
        else{
            for (i=1;i<=3;i++){
                window['electron'+i].visible=true;
            }
            for (i=15;i<=30;i++){
                window['electron'+i].visible=true;
            }

            for (i=32;i<=35;i++){
                window['electron'+i].visible=true;
            }

        }
        
    }
    else {
        console.log("hi??");
        lamp_on1.visible = false;
        lamp_off1.visible = true;
        kyk1_res=false;
        $("#oer_results p").text("Η λάμπα δεν ανάβει");
        for (i=1;i<=30;i++){
            window['electron'+i].visible=false;
        }

        for (i=32;i<=35;i++){
            window['electron'+i].visible=false;
        }

        stage.update();


    }

    stage.update();

}

function handle_k2_lamp() {
    //var fasi9=new createjs.Shape();
    if ((switchk2_1_l.visible && switchk2_3_l.visible && switchk2_2_r.visible) || (switchk2_1_l.visible && switchk2_3_r.visible && switchk2_2_l.visible) || (switchk2_1_r.visible && switchk2_3_r.visible && switchk2_2_r.visible) ||  (switchk2_1_r.visible && switchk2_3_l.visible && switchk2_2_l.visible) ){

        lamp_on2.visible = true;
        lamp_off2.visible = false;
        
        $("#oer_results p").text("Η λάμπα ανάβει!");
        kyk2_res=true;


    }
    else{
        lamp_on2.visible = false;
        lamp_off2.visible = true;
        $("#oer_results p").text("Η λάμπα δεν ανάβει");
        kyk2_res=false;

        electronk2_1.visible=false;
        electronk2_2.visible=false;
        electronk2_3.visible=false;

        for (i=24;i<=30;i++){
            window['electronk2_'+i].visible=false;
        }

        for (i=32;i<=35;i++){
            window['electronk2_'+i].visible=false;
        }
    
        for (i=36;i<=61;i++){
            window['electron'+i].visible=false;
        }
    }

    if (switchk2_1_l.visible && switchk2_3_l.visible && switchk2_2_r.visible){
        electronk2_1.visible=true;
        electronk2_2.visible=true;
        electronk2_3.visible=true;

        

        for (i=43;i<=49;i++){
            window['electron'+i].visible=true;
        }

        for (i=50;i<=56;i++){
            window['electron'+i].visible=true;
        }

        for (i=24;i<=30;i++){
            window['electronk2_'+i].visible=true;
        }

        for (i=32;i<=35;i++){
            window['electronk2_'+i].visible=true;
        }



    }

    if((switchk2_1_l.visible && switchk2_3_r.visible && switchk2_2_l.visible)){

        electronk2_1.visible=true;
        electronk2_2.visible=true;
        electronk2_3.visible=true;

        

        for (i=43;i<=49;i++){
            window['electron'+i].visible=true;
        }

        for (i=57;i<=61;i++){
            window['electron'+i].visible=true;
        }

        for (i=24;i<=30;i++){
            window['electronk2_'+i].visible=true;
        }

        for (i=32;i<=35;i++){
            window['electronk2_'+i].visible=true;
        }

    }

    if(switchk2_1_r.visible && switchk2_3_r.visible && switchk2_2_r.visible){

        electronk2_1.visible=true;
        electronk2_2.visible=true;
        electronk2_3.visible=true;

        

        for (i=36;i<=42;i++){
            window['electron'+i].visible=true;
        }

        for (i=50;i<=56;i++){
            window['electron'+i].visible=true;
        }

        for (i=24;i<=30;i++){
            window['electronk2_'+i].visible=true;
        }

        for (i=32;i<=35;i++){
            window['electronk2_'+i].visible=true;
        }


    }

    if(switchk2_1_r.visible && switchk2_3_l.visible && switchk2_2_l.visible){

        electronk2_1.visible=true;
        electronk2_2.visible=true;
        electronk2_3.visible=true;

        

        for (i=36;i<=42;i++){
            window['electron'+i].visible=true;
        }

        for (i=57;i<=61;i++){
            window['electron'+i].visible=true;
        }

        for (i=24;i<=30;i++){
            window['electronk2_'+i].visible=true;
        }

        for (i=32;i<=35;i++){
            window['electronk2_'+i].visible=true;
        }
    }


    
    stage.update();
}
