/* ** This files handles the course's data function(s) ** */
// Animate

// Update stage will render next frame
createjs.Ticker.framerate = 30;
createjs.Ticker.addEventListener("tick", tickHandler);

// createjs.Ticker.addEventListener("tick", function);

function tickHandler(e) {
    stage.update();
}

// createjs.Ticker.addEventListener("tick", change_angle);
// createjs.Ticker.addEventListener("tick", change_length);

//Function Call

// Keep sliders and numbers in sync
$("#piesi_p").change(function() {
    $("#num_piesi_p").val($(this).val());
    calc();
});

$("#num_piesi_p").change(function() {
    $("#piesi_p").val($(this).val());
    calc();
});
$("#power_f").change(function() {
    $("#num_power_f").val($(this).val());
    calc();
});

$("#num_power_f").change(function() {
    $("#power_f").val($(this).val());
    calc();
});

$("#length").change(function() {
    $("#num_length").val($(this).val());
    calc();
});

$("#num_length").change(function() {
    $("#length").val($(this).val());
    calc();
});

$("#angle").change(function() {
    $("#num_angle").val($(this).val());
    calc();
});

$("#num_angle").change(function() {
    $("#angle").val($(this).val());
    calc();
});
//
function piesi_p_changed() {
    calc();
}
//
function power_f_changed() {
    calc();
}
//
function length_l_changed() {
    calc();
}
//
function angle_changed() {
    calc();
}

//Show Vehicle Value
function showVehicleVal() {
    var vehicleVal = document.getElementById("oxima").value;
    if (vehicleVal == 0.035) {
        document.getElementById("loadImage").src = "images/scooter.png";
    } else if (vehicleVal == 0.075) {

        document.getElementById("loadImage").src = "images/car.png";
    } else if (vehicleVal == 0.1) {

        document.getElementById("loadImage").src = "images/bus.png";
    } else {
        document.getElementById("loadImage").src = "";

    }
    document.getElementById("d_value").innerHTML = "D = " + vehicleVal + "m (" + vehicleVal * 1000 + "mm)";
    calc();
}


// Calculate M = F x d
function calc() {
    //Get Controls Values
    //Dinami p
    var power_f = 0; //= $("#num_power_f").val();
    //Piesi p
    var p = $("#num_piesi_p").val();
    var p_changed = $("#num_piesi_p").val();
    //Oxima
    var D = $("#oxima").val();
    //Mhkos L
    var length_l = $("#num_length").val();
    //Gwnia φ
    var angle = $("#num_angle").val();
    var angleCos = $("#num_angle").val();
    //Mathimatikes Sxeseis
    var fg_value = $("#fg_value");
    //Moxlovraxionas
    var distance_d;
    //Texts
    var moxlobraxionas = $("#moxlobraxionas");
    var ropi = $("#ropi");
    var f_Txt = $("#fg");
    //Epifania Emvolou
    var A;
    //π =3,14
    var Pi = Math.PI;
    Pi = Pi.toFixed(2);
    //Change Dimensions on thalamos kausis

    //Check that the boxes (sliders) have value greater than zero
    if (length_l < 0.1 && length_l != 0 || length_l > 1 && length_l != 0) {
        alert("To Μήκος μπορεί να πάρει τιμές από 0,1 m έως 1 m");
    } else if (angle < 5 && angle != 0 || angle > 15 && angle != 0) {
        alert("Η Γωνία μπορεί να πάρει τιμές από 5 μοίρες έως 15 μοίρες");
    } else if (angle >= 5 || length_l >= 0.1) {
        //remove power check from if and put it in in the calculation as an if statement since it is being used there.
        moveShapes(angle, length_l); //length_l


    }
    getD(D);

    // Converts from degrees to radians.
    Math.radians = function(degrees) {
        return degrees * Math.PI / 180;
    };

    //Calculations
    //Get Angle
    angle = Math.radians(angle);
    angleCos = Math.radians(angleCos);
    //Hmitono
    angle = Math.sin(angle).toFixed(6);

    //Sinimitono
    cosAngle = Math.cos(angleCos).toFixed(6);

    //Calc d
    distance_d = length_l * angle;

    //Show Forces Arrow
    forcesOpacity(p);

    //A = epifaneia emvolou
    A = (Pi * Math.pow(D, 2) / 4).toFixed(5);
    //Convert p from bar to N/m^2
    p = Math.round(p * 100000);

    if (D) {

        power_f = power_f = p * A;
        power_f = Math.ceil(power_f);
    }
    //Calculate M = ROPI
    ropi_m = (power_f / cosAngle) * distance_d;

    //Dinami F
    var force = power_f / cosAngle;

    //Get 2 decimicals
    ropi_m = ropi_m.toFixed(2);
    distance_d = distance_d.toFixed(5);
    force = force.toFixed(2);

    //Make it String
    ropi_m = ropi_m.toString();
    distance_d = distance_d.toString();
    force = force.toString();

    //Replace dot character with comma
    ropi_m = ropi_m.replace(".", ",");
    distance_d = distance_d.replace(".", ",");
    ropi_m = ropi_m.replace(".", ",");
    force = force.replace(".", ",");


    //Results Value Boxes
    moxlobraxionas.html('d = ' + distance_d + ' m ');
    ropi.html('M = ' + ropi_m + " N &sdot; m");
    f_Txt.html('F = ' + force + ' N');

    //Result on Control Section
    fg_value.html('F<sub>G</sub> = ' + power_f + ' N');
    //fg_value.css('color', '#990000');

    return true;
}