var intervalID=-1;

function showDebugInfo(info){
	if(debug)document.getElementById("debug").innerHTML=info;
}

function showMouseCoords(info){
    $("#mouseCoords").html(info);
}

function initialise(){
    showAppTitle();
    if(!eventsInitialised){
        initialiseEvents();
    }
    initialiseCanvas();
    initialisePage();
}

function initialiseEvents(){
    $(document).on("keydown",function(e){
        if(e.shiftKey){
            shiftPressed=true;
        }
        else if(e.ctrlKey){
            ctrlPressed=true;
        }
        else if(e.altKey){
            altPressed=true;
        }
    });
    
    $(document).on("keyup",function(e){
        shiftPressed=false;
        ctrlPressed=false;
        altPressed=false;
    });

    $("#slideSpeed").bind("change keyup touchstart touchend",function(event,ui) {
        var maxSimSpeed=Number($(this).attr("max"))+1;
        simSpeed=maxSimSpeed-$(this).val();
        if($("#btnSimulateSolution").text()=="Παύση"){
            stopSimulation();
            startSimulation();
        }
    });
    
    $("#slideFontSize").bind("change keyup touchstart touchend",function(event,ui) {
        graphFontSize=$(this).val();
        setGraphFont();
        initialTheta-=dTheta;
        nextFrame();
    });

    $("#btnInitialise").on("click touchstart touchend",function(){
        exerciseMode=false;
        $("#btnOptions").prop("disabled",false);
        $("#showOptions").attr("href","#popupSpeed");
        initialisePage();
    });
    
    $("#btnTransition").on("click touchstart touchend",function(){
        transition();
    });
    
    $("#btnInitColors").on("click touchstart touchend",function(){
        initialiseColors();
    });
    
    $("#btnSimulateSolution").on("click touchstart touchend",function(){
        if($("#btnSimulateSolution").text()=="Προσομοίωση"){
            startSimulation();
        }
        else{
            stopSimulation();
        }
    });
    
    $("#btnPreviousFrame").on("click touchstart touchend",function(){
        initialTheta-=2*dTheta;
        nextFrame();
    });
    
    $("#btnNextFrame").on("click touchstart touchend",function(){
        nextFrame();
    });

    $("#boxGraph").bind("change keyup touchstart touchend",function(event,ui){
        initialiseColors();
        initialTheta-=dTheta;
        nextFrame();
    });
    
    $("#showGraphGrid").bind("change keyup touchstart touchend",function(event,ui){
        initialTheta-=dTheta;
        nextFrame();
    });

    $("#showCoordinates").bind("change keyup touchstart touchend",function(event,ui){
        initialTheta-=dTheta;
        nextFrame();
    });

    $("#showSymbols").bind("change keyup touchstart touchend",function(event,ui){
        showScaleSymbols=$("#showSymbols").is(":checked");
        initialTheta-=dTheta;
        nextFrame();
    });

    $("#showParticles").bind("change keyup touchstart touchend",function(event,ui){
        initialTheta-=dTheta;
        nextFrame();
    });

    $("#showVelocities").bind("change keyup touchstart touchend",function(event,ui){
        initialTheta-=dTheta;
        nextFrame();
    });
    
    $("#selectedParticle").bind("change keyup touchstart touchend",function(event,ui){
        if($("#selectedParticle").is(":checked")){
            setParticleSelectorMaxValue();
            $("#particleSelectorDIV").show();
        }
        else{
            $("#particleSelectorDIV").hide();
        }
        initialTheta-=dTheta;
        nextFrame();
    });
    
    $("#showIncidentWave").bind("change keyup touchstart touchend",function(event,ui){
        initialTheta-=dTheta;
        nextFrame();
    });

    $("#showReflectedWave").bind("change keyup touchstart touchend",function(event,ui){
        initialTheta-=dTheta;
        nextFrame();
    });
    
    $("#showStandingWave").bind("change keyup touchstart touchend",function(event,ui){
        initialTheta-=dTheta;
        nextFrame();
    });

    $("#requestConfirmations").bind("change keyup touchstart touchend",function(event,ui) {
        if($("#requestConfirmations:checked").val()=="reqConfirm"){
            requestConfirmationState=requestConfirmation;
        }
        else{
            requestConfirmationState=dontRequestConfirmation;
        }
    });
    
    eventsInitialised=true;
}

function startSimulation(){
    intervalID=setInterval(nextFrame,simSpeed);
    $("#btnSimulateSolution").text("Παύση");
    $("#btnPreviousFrame").attr("disabled",true);
    $("#btnNextFrame").attr("disabled",true);
}

function stopSimulation(){
    clearInterval(intervalID);
    intervalID=-1;
    $("#btnSimulateSolution").text("Προσομοίωση");
    $("#btnPreviousFrame").attr("disabled",false);
    $("#btnNextFrame").attr("disabled",false);
}

function initialiseScene(){
    basicWidth=4;
}