function updateParamValue(pool, parName, value) {
    var evt = Event.GET_EVT_PARAM_SET_DO(pool + "." + parName + ".*", value);
    evt.PostToKernel();
}

function updateInstParamValue(pool, parName, inst, value) {
    var evt = Event.GET_EVT_PARAM_SET_DO(pool + "." + parName + "." + inst, value);
    evt.PostToKernel();
}

/*//old seat
function SetCarHWMotor(motorID, revolutions) {
    if (motorID < 10)
        motorID = "0" + motorID;
    byte0 = revolutions % 16;
    byte1 = Math.floor(revolutions / 16) % 16;
    byte2 = Math.floor(revolutions / 256) % 16;
    byte3 = Math.floor(revolutions / 4096) % 16;
    function byteToHexa(byte) {
        return "0123456789ABCDEF".charAt(byte);
    }
    updateParamValue("CarHW", "rawPortData", "$CAN,7EE," + motorID + ",14," + byteToHexa(byte1) + byteToHexa(byte0) + "," + byteToHexa(byte3) + byteToHexa(byte2));
}*/

function SetCarHWMotors(revolutionsArr) {
    /*//old seat
    for (var i = 0; i < revolutionsArr.length; ++i) {
        if (i > 0)
            sleep(100);
        SetCarHWMotor(i+1, revolutionsArr[i]);
    }*/
    //new seat
    motorsString = "";
    for (var i = 0; i < revolutionsArr.length; ++i) {
        motorsString += "," + revolutionsArr[i];
    }
    //LOG("CarHW.rawPortData $LOC,SET" + motorsString);
    updateParamValue("CarHW", "rawPortData", "$LOC,SET" + motorsString);
}

