var canvasCenterX=0;
var canvasCenterY=0;

var surface;
var surfaceLeft;
var surfaceTop;
var surfaceLength;
var surfaceHeight;

var testBody;
var testBodyLeft=0;
var testBodyTop=400;
var testBodyRadius=4;

var forceStartMovement=true;
var bodyColor=200;
//var solutionVisible=false;

var refreshingSlider;

//Παράμετροι γραφήματος
var graphBorder=10;
var graphLeft;
var graphTop;
var graphHeight=0;
var graphWidth=0;
var graphGridStep=0;
var pixelsPerGridStep=25;
var graphHovering=false;
var graphPanning=false;
var graphPanX=-1;
var graphPanY=-1;
var dyGraphRange=0;

//Παράμετροι κίνησης
var xo;
var vo;
var a;
var tMin=0;
var tMax=100;
var tInit=0;
var tFinal=0;
var xMin=0;
var xMax=0;
var xRange=100;

//Παράμετροι απεικόνισης
var tAlpha=0;
var tBeta=0;
var xAlpha=0;
var xBeta=0;
var terrainAlpha;
var terrainBeta;

//Παράμετροι animation
var tVal;
var tValXscr;
var tValXstep;
var tValYscr;
var tValYstep;
var xVal;
var xValXscr;
var xValXstep;
var xValYscr;
var xValYstep;
var tValDestinationX;
var tValDestinationY;
var xValDestinationX;
var xValDestinationY;
var animatedScalePeriod=2;
var animationRunning=false;

//Παράμετροι προσομοίωσης
var noOfTimeSlots=10;//25;
var currentTimeSlot=1;
var currentTime=0;
var timeStep=1/noOfTimeSlots;
var currentPosition=0;

function initialisePage(){
    $("#zoomFactor").val(1);
    $("#unitTitle").html(unitTitle[currentPage]);
    objectsDispose();
    initialising=true;
    initialiseScene();
        
    canvasCenterX=canvasWidth/2;
    canvasCenterY=parseInt(canvasHeight/(2*gridStep))*gridStep;
    dyGraphRange=0;
    
    initialiseBodies();
    updateTestBodyInitialPosition();
    
    initialising=false;
    handlePreviousNextButtons();
    drawScene();
}


function objectsDispose(){
    linkedVecs=[];
    bodies=[];
    clearBodies();
    clearLinkedVectors();
    clearFreeVectors();
}

function initialiseSurface(){
    var orientation=0;
    surfaceLength=canvasWidth+4*gridStep;
    surfaceHeight=gridStep;
    surfaceLeft=-2*gridStep;
    surfaceTop=canvasHeight-surfaceHeight/2;
    var surfaceWeight=0;
    var maxSurfaceWeight=0;
    var surfaceFext=0;
    var maxSurfaceFext=0;
    var surfaceN=0;
    var surfaceT=0;
    surface=new body(surfaceLeft,surfaceTop,surfaceLength,surfaceHeight,surfaceWeight,maxSurfaceWeight,surfaceFext,maxSurfaceFext,surfaceN,surfaceT,cntx,shapedAsRectangle,!bodySizeable,bodyCoordLocked,bodyCoordLocked);
    surface.visible=true;
    surface.bodyRed=0;
    surface.bodyGreen=180;
    surface.bodyBlue=0;
    surface.weightVctr.linked=true;
    surface.setOrientation(orientation);
    surface.showValue=false;
    surface.showOrientation=true;
    surface.movable=false;
    surface.rotateable=false;
    bodies.push(surface);
}

function initialiseBodies(){
    initialiseSurface();
    var testBodyRadius=gridStep;
    testBodyLeft=graphBorder-gridStep;
    testBodyTop=surface.top-3*gridStep/2;

    testBody=initialiseBody(testBodyLeft,testBodyTop,testBodyRadius);
	testBody.FextVctr.setOrientation(0);
	testBody.lockedX=false;
	testBody.lockedY=true;
	testBody.movable=true;
	testBody.bodyRed=255;
	testBody.bodyGreen=0;
	testBody.bodyBlue=0;
    bodies.push(testBody);
}

function initialiseBody(bodyLeft,bodyTop,bodySize){
    var Fext=0;
    var N=0;
    var T=0;
    var maxBodyWeight=50;
    var maxFexternal=50;
    Fext=0;
    N=0;
    T=0;
    bodyLength=2*bodySize;
    bodyHeight=2*bodySize;
    var newBody=new body(bodyLeft,bodyTop,bodyLength,bodyHeight,bodyWeight,maxBodyWeight,Fext,maxFexternal,N,T,cntx,!shapedAsRectangle,!bodySizeable,!bodyCoordLocked,bodyCoordLocked);
    newBody.visible=true;
    newBody.bodyRed=bodyColor;
    newBody.bodyGreen=bodyColor;
    newBody.bodyBlue=bodyColor;
    newBody.lockedX=false;
    newBody.lockedY=false;
    newBody.movable=false;
    newBody.FextVctr.lockedX=true;
    newBody.FextVctr.lockedY=true;
    newBody.FextVctr.sizeable=false;
    newBody.FextVctr.showValue=false;
    newBody.FextVctr.showXComponent=false;
    newBody.FextVctr.showYComponent=false;
    newBody.FextVctr.vecRed=0;
    newBody.FextVctr.vecGreen=0;
    newBody.FextVctr.vecBlue=0;
    newBody.weightVctr.visible=false;
    newBody.blackBorder=true;
    return newBody;
}

function updateTestBodyInitialPosition(){
    if(refreshingSlider){
        return;
    }
    
    var xOrigin=graphBorder-gridStep;
    var newPosition=terrainAlpha*(xOrigin+$("#testBodyInitialPos").val()*(canvasWidth-Math.max(testBody.length/2,graphBorder)-xOrigin)/100)+terrainBeta;
    testBody.move(newPosition,testBody.top);
    drawScene();
}

function mouseMoveDraw(shiftKeyPressed,ctrlKeyPressed,altKeyPressed){
    $("#canvas2D").css("cursor","default");
    //ΔΙΑΧΕΙΡΙΣΗ 'ΕΛΕΥΘΕΡΩΝ' ΔΙΑΝΥΣΜΑΤΩΝ
    if(movingVector){//μετακίνηση 'ελεύθερου' διανύσματος
        vecs[hoveringVectorID].move(movingOffsetX+mouseX,movingOffsetY+mouseY);
        calculateNs();
    }
    else if(sizingVector){//τροποποήση μεγέθους 'ελεύθερου' διανύσματος
        if(!altKeyPressed && vecs[hoveringVectorID].sizeable)vecs[hoveringVectorID].value=newVectorSize(vecs,hoveringVectorID);
        if(!ctrlKeyPressed && vecs[hoveringVectorID].rotatable)vecs[hoveringVectorID].setOrientation(newVectorOrientation(vecs,hoveringVectorID));
    }
    else if(vecs.length>0){//έλεγχος hovering σε 'ελεύθερα' διανύσματα
        hoveringVectorStart=false;
        hoveringVectorEnd=false;
        hoveringVectorID=-1;
        var i=0;
        
        //Η επόμενη συνθήκη 'χαλαρώνει' για να παίρνουν την κατάλληλη τιμή οι δείκτες mvHover και szHover ΟΛΩΝ των ελεύθερων διανυσμάτων
        //while(i<vecs.length && hoveringVectorID==-1){
        while(i<vecs.length){
            var dx1=Math.abs(mousePos.x-vecs[i].left);
            var dy1=Math.abs(mousePos.y-vecs[i].top);
            var vecEnd=vecs[i].getVectorEnd();
            var dx2=Math.abs(mousePos.x-vecEnd.endX);
            var dy2=Math.abs(mousePos.y-vecEnd.endY);
            if(vecs[i].movable() && dx1<=traceSize && dy1<=traceSize){
                vecs[i].mvHover=true;
                hoveringVectorStart=true;
                hoveringVectorID=i;
                $("#canvas2D").css("cursor","pointer");
            }
            else if(vecs[i].sizeable && dx2<=traceSize && dy2<=traceSize){
                vecs[i].szHover=true;
                hoveringVectorEnd=true;
                hoveringVectorID=i;
                $("#canvas2D").css("cursor","all-scroll");
            }
            else{
                vecs[i].szHover=false;
                vecs[i].mvHover=false;
            }
            i++;
        }
    }
    //ΔΙΑΧΕΙΡΙΣΗ 'ΔΕΣΜΙΩΝ' ΔΙΑΝΥΣΜΑΤΩΝ
    if(sizingLinkedVector){//τροποποήση μεγέθους 'δέσμιου' διανύσματος
        if(!altKeyPressed && linkedVecs[hoveringLinkedVectorID].sizeable){
            linkedVecs[hoveringLinkedVectorID].value=newVectorSize(linkedVecs,hoveringLinkedVectorID);
        }
        if(!ctrlKeyPressed && linkedVecs[hoveringLinkedVectorID].rotatable){
            linkedVecs[hoveringLinkedVectorID].setOrientation(newVectorOrientation(shiftKeyPressed,linkedVecs,hoveringLinkedVectorID));
        }
    }
    else if(linkedVecs.length>0){//έλεγχος hovering σε 'δέσμια' διανύσματα
        hoveringLinkedVectorEnd=false;
        hoveringLinkedVectorID=-1;
        var i=0;
        //Η επόμενη συνθήκη 'χαλαρώνει' για να παίρνουν την κατάλληλη τιμή οι δείκτες mvHover και szHover ΟΛΩΝ των δέσμιων διανυσμάτων
        //while(i<linkedVecs.length && hoveringLinkedVectorID==-1){
        while(i<linkedVecs.length){
            var vecEnd=linkedVecs[i].getVectorEnd();
            var dx2=Math.abs(mousePos.x-vecEnd.endX);
            var dy2=Math.abs(mousePos.y-vecEnd.endY);
            if(linkedVecs[i].sizeable && dx2<=traceSize && dy2<=traceSize){
                linkedVecs[i].szHover=true;
                hoveringLinkedVectorEnd=true;
                hoveringLinkedVectorID=i;
                $("#canvas2D").css("cursor","all-scroll");
            }
            else{
                linkedVecs[i].szHover=false;
            }
            i++;
        }
    }
    //ΔΙΑΧΕΙΡΙΣΗ ΣΩΜΑΤΩΝ
    if(movingBody){//μετακίνηση σώματος
        currentPosition=parseInt((mouseX-terrainBeta)/terrainAlpha);
        $("#testBodyInitialPos").val(currentPosition).slider("refresh");
    }
    else if(sizingBody){//τροποποήση μεγέθους σώματος
        if(bodies[hoveringBodyID].triangleSupport || bodies[hoveringBodyID].rectangleShape){//Για σώμα τριγωνικού / παραλληλόγραμμου σχήματος
            //Γενίκευση της τροποποίησης μεγέθους
            var newLength=Math.sqrt(Math.pow(mouseX-bodies[hoveringBodyID].left,2)+Math.pow(mouseY-bodies[hoveringBodyID].top,2));
            if(newLength>0){
                bodies[hoveringBodyID].setLength(newLength);
            }
        }
        else{//Για σώμα κυκλικού σχήματος αλλάζουμε την ακτίνα
            var xCenter=bodies[hoveringBodyID].left+bodies[hoveringBodyID].length/2;
            var newRadius=Math.sqrt(Math.pow(mouseX-(bodies[hoveringBodyID].left+bodies[hoveringBodyID].length/2),2)+Math.pow(mouseY-bodies[hoveringBodyID].top,2));

            bodies[hoveringBodyID].length=2*newRadius;
            bodies[hoveringBodyID].height=2*newRadius;
            bodies[hoveringBodyID].move(xCenter-newRadius,bodies[hoveringBodyID].top,true);
            updateTestBodyInitialPosition();
        }
        /**
         * ΓΙΑ ΜΕΛΛΟΝΤΙΚΗ ΥΛΟΠΟΙΗΣΗ (Αυξομείωση μεγέθους κατά κατεύθυνση x - y) 
        var newHeight=mouseY-bodies[hoveringBodyID].top;
        if(newHeight>0){
            bodies[hoveringBodyID].height=newHeight;
        }*/
    }
    else if(rotatingBody){//περιστροφή σώματος
        var dx=mousePos.x-bodies[hoveringBodyID].left;
        var dy=mousePos.y-bodies[hoveringBodyID].top;
        var newOrientation=Math.atan2(dy,dx);
        if(newOrientation>0 && newOrientation<183*Math.PI/180){
            newOrientation=183*Math.PI/180;
            newOrientDegrees=3;
        }
        if(newOrientation<0 && newOrientation>-93*Math.PI/180){
            newOrientation=-93*Math.PI/180;
        }
        var newOrientDegrees=parseInt(180+newOrientation*180/Math.PI) % 360;
        $("#angle").val(10*newOrientDegrees);
        $("#lblAngle").html(newOrientDegrees);
        bodies[hoveringBodyID].setOrientation(2*Math.PI-newOrientation);
    }
    else if(bodies.length>0){//έλεγχος hovering σε σώμα
        hoveringBodyStart=false;
        hoveringBodyEnd=false;
        hoveringRotationSwitch=false;
        hoveringBodyID=-1;
        var i=0;

        //Η επόμενη συνθήκη 'χαλαρώνει' για να παίρνουν την κατάλληλη τιμή οι δείκτες mvHover και szHover ΟΛΩΝ των σωμάτων
        //while(i<bodies.length && hoveringBodyID==-1){
        while(i<bodies.length){
            var dx1=Math.abs(mousePos.x-bodies[i].weightVctr.left);
            var dy1=Math.abs(mousePos.y-bodies[i].weightVctr.top);
            var bodyEnd=bodies[i].getBodyEnd();
            var dx2=Math.abs(mousePos.x-bodyEnd.endX);
            var dy2=Math.abs(mousePos.y-bodyEnd.endY);
            
            if(bodies[i].triangleSupport || bodies[i].rectangleShape){//Για σώμα τριγωνικού / παραλληλόγραμμου σχήματος
                dx2=Math.abs(mousePos.x-bodyEnd.endX);
                dy2=Math.abs(mousePos.y-bodyEnd.endY);
            }
            else{//Για σώμα κυκλικού σχήματος ελέγχουμε την ακτινική απόσταση από την περιφέρεια
                var distanceFromCenter=Math.sqrt(Math.pow(mousePos.x-bodyEnd.endX,2)+Math.pow(mousePos.y-bodyEnd.endY,2));
                dx2=Math.abs(distanceFromCenter-bodies[i].length/2);
                dy2=0;
            }            
            var dx3=Math.abs(mousePos.x-bodies[i].left);
            var dy3=Math.abs(mousePos.y-bodies[i].top);
            if(bodies[i].movable && dx1<=traceSize && dy1<=traceSize){
                bodies[i].mvHover=true;
                hoveringBodyStart=true;
                hoveringBodyID=i;
                $("#canvas2D").css("cursor","pointer");
            }
            else if(bodies[i].sizeable && dx2<=traceSize && dy2<=traceSize){
                bodies[i].szHover=true;
                hoveringBodyEnd=true;
                hoveringBodyID=i;
                if(!bodies[i].lockedX && !bodies[i].lockedY){
                    $("#canvas2D").css("cursor","all-scroll");
                }
                else if(!bodies[i].lockedX){
                    $("#canvas2D").css("cursor","ew-resize");
                }
                else{
                    $("#canvas2D").css("cursor","ns-resize");
                }
            }
            else if(bodies[i].rotateable && dx3<=traceSize && dy3<=traceSize){
                bodies[i].rotHover=true;
                hoveringRotationSwitch=true;
                hoveringBodyID=i;
                $("#canvas2D").css("cursor","pointer");
            }
            else{
                bodies[i].mvHover=false;
                bodies[i].szHover=false;
                bodies[i].rotHover=false;
            }
            i++;
        }
    }
    if(graphPanning){
        //Μελλοντική υλοποίηση
        //dyGraphRange=graphPanY-mousePos.y/4;
    }
    else if(mousePos.x>=graphLeft && mousePos.x<=graphLeft+graphWidth && mousePos.y>=graphTop && mousePos.y<=graphTop+graphHeight){
        graphHovering=true;
        //$("#canvas2D").css("cursor","all-scroll");
    }
    drawScene();
}

