window.addEventListener("load", eventWindowLoaded, false);

var cancasmode = 0;

function tabcheck() {
    var introduce = document.getElementById('introduce');
    var ineh = document.getElementById('eucentricheightcontents');
    var inform = document.getElementById('form');
    var insingle = document.getElementById('simplephotecontents');
    var intomo = document.getElementById('tomography');
    var inmds = document.getElementById('mds');

    if (insingle.style.display == "") {
        canvasmode = 1;
        //canvasApp();
        console.log("canvasmode=" + canvasmode);
    }else if(intomo.style.display ==""){
        canvasmode = 2;
        canvastomo();
        console.log("canvasmode=" + canvasmode);
    }else  if(inmds.style.display ==""){
        canvasmode =3;
        canvasmds();
        console.log("canvasmode=" + canvasmode);
    }else{
        canvasmode = 0;
        console.log("canvasmode=" + canvasmode);
    }
}

function eventWindowLoaded() {
    //画面がロードされたら、canvasAppを呼び出す
  /*  if (canvasmode == 1) {
        //canvasApp();
    } else if (canvasmode == 2) {
        canvastomo();
    }
    */
   //canvasmds();
    //モード切り替えの初期formの色の設定
    document.getElementById("formmode").style.backgroundColor = '#e0ffff';
    document.getElementById("formmode2").style.backgroundColor = '#e0ffff';
    document.getElementById("formmode3").style.backgroundColor = '#e0ffff';
}
/*
 function canvasSupport(){
 return Modernizer.canvas;
 }
 */
//var movex = document.forms.movestage.movex.value;
//var movey = document.forms.movestage.movey.value;
/*
 function putTxt(){
 document.onkeydown = funcKey;
 }
 */

//キャンバスの初期値の設定
var movex = 1;
var movey = 1;
var movez = 0;
var movea = 0;
var mode = 0;
var moveformx=0;
var moveformy=0;
var moveformz=0;
var moveformas=0;
var moveformat=0;
var movingdistance =1;
var tiltmode = 1;