function CarHWSettings(car, scene, SwitchPositionsID) {
    /*//old seat
    this.carDefinitions = [
        //name, driver position, HW motors, back position
        ["sk26x", new Vector3(422, -333, 12), [282, 485, 307, 79, 347, 332]],
        ["sk37x", new Vector3(455.5, -350, 0), [393, 596, 178, 0, 362, 535]],
        ["sk48x", new Vector3(455, -360, 0), [393, 596, 178, 0, 362, 535]],
        ["sk326/0", new Vector3(488.5, -360, 10), [397, 0, 201, 191, 269, 4], new Vector3(1347, -365, -22)],
        ["sk326/1", new Vector3(488, -360, 39), [397, 0, 201, 191, 269, 4]],
        ["sk38x", new Vector3(455.5, -350, 0), [393, 596, 178, 0, 362, 535]],
    ];*/
    //new seat
    this.carDefinitions = [
        //name, driver position, HW motors, back left position
        ["sk26x", new Vector3(422, -333, 12), [1450, 600, 550, 2850, 5900, 4000]],//OK sedacka i volant
        ["sk27x", new Vector3(433, -340, -12), [1450, 800, 200, 3150, 5500, 2900], new Vector3(1225, -335, 1)],//OK sedacka, volant z SK26x
        ["sk37x", new Vector3(455.5, -350, -12), [1800, 700, 710, 2300, 4553, 2467]],//OK sedacka i volant
        ["sk370/3", new Vector3(431.5, -340, -12), [1450, 700, 800, 2850, 4400, 2700], new Vector3(1331, -320, 36)],//OK sedacka i volant
        ["sk370/4", new Vector3(434, -340, -12), [1450, 700, 800, 2850, 4400, 2700]],//OK sedacka i volant
        ["sk48x", new Vector3(455, -360, -12), [1800, 700, 710, 2150, 3300, 1300]],//OK sedacka i volant
        ["sk326/0", new Vector3(488.7, -360, -12), [1100, 320, 1300, 3300, 9600, 10500], new Vector3(1347, -365, -22)],//OK sedacka i volant
        ["sk326/1", new Vector3(488, -360, 19), [850, 320, 2150, 3590, 13800, 14500], new Vector3(1383, -365, 23.9)],//OK sedacka i volant
        ["sk326/1-7", new Vector3(488, -360, 19), [850, 320, 2150, 3590, 13800, 14500], new Vector3(1323, -365, 26), new Vector3(2255, -240, 92)],//OK sedacka i volant
        ["sk326/2", new Vector3(448, -360, 60), [850, 320, 2150, 3590, 13800, 14500], new Vector3(1450, -365, 40)],//OK sedacka i volant
        ["sk38x", new Vector3(455, -350, -12), [1800, 950, 710, 2300, 3500, 1700], new Vector3(1309, -355, -12)],//OK sedacka i volant
        ["sk316/3", new Vector3(455, -365, -12), [1800, 950, 710, 1982, 4107, 3073], new Vector3(1359, -355, 14)],
        ["VW37x", new Vector3(455.4, -350, -12), [1900, 820, 800, 2400, 5300, 2800]],
        ["dummy", new Vector3(489, -343, -12), [1400, 300, 1250, 3700, 16400, 11700]]
    ];
    this.currentCAR="";
    this.scene=scene;
    this.SwitchPositionsID=SwitchPositionsID;

    this.SetCar=function(car) {
        /*if (this.currentCar==car)
            return;*/
        this.currentCar=car;
        for (var i = 0; i < this.carDefinitions.length; ++i) {
            if (this.carDefinitions[i][0] == car) {
                this.scene.TranslateAbs(this.SwitchPositionsID, this.carDefinitions[i][1]);
                SetCarHWMotors(this.carDefinitions[i][2]);
            }
        }
    }
    this.SetCarDriverPosition=function(car, position) {
        /*if (this.currentCar==car)
            return;*/
        for (var i = 0; i < this.carDefinitions.length; ++i) {
            if (this.carDefinitions[i][0] == car) {
                switch (position) {
                    case 0://driver
                        //this.scene.TranslateAbs(this.SwitchPositionsID, this.carDefinitions[i][1]);
                        this.scene.SetTransformation(this.SwitchPositionsID, Matrix.Translation(this.carDefinitions[i][1]));
                        break;
                    case 1://co-driver
                        var tmpPos = new Vector3(this.carDefinitions[i][1].x, -this.carDefinitions[i][1].y, this.carDefinitions[i][1].z);
                        //this.scene.TranslateAbs(this.SwitchPositionsID, tmpPos);
                        this.scene.SetTransformation(this.SwitchPositionsID, Matrix.Translation(tmpPos));
                        break;
                    case 2://back left
                        //this.scene.TranslateAbs(this.SwitchPositionsID, this.carDefinitions[i][3]);
                        this.scene.SetTransformation(this.SwitchPositionsID, Matrix.Translation(this.carDefinitions[i][3]));
                        break;
                    case 3://back right
                        var tmpPos = new Vector3(this.carDefinitions[i][3].x, -this.carDefinitions[i][3].y, this.carDefinitions[i][3].z);
                        //this.scene.TranslateAbs(this.SwitchPositionsID, tmpPos);
                        this.scene.SetTransformation(this.SwitchPositionsID, Matrix.Translation(tmpPos));
                        break;
                    case 4://third left
                        //this.scene.TranslateAbs(this.SwitchPositionsID, this.carDefinitions[i][4]);
                        this.scene.SetTransformation(this.SwitchPositionsID, Matrix.Translation(this.carDefinitions[i][4]));
                        break;
                    case 5://third right
                        var tmpPos = new Vector3(this.carDefinitions[i][4].x, -this.carDefinitions[i][4].y, this.carDefinitions[i][4].z);
                        //this.scene.TranslateAbs(this.SwitchPositionsID, tmpPos);
                        this.scene.SetTransformation(this.SwitchPositionsID, Matrix.Translation(tmpPos));
                        break;
                    default:
                        LOGERROR("CarHWSettings::SetCarDriverPosition Unknown position: " + position);
                }
            }
        }
    }
    this.SetCar(car);//set the initial value
}

function SetTableRow(pool, parName, rowID, valuesArr) {
    for (var i = 0; i < valuesArr.length; ++i) {
        if (valuesArr[i] != undefined)
            updateParamValue(pool, parName, "" + i + "," + rowID + "," + valuesArr[i]);
    }
}