function mouseDownDraw(){
    if(hoveringVectorStart){
        movingVector=true;
        if($("#snapToGrid").is(":checked")){
            movingOffsetX=parseInt((vecs[hoveringVectorID].left-mouseX)/gridStep)*gridStep;
            movingOffsetY=parseInt((vecs[hoveringVectorID].top-mouseY)/gridStep)*gridStep;
        }
        else{
            movingOffsetX=vecs[hoveringVectorID].left-mouseX;
            movingOffsetY=vecs[hoveringVectorID].top-mouseY;
        }
    }
    else if(hoveringVectorEnd){
        sizingVector=true;
        var vecEnd=vecs[hoveringVectorID].getVectorEnd();
        if($("#snapToGrid").is(":checked")){
            sizingOffsetX=parseInt((vecEnd.endX-mouseX)/gridStep)*gridStep;
            sizingOffsetY=parseInt((vecEnd.endY-mouseX)/gridStep)*gridStep;
        }
        else{
            sizingOffsetX=vecEnd.endX-mouseX;
            sizingOffsetY=vecEnd.endY-mouseX;
        }
    }
    else if(hoveringLinkedVectorEnd){
        sizingLinkedVector=true;
        var vecEnd=linkedVecs[hoveringLinkedVectorID].getVectorEnd();
        if($("#snapToGrid").is(":checked")){
            sizingOffsetX=parseInt((vecEnd.endX-mouseX)/gridStep)*gridStep;
            sizingOffsetY=parseInt((vecEnd.endY-mouseX)/gridStep)*gridStep;
        }
        else{
            sizingOffsetX=vecEnd.endX-mouseX;
            sizingOffsetY=vecEnd.endY-mouseX;
        }
    }
    else if(hoveringBodyStart){
        movingBody=true;
        if($("#snapToGrid").is(":checked")){
            movingOffsetX=parseInt((bodies[hoveringBodyID].left-mouseX)/gridStep)*gridStep;
            movingOffsetY=parseInt((bodies[hoveringBodyID].top-mouseY)/gridStep)*gridStep;
        }
        else{
            movingOffsetX=bodies[hoveringBodyID].left-mouseX;
            movingOffsetY=bodies[hoveringBodyID].top-mouseY;
        }
    }
    else if(hoveringBodyEnd){
        sizingBody=true;
        var bodyEnd=bodies[hoveringBodyID].getBodyEnd();
        if($("#snapToGrid").is(":checked")){
            sizingOffsetX=parseInt((bodyEnd.endX-mouseX)/gridStep)*gridStep;
            sizingOffsetY=parseInt((bodyEnd.endY-mouseX)/gridStep)*gridStep;
        }
        else{
            sizingOffsetX=bodyEnd.endX-mouseX;
            sizingOffsetY=bodyEnd.endY-mouseX;
        }
    }
    else if(hoveringRotationSwitch){
        rotatingBody=true;
    }
    else if(graphHovering){
        graphHovering=false;
        graphPanning=true;
        graphPanX=mouseX;
        graphPanY=mouseY;
    }
}

function mouseUpDraw(){
    hoveringVectorStart=false;
    hoveringVectorEnd=false;
    hoveringVectorID=-1;
    movingVector=false;
    sizingVector=false;

    hoveringBodyStart=false;
    hoveringBodyEnd=false;
    hoveringRotationSwitch=false;
    hoveringBodyID=-1;
    movingBody=false;
    sizingBody=false;
    
    hoveringLinkedVectorEnd=false;
    hoveringLinkedVectorID=-1;
    sizingLinkedVector=false;
    
    graphPanning=false;
    
    if(exerciseMode){

        drawScene();
    }
}

function drawScene(){
    if(!initialising){
        if(testBody.left+testBody.length/2<0){
            testBody.move(-testBody.length/2,testBody.top);
        }
        else if(testBody.left+testBody.length/2>canvasWidth){
            testBody.move(canvasWidth-testBody.length/2,testBody.top);
        }
        //@@
        xMin=-50*parseFloat($("#zoomFactor").val())-dyGraphRange;
        xRange=2*50*parseFloat($("#zoomFactor").val());
        xMax=xMin+xRange;
        clearCanvas();
        //drawGrid();
        renderGraph();
        showTerrainScale();
        surface.render();
        var bodyScrX=terrainAlpha*currentPosition+terrainBeta-testBody.length/2;
        testBody.move(bodyScrX,testBody.top);
        testBody.render();
        showMovementInfo();
        updateFunctionX();
    }
}

function renderGraph(){
    var graphPaperColor=230;
    var xOrigin=0;
    var yOrigin=0;
    
    graphWidth=parseInt((canvasWidth-2*graphBorder)/pixelsPerGridStep)*pixelsPerGridStep;
    graphBorder=(canvasWidth-graphWidth)/2;
    graphGridStep=graphWidth/pixelsPerGridStep;
    graphHeight=parseInt(0.75*canvasHeight/pixelsPerGridStep)*pixelsPerGridStep;
    
    graphLeft=graphBorder;
    graphTop=graphBorder;

    defineMovementParameters();
    //ΣΧΕΔΙΑΣΗ ΧΙΛΙΟΣΤΟΜΕΤΡΙΚΟΥ ΧΑΡΤΙΟΥ ΜΕ ΠΕΡΙΓΡΑΜΜΑ
    if($("#boxGraph").is(":checked")){
        cntx.fillStyle="rgb("+graphPaperColor+","+graphPaperColor+","+graphPaperColor+")";
        cntx.fillRect(graphLeft,graphTop,graphWidth,graphHeight);
        cntx.strokeStyle="rgb(0,0,0)";
        cntx.beginPath();
        cntx.rect(graphLeft,graphTop,graphWidth,graphHeight);
        cntx.stroke();
    }
    else{
        cntx.fillStyle="rgb(250,250,210)"; //χρώμα φόντου καμβά
        cntx.fillRect(graphLeft,graphTop,graphWidth,graphHeight);
    }
    
    cntx.strokeStyle="rgb(0,0,235)";
    cntx.lineWidth=2;
    cntx.beginPath();
    //ΑΞΟΝΑΣ X
    cntx.moveTo(tAlpha*xOrigin+tBeta,graphTop);
    cntx.lineTo(tAlpha*xOrigin+tBeta,graphTop+graphHeight);
    //ΑΞΟΝΑΣ Y
    cntx.moveTo(graphLeft,xAlpha*yOrigin+xBeta);
    cntx.lineTo(graphLeft+graphWidth,xAlpha*yOrigin+xBeta);
    cntx.stroke();
    cntx.lineWidth=1;
    cntx.strokeStyle="rgb(0,0,150)";

    if($("#showGraphGrid").is(":checked")){
        //ΥΠΟΔΙΑΙΡΕΣΕΙΣ ΑΞΟΝΑ Χ
        cntx.setLineDash([1,2]);
        
        var i=tAlpha*xOrigin+tBeta;//1ο και 4ο τεταρτημόρια ορατά
        while(i<=graphLeft+graphWidth){
            cntx.beginPath();
            cntx.moveTo(i,graphTop);
            cntx.lineTo(i,graphTop+graphHeight);
            cntx.stroke();
            i+=graphGridStep;
        }
        var i=tAlpha*xOrigin+tBeta;//2ο και 3ο τεταρτημόρια ορατά
        while(i>graphLeft){
            cntx.beginPath();
            cntx.moveTo(i,graphTop);
            cntx.lineTo(i,graphTop+graphHeight);
            cntx.stroke();
            i-=graphGridStep;
        }

        //ΥΠΟΔΙΑΙΡΕΣΕΙΣ ΑΞΟΝΑ Y
        var j=xAlpha*yOrigin+xBeta;//3ο και 4ο τεταρτημόρια ορατά
        while(j<graphTop+graphHeight){
            cntx.beginPath();
            cntx.moveTo(graphLeft,j);
            cntx.lineTo(graphLeft+graphWidth,j);
            cntx.stroke();
            j+=graphGridStep;
        }
        j=xAlpha*yOrigin+xBeta;//1ο και 2ο τεταρτημόρια ορατά
        while(j>graphTop){
            cntx.beginPath();
            cntx.moveTo(graphLeft,j);
            cntx.lineTo(graphLeft+graphWidth,j);
            cntx.stroke();
            j-=graphGridStep;
        }

        cntx.setLineDash([]);
    }
    
    //ΓΡΑΦΙΚΗ ΠΑΡΑΣΤΑΣΗ
    cntx.lineWidth=2;
    cntx.strokeStyle="rgb(255,0,0)";
    cntx.beginPath();
    cntx.moveTo(tAlpha*tInit+tBeta,xAlpha*xo+xBeta);
    var x=0;
    var xScr=0;
    var yScr=0;
    var timeLimit=currentTime;
    if($("#showPointsCoords").is(":checked") && currentTime<tFinal){
        timeLimit=currentTime-animatedScalePeriod;
    }
    if($("#showGraphCurve").is(":checked")){
        for(var t=tInit;t<=timeLimit;t+=timeStep){
            x=xo+vo*(t-tInit)+0.5*a*Math.pow(t-tInit,2);
            xScr=tAlpha*(t-tInit)+tBeta;
            yScr=xAlpha*x+xBeta;
            cntx.lineTo(xScr,yScr);
        }
    }
    cntx.stroke();
    cntx.lineWidth=1;


    //ΕΠΙΛΕΓΜΕΝΕΣ ΤΙΜΕΣ ΕΠΙ ΤΩΝ ΑΞΟΝΩΝ
    //if($("#showAdditionalInfo").is(":checked")){
        cntx.setLineDash([1,1]);
        for(var tt=tInit;tt<=timeLimit;tt+=animatedScalePeriod){
            var ttScr=tAlpha*tt+tBeta;
            var xx=xo+vo*(tt-tInit)+0.5*a*Math.pow(tt-tInit,2);
            var xxScr=xAlpha*xx+xBeta;
            if($("#showGraphPoints").is(":checked")){
                cntx.fillStyle="rgb(255,0,0)";
                cntx.beginPath();
                cntx.arc(ttScr,xxScr,3,0,2*Math.PI,true);
                cntx.closePath();
                cntx.fill();
            }
            if($("#showPointsCoords").is(":checked")){
                //Γραμμές σύνδεσης
                cntx.strokeStyle="rgb(0,100,0)";
                cntx.beginPath();
                cntx.moveTo(ttScr,xBeta);
                cntx.lineTo(ttScr,xxScr);
                cntx.lineTo(tBeta,xxScr);
                cntx.stroke();
                //Συντεταγμένες
                writeOnCanvas(ttScr,xBeta-8,tt,graphFontSize,graphFont,"center","rgb(0,0,255)","","",true,0);
                writeOnCanvas(tBeta+2,xxScr,parseFloat(xx).toFixed(2),graphFontSize,graphFont,"left","rgb(0,0,255)","","",true,0);
            }
        }
        cntx.setLineDash([]);
    //}
    
    //Διαγραφή τμημάτων γραφικής παράστασης που βρίσκονται εκτός ορίων
    cntx.fillStyle="rgb(250,250,210)"; //χρώμα φόντου καμβά
    cntx.fillRect(0,0,canvasWidth,graphTop-1);//Πάνω
    cntx.fillRect(0,0,graphLeft-1,canvasHeight);//Αριστερά
    cntx.fillRect(0,graphTop+graphHeight+1,canvasWidth,canvasHeight-(graphTop+graphHeight+1));//Κάτω
    cntx.fillRect(graphLeft+graphWidth+1,0,canvasWidth-(graphLeft+graphWidth+1),canvasHeight);//Δεξιά

    //ΚΛΙΜΑΚΑ ΕΠΙ ΤΟΥ ΑΞΟΝΑ Χ
    if(xBeta>=graphTop && xBeta<=graphTop+graphHeight){
        var i=tAlpha*xOrigin+tBeta;//1ο και 4ο τεταρτημόρια ορατά
        var timeToShow=0;
        while(i<=graphLeft+graphWidth){
            timeToShow=parseInt((i-tBeta)/tAlpha);
            writeOnCanvas(i,xBeta+10,timeToShow,graphFontSize,graphFont,"center","rgb(0,0,0)","rgb(0,0,0)","",true,0);
            i+=graphGridStep;
        }
        i=tAlpha*xOrigin+tBeta;//2ο και 3ο τεταρτημόρια ορατά
        while(i>graphLeft){
            timeToShow=parseInt((i-tBeta)/tAlpha);
            writeOnCanvas(i,xBeta+10,timeToShow,graphFontSize,graphFont,"center","rgb(0,0,0)","rgb(0,0,0)","",true,0);
            i-=graphGridStep;
        }
    }
    
    //ΚΛΙΜΑΚΑ ΕΠΙ ΤΟΥ ΑΞΟΝΑ Y
    var j=xAlpha*yOrigin+xBeta;//3ο και 4ο τεταρτημόρια ορατά
    while(j>graphTop+graphHeight){
        j-=graphGridStep;
    }
    while(j<graphTop){
        j+=graphGridStep;
    }
    var positionToShow=0;
    while(j<=graphTop+graphHeight){
        positionToShow=parseInt((j-xBeta)/xAlpha);
        writeOnCanvas(tBeta-2,j+5,positionToShow,graphFontSize,graphFont,"right","rgb(0,0,0)","rgb(0,0,0)","",true,0);
        j+=graphGridStep;
    }
    j=xAlpha*yOrigin+xBeta;//1ο και 2ο τεταρτημόρια ορατά
    positionToShow=0;
    while(j>graphTop+graphHeight){
        j-=graphGridStep;
    }
    while(j<graphTop){
        j+=graphGridStep;
    }
    while(j>=graphTop){
        positionToShow=parseInt((j-xBeta)/xAlpha);
        writeOnCanvas(tBeta-2,j+5,positionToShow,graphFontSize,graphFont,"right","rgb(0,0,0)","rgb(0,0,0)","",true,0);
        j-=graphGridStep;
    }

    if($("#showAdditionalInfo").is(":checked")){

    }
}