function gamepad() {
    if (navigator.getGamepads) {
        // ゲームパッドリストを取得する
        var gamepad_list = navigator.getGamepads();
        // 出力テスト
        console.log(gamepad_list);
    }
    console.log("function gamepad");
    window.addEventListener("gamepadconnected", function(e) {
        console.log("gamepad connected!");
    });

    if (window.GamepadEvent) {

        // ------------------------------------------------------------
        // ゲームパッドを接続すると実行されるイベント
        // ------------------------------------------------------------
        window.addEventListener("gamepadconnected", function(e) {
            console.log("ゲームパッドが接続された");
            console.log(e.gamepad);
        });

        // ------------------------------------------------------------
        // ゲームパッドの接続を解除すると実行されるイベント
        // ------------------------------------------------------------
        window.addEventListener("gamepaddisconnected", function(e) {
            console.log("ゲームパッドの接続が解除された");
            console.log(e.gamepad);
        });

    }
    /*
     setInterval(function(e){
     var gamepad = e.gamepad;
     if (gamepad.axes[0] < -0.5) {
     movex = parseInt(movex) + parseInt(10);
     console.log("left!");
     ////canvasApp();
     }
     if (gamepad.axes[0] > 0.5) {
     movex = parseInt(movex) - parseInt(10);
     //canvasApp();
     }
     if (gamepad.axes[1] < -0.5) {
     movey = parseInt(movey) - parseInt(10);
     //canvasApp();
     }
     if (gamepad.axes[1] > 0.5) {
     movey = parseInt(movey) + parseInt(10);
     //canvasApp();

     }
     },1000/60);
     */

    setInterval(function() {
        var str = "";
        // ゲームパッドリストを取得する
        var gamepad_list = navigator.getGamepads();
        // ゲームパッドリスト内のアイテム総数を取得する
        var num = gamepad_list.length;
        var i;
        for (i = 0; i < num; i++) {
            // Gamepad オブジェクトを取得
            var gamepad = gamepad_list[i];
            if (!gamepad)
                continue;
            // ゲームパッドのインデックス値
            str += "index: " + gamepad.index + "\n";
            // タイムスタンプ情報
            str += "timestamp: " + gamepad.timestamp + "\n";
            // ゲームパッドの識別名
            str += "id: \"" + gamepad.id + "\"\n";
            // ゲームパッドの物理的な接続状態
            str += "connected: " + gamepad.connected + "\n";
            // マッピングタイプ情報
            str += "mapping: \"" + gamepad.mapping + "\"\n";

            // ボタンリスト
            // ------------------------------------------------------------
            var buttons = gamepad.buttons;
            str += "buttons: {\n";
            var j;
            var n = buttons.length;
            for (j = 0; j < n; j++) {
                // GamepadButton オブジェクトを取得
                var button = buttons[j];
                str += "  \"" + j + "\": { ";
                // ボタン押下状態
                str += "pressed:" + button.pressed + " , ";
                // ボタン入力強度
                str += "value:" + button.value + " }\n";
            }
            str += "}\n";
            // 軸リスト
            // ------------------------------------------------------------
            if (mode == 2) {
                var axes = gamepad.axes;
                str += "axes: {\n";
                var j;
                var n = axes.length;
                for (j = 0; j < n; j++) {
                    if (j == 0) {
                        if (axes[j] < -0.5) {
                            movex = parseInt(movex) - parseInt(1);
                            console.log(movex);
                            console.log(axes[j]);
                            stagecontrol('stage',movex,movey,movez,movea);
                            emcondition('request');
                            if (canvasmode == 1) {
                                //canvasApp();
                            } else if (canvasmode == 2) {
                                canvastomo();
                            } else if (canvasmode == 3) {
                                canvasmds();
                            }
                        }
                        if (axes[j] > 0.5) {
                            movex = parseInt(movex) + parseInt(1);
                            stagecontrol('stage',movex,movey,movez,movea);
                            emcondition('request');
                            if (canvasmode == 1) {
                                //canvasApp();
                            } else if (canvasmode == 2) {
                                canvastomo();
                            } else if (canvasmode == 3){
                                canvasmds();
                            }
                        }
                    } else if (j == 1) {
                        if (axes[j] < -0.5) {
                            movey = parseInt(movey) + parseInt(1);
                            stagecontrol('stage',movex,movey,movez,movea);
                            emcondition('request');
                            if (canvasmode == 1) {
                                //canvasApp();
                            } else if (canvasmode == 2) {
                                canvastomo();
                            } else if (canvasmode ==3 ) {
                                canvasmds();
                            }
                        }
                        if (axes[j] > 0.5) {
                            movey = parseInt(movey) - parseInt(1);
                            stagecontrol('stage',movex,movey,movez,movea);
                            emcondition('request');
                            if (canvasmode == 1) {
                                //canvasApp();
                            } else if (canvasmode == 2) {
                                canvastomo();
                            } else if (canvasmode == 3){
                                canvasmds();
                            }
                        }
                    }
                }
            }
        }
    }, 1000 / 60);


    window.addEventListener("gamepaddisconnected", function(e) {
            console.log("gamepad disconnected");
        });
    window.addEventListener("gamepadconnected", function(e) {
        console.log("Gamepad connected at index %d: %s. %d buttons, %d axes.",
                e.gamepad.index, e.gamepad.id,
                e.gamepad.buttons.length, e.gamepad.axes.length);
    });

};


function canvasApp() {
    var theCanvas = document.getElementById("canvasOne");
    //canvasとして利用するhtmlのタグの場所を指定
    var context = theCanvas.getContext("2d");
    //2Dcontextの取得

    function drawScreen() {
        //実際に画面の描写を行っている部分
        context.fillStyle = "fffffaa";
        //描画画面の塗りつぶし、今回は黒
        context.fillRect(0, 0, 540, 605);
        //画面上に形を作る
        if (mode == 0) {
            //if(document.forms.movestage.movex.value!=moveformx){
            moveformx = document.forms.movestage.movex.value;
            movex=parseInt(moveformx);
            //}
            //if(document.forms.movestage.movey.value!=moveformy){
            moveformy = document.forms.movestage.movey.value;
            movey=parseInt(moveformy);

            moveformz = document.forms.moveza.movez.value;
            movez=parseInt(moveformz);


            stagecontrol('stage',movex,movey,movez,movea);
            emcondition('request');
            //}
               //画面上に入力した数値から位置を取得
        }
        /*
        var movez = document.forms.s2.movez.value;

        if (movez == 0) {
            movez = 1;
        }
        */
        var img = new Image();

        img.onload = function() {
            //イメージがロードするのを待つ必要があるのでonload
            //context.scale(movez*0.1,movez*0.1);
            //context.setTransform(movez*0.1,0,0,movez*0.1,0,0);
            //context.translate(540/2,605/2);
            context.drawImage(img, movex, movey);
            //イメージを描写、引数は画像のオブジェクト,左端のx,y
            document.getElementById("currentx").innerHTML = movex;
            document.getElementById("currenty").innerHTML = movey;
            console.log("x=" + movex);
            console.log("y=" + movey);
            var movingdistance = $("#movingdistance");
            console.log(movingdistance[0]);
        }

        //見る画像
        img.src = "./image/CC124crudeダイニン.jpg";

        //--ふちの設定--現在見えてない
        context.strokeStyle = "#000000";
        //線の色
        context.strokeRect(5, 5, 505, 610);
        //わくの輪郭を書く、引数は左端のx,y,幅,高さ
    }
    drawScreen();
}

function canvastomo() {
    var theCanvas = document.getElementById("canvastomo");
    //canvasとして利用するhtmlのタグの場所を指定
    var context = theCanvas.getContext("2d");
    //2Dcontextの取得

    function drawScreen() {
        //実際に画面の描写を行っている部分
        context.fillStyle = "fffffaa";
        //描画画面の塗りつぶし、今回は黒
        context.fillRect(0, 0, 540, 605);
        //画面上に形を作る
        if (mode == 0) {
            //if(document.forms.movestage.movex.value!=moveformx){
            moveformx = document.forms.movestagetomo.movex.value;
            movex=parseInt(moveformx);
            //}
            //if(document.forms.movestage.movey.value!=moveformy){
            moveformy = document.forms.movestagetomo.movey.value;
            movey=parseInt(moveformy);

            moveformz = document.forms.moveztomo.movez.value;
            movez=parseInt(moveformz);

            if (tiltmode == 1) {
                moveformas = document.forms.setting.standardnum.value;
                movea = parseInt(moveformas);
                stagecontrol('stage', movex, movey, movez, movea);
                emcondition('request');
            } else if (tiltmode == 2) {
                moveformat = document.forms.setting.tiltingnum.value;
                movea = parseInt(moveformat);
                stagecontrol('stage', movex, movey, movez, movea*(Math.cos(movea * (Math.PI / 180))));
                emcondition('request');
            }
            stagecontrol('stage',movex,movey,movez,movea);
            emcondition('request');
            //}
               //画面上に入力した数値から位置を取得
        }
        /*
        var movez = document.forms.s2.movez.value;

        if (movez == 0) {
            movez = 1;
        }
        */
        var img = new Image();

        img.onload = function() {
            //イメージがロードするのを待つ必要があるのでonload
            //context.scale(movez*0.1,movez*0.1);
            //context.setTransform(movez*0.1,0,0,movez*0.1,0,0);
            //context.translate(540/2,605/2);
            context.drawImage(img, movex, movey);
            //イメージを描写、引数は画像のオブジェクト,左端のx,y
            document.getElementById("currenttomox").innerHTML = movex;
            document.getElementById("currenttomoy").innerHTML = movey;
            console.log("x=" + movex);
            console.log("y=" + movey);
        }

        //見る画像
        img.src = "./image/axoF11-2 .jpg";

        //--ふちの設定--現在見えてない
        context.strokeStyle = "#000000";
        //線の色
        context.strokeRect(5, 5, 505, 610);
        //わくの輪郭を書く、引数は左端のx,y,幅,高さ
    }
    drawScreen();
}

function controlmodeselect(modename){
        document.getElementById("key").style.backgroundColor = 'transparent';
        document.getElementById("key2").style.backgroundColor = 'transparent';
        document.getElementById("key3").style.backgroundColor = 'transparent';
        document.getElementById("formmode").style.backgroundColor = 'transparent';
        document.getElementById("formmode2").style.backgroundColor = 'transparent';
        document.getElementById("formmode3").style.backgroundColor = 'transparent';
        document.getElementById("gamepad").style.backgroundColor = 'transparent';
        document.getElementById("gamepad2").style.backgroundColor = 'transparent';
        document.getElementById("gamepad3").style.backgroundColor = 'transparent';

        if(modename){
            document.getElementById(modename).style.backgroundColor = '#e0ffff';
        }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function tilt() {
     radio = document.getElementsByName('tilting');
     document.getElementById('angurar').innerHTML= document.forms.setting.tiltingnum.value;
    if (radio[0].checked) {
        tiltmode=1;
        document.getElementById('standard').style.display = "";
        document.getElementById('saxtorn').style.display = "none";
    } else if (radio[1].checked) {
        tiltmode=2;
        document.getElementById('standard').style.display = "none";
        document.getElementById('saxtorn').style.display = "";
    }
}

function canvasmds() {
    var theCanvas = document.getElementById("canvasmds");
    //canvasとして利用するhtmlのタグの場所を指定
    var context = theCanvas.getContext("2d");
    //2Dcontextの取得
    function drawScreen() {
        //実際に画面の描写を行っている部分
        context.fillStyle = "fffffaa";
        //描画画面の塗りつぶし、今回は黒
        context.fillRect(0, 0, 540, 605);
        //画面上に形を作る
        if (mode == 0) {
            //if(document.forms.movestage.movex.value!=moveformx){
            moveformx = document.forms.movestagemds.movex.value;
            movex=parseInt(moveformx);
            //}
            //if(document.forms.movestage.movey.value!=moveformy){
            moveformy = document.forms.movestagemds.movey.value;
            movey=parseInt(moveformy);

            moveformz = document.forms.movezmds.movez.value;
            movez=parseInt(moveformz);


            stagecontrol('stage',movex,movey,movez,movea);
            emcondition('request');
            //}
               //画面上に入力した数値から位置を取得
        }
        /*
        var movez = document.forms.s2.movez.value;

        if (movez == 0) {
            movez = 1;
        }
        */
        var img = new Image();

        img.onload = function() {
            //イメージがロードするのを待つ必要があるのでonload
            //context.scale(movez*0.1,movez*0.1);
            //context.setTransform(movez*0.1,0,0,movez*0.1,0,0);
            //context.translate(540/2,605/2);
            context.drawImage(img, movex, movey);
            //イメージを描写、引数は画像のオブジェクト,左端のx,y
            document.getElementById("currentmdsx").innerHTML = movex;
            document.getElementById("currentmdsy").innerHTML = movey;
            console.log("x=" + movex);
            console.log("y=" + movey);
        }

        //見る画像
        img.src = "./image/flower.jpg";

        //--ふちの設定--現在見えてない
        context.strokeStyle = "#000000";
        //線の色
        context.strokeRect(5, 5, 505, 610);
        //わくの輪郭を書く、引数は左端のx,y,幅,高さ
    }
    drawScreen();
}

function shiftplace() {
    mode = 4;
    movex = parseInt(movex) + parseInt(10);
    stagecontrol('stage', movex, movey, movez, movea);
    emcondition('request');
    canvasmds();
}

function replace() {
    mode = 4;
    movex = parseInt(movex) - parseInt(10);
    stagecontrol('stage', movex, movey, movez, movea);
    emcondition('request');
    canvasmds();

}

function canvasroot() {
    var theCanvas = document.getElementById("canvasroot");
    //canvasとして利用するhtmlのタグの場所を指定
    var context = theCanvas.getContext("2d");
    //2Dcontextの取得

    function drawScreen() {
        //実際に画面の描写を行っている部分
        context.fillStyle = "fffffaa";
        //描画画面の塗りつぶし、今回は黒
        context.fillRect(0, 0, 200, 200);
        //画面上に形を作る
       /* if (mode == 0) {
            //if(document.forms.movestage.movex.value!=moveformx){
            moveformx = document.forms.movestage.movex.value;
            movex=parseInt(moveformx);
            //}
            //if(document.forms.movestage.movey.value!=moveformy){
            moveformy = document.forms.movestage.movey.value;
            movey=parseInt(moveformy);

            moveformz = document.forms.moveza.movez.value;
            movez=parseInt(moveformz);


            stagecontrol('stage',movex,movey,movez,movea);
            emcondition('request');
            //}
               //画面上に入力した数値から位置を取得
        }*/
        /*
        var movez = document.forms.s2.movez.value;

        if (movez == 0) {
            movez = 1;
        }
        */
        var img = new Image();

        img.onload = function() {
            //イメージがロードするのを待つ必要があるのでonload
            //context.scale(movez*0.1,movez*0.1);
            //context.setTransform(movez*0.1,0,0,movez*0.1,0,0);
            //context.translate(540/2,605/2);
            context.drawImage(img, movex, movey);
            //イメージを描写、引数は画像のオブジェクト,左端のx,y
            document.getElementById("currentx").innerHTML = movex;
            document.getElementById("currenty").innerHTML = movey;
            console.log("x=" + movex);
            console.log("y=" + movey);

        }

        //見る画像
        img.src = "./image/ele.jpg";

        //--ふちの設定--現在見えてない
        context.strokeStyle = "#000000";
        //線の色
        context.strokeRect(5, 5, 505, 610);
        //わくの輪郭を書く、引数は左端のx,y,幅,高さ
    }
    drawScreen();
}