function defineMovementParameters(){
    xo=parseFloat($("#testBodyInitialPos").val());
    vo=parseFloat($("#testBodyInitialVelocity").val());
    a=parseFloat($("#testBodyAcceleration").val());
    tFinal=tInit+tMax;
    defineGraphParameters();
}

function defineGraphParameters(){
    tAlpha=(canvasWidth-2*graphBorder)/(tFinal-tInit);
    tBeta=graphBorder-tAlpha*tInit;
    xAlpha=graphHeight/(xMin-xMax);
    xBeta=graphBorder-xAlpha*xMax;
}

function showTerrainScale(){
    terrainAlpha=canvasWidth/(xMax-xMin);
    terrainBeta=-terrainAlpha*xMin;
    var tickHeight=5;
    var yScr=surface.top-surface.height/2;
    for(var x=parseInt(xMin);x<=xMax;x++){
        var xScr=terrainAlpha*x+terrainBeta;
        cntx.beginPath();
        var scaleStep=parseInt((xMax-xMin)/20);
        if(x % scaleStep==0){
            cntx.strokeStyle="rgb(0,0,0)";
            cntx.moveTo(xScr,yScr-tickHeight);
        }
        else{
            cntx.strokeStyle="rgb(150,150,150)";
            cntx.moveTo(xScr,yScr);
        }
        cntx.lineTo(xScr,yScr+tickHeight);
        cntx.stroke();
        if(x % scaleStep==0){
            writeOnCanvas(xScr,surface.top+4," "+x+" ",graphFontSize,graphFont,"center","rgb(0,0,0)","rgb(0,0,0)","",true,0);
        }
    }
}

function showMovementInfo(){
    //if($("#showSpaceTimeInfo").is(":checked") && animationRunning){//Αν τρέχει η προσομοίωση...
    if($("#showSpaceTimeInfo").is(":checked")){
        var x=testBody.left+testBody.length/2;
        var y=testBody.top-testBody.height/2-8;
        writeOnCanvas(x,y," t="+currentTime.toFixed(2)+" s , x="+parseFloat(currentPosition).toFixed(2)+" m ",graphFontSize,graphFontSize+"px Verdana","center","rgb(0,0,0)","rgb(0,0,0)","rgb(255,255,0)",true,0);
    }
    
    //Animation τιμών
    if($("#showPointsCoords").is(":checked") && animationRunning){//Μόνο αν τρέχει η προσομοίωση...
        writeOnCanvas(tValXscr,tValYscr,tVal,graphFontSize,graphFontSize+"px Verdana","center","rgb(0,0,255)","","",true,0);    
        writeOnCanvas(xValXscr,xValYscr,xVal,graphFontSize,graphFontSize+"px Verdana","center","rgb(0,0,255)","","",true,0);
    }
}

function resetSimulation(){
    currentTime=-timeStep;
    currentTimeSlot=animatedScalePeriod*noOfTimeSlots;
    currentTime=tInit-timeStep;
    $("#btnSimulateSolution").prop("disabled",false);
}

function simulateMovement(){
    currentTime+=timeStep;
    defineMovementParameters();
    currentPosition=xo+vo*(currentTime-tInit)+0.5*a*Math.pow(currentTime-tInit,2);
    handlePreviousNextButtons();
    updateFunctionX();
    if(currentTimeSlot==animatedScalePeriod*noOfTimeSlots){//Νέα Περίοδος animation
        currentTimeSlot=1;
        //Υπολογισμοί για το animation της τιμής του t
        tValDestinationX=tAlpha*currentTime+tBeta;
        tValDestinationY=xBeta-10;
        tVal=currentTime.toFixed(0);
        tValXscr=parseInt(testBody.left+testBody.length/4);
        tValXstep=(tValDestinationX-tValXscr)/(animatedScalePeriod*noOfTimeSlots);
        tValYscr=testBody.top-testBody.height/2-12;
        tValYstep=(tValDestinationY-tValYscr)/(animatedScalePeriod*noOfTimeSlots);

        //Υπολογισμοί για το animation της τιμής του x
        xValDestinationX=tBeta+2;
        xValDestinationY=xAlpha*currentPosition+xBeta;
        xVal=currentPosition.toFixed(0);
        xValXscr=parseInt(testBody.left+3*testBody.length/4);
        xValXstep=(xValDestinationX-xValXscr)/(animatedScalePeriod*noOfTimeSlots);
        xValYscr=testBody.top-testBody.height/2-12;
        xValYstep=(xValDestinationY-xValYscr)/(animatedScalePeriod*noOfTimeSlots);
    }
    else{
        currentTimeSlot++;
        tValXscr+=tValXstep;
        tValYscr+=tValYstep;
        xValXscr+=xValXstep;
        xValYscr+=xValYstep;
    }
    if(currentTime>=tFinal){
        currentTime=tFinal;
        stopSimulation();
    }
    drawScene();
}

function setZoomFactor(){
    drawScene();
}

function updateFunctionX(){
    var signToUse="+";
    var html="Συνάρτηση θέσης:<br><math><mi>x</mi><mo>=</mo>";
    var withNumbers="<br><math><mi>x</mi><mo>=</mo>";
    if(xo!=0){
        html+="<mmultiscripts><mi>x</mi><mi>o</mi><mi></mi></mmultiscripts>";
        if(xo>0){
            signToUse="";
        }
        else{
            signToUse="-";
        }
        if(signToUse!=""){
            withNumbers+="<mo>"+signToUse+"</mo>";
        }
        withNumbers+="<mi>"+Math.abs(xo)+"</mi>";
    }
    if(vo!=0){
        if(vo>0){
            if(xo==0){
                signToUse="";
            }
            else{
                signToUse="+";
            }
        }
        else if(vo<0){
            signToUse="-";
        }
        if(xo!=0){
            html+="<mo>+</mo>";
        }
        html+="<mmultiscripts><mi>υ</mi><mi>o</mi><mi></mi></mmultiscripts><mmultiscripts><mi></mi><mi></mi><mi>.</mi></mmultiscripts><mi>t</mi>";
        if(signToUse!=""){
            withNumbers+="<mo>"+signToUse+"</mo>";
        }
        withNumbers+="<mi>"+Math.abs(vo)+"</mi><mmultiscripts><mi></mi><mi></mi><mi>.</mi></mmultiscripts><mi>t</mi>";
    }
    if(a!=0){
        if(a<0){
            signToUse="-";
        }
        else{//a>0 αυστηρά
            if(xo!=0 || vo!=0){
                signToUse="+";
            }
            else{
                signToUse="";
            }
        }
        if(xo!=0 || vo!=0){
            html+="<mo>+</mo>";
        }
        html+="</math><math><mfrac><mrow><mn>1</mn></mrow><mrow><mn>2</mn></mrow></mfrac></math><math><mmultiscripts><mi></mi><mi></mi><mi>.</mi></mmultiscripts><mi>α</mi><mmultiscripts><mi></mi><mi></mi><mi>.</mi></mmultiscripts><mmultiscripts><mi>t</mi><mi></mi><mi>2</mi></mmultiscripts></math>";
        if(signToUse!=""){
            withNumbers+="<mo>"+signToUse+"</mo>";
        }
        withNumbers+="</math><math><mfrac><mrow><mn>1</mn></mrow><mrow><mn>2</mn></mrow></mfrac></math><math><mmultiscripts><mi></mi><mi></mi><mi>.</mi></mmultiscripts><mn>"+Math.abs(a)+"</mn><mmultiscripts><mi></mi><mi></mi><mi>.</mi></mmultiscripts><mmultiscripts><mi>t</mi><mi></mi><mi>2</mi></mmultiscripts><mi>&nbsp&nbsp(S.I.)</mi></math>";
    }
    else{
        if(xo==0 && vo==0){
            html+="<mn>0</mn></math>";
            withNumbers="";
        }
        else{
            html+="</math>";
            withNumbers+="<mi>&nbsp&nbsp(S.I.)</mi></math>";
        }
    }
    $("#xft").html(html+withNumbers);
}