var guide = {
    isJumping: false,
    addGuide: function(scene, text, position, userData) {
        if (typeof(scene) === "undefined") {
            console.error("当前场景尚未初始化，请检查程序是否加载完成");
            return false;
        }
        if (typeof(position) === "undefined") {
            console.error("请输入正确的导航坐标");
            return false;
        } else if (!position.isVector3) {
            console.error("请输入正确的导航坐标");
            return false;
        }
        var guideDiv = document.createElement('div');

        var guideText = document.createElement('div');
        guideText.className = 'guide_label';
        guideText.textContent = text;
        guideText.onclick = this.jumpTo;
        guideText.ontouchend = this.jumpTo;
        guideText.style.cursor = "pointer";
        //guideText.style.marginTop='10px';
        guideDiv.appendChild(guideText);
        var guideImg = document.createElement('img');
        guideDiv.className = "guide_wrapper";
        guideImg.src = "static/icons/guide.png";
        guideImg.className = "guide_img";
        guideImg.onclick = this.jumpTo;
        guideImg.ontouchend = this.jumpTo;
        guideImg.style.cursor = "pointer";
        guideDiv.appendChild(guideImg);
        guideDiv.userData = userData;
        var guide = new THREE.CSS2DObject(guideDiv);
        guide.position.copy(position);
        guide.userData = userData;
        guideDiv.threeTarget = guide;
        scene.add(guide);
    },


    getRotate: function() {
        //设置相机初始角度
        var upVector = new THREE.Vector3(0, 1, 0);
        var leftVector = new THREE.Vector3(-1, 0, 0);
        var rightVector = new THREE.Vector3(1, 0, 0);
        var forwardVector = new THREE.Vector3(0, 0, 1);

        //根据当前旋转四元素，把初始的方向向量进行跟随旋转
        var quat = window.that.camera.quaternion;
        var upAfterRotate = upVector.clone();
        var leftAfterRotate = leftVector.clone();
        var rightAfterRotate = rightVector.clone();
        var forwardAfterRotate = forwardVector.clone();
        upAfterRotate.applyQuaternion(quat);
        leftAfterRotate.applyQuaternion(quat);
        rightAfterRotate.applyQuaternion(quat);
        forwardAfterRotate.applyQuaternion(quat);

        var projectVector = upAfterRotate.clone();
        forwardAfterRotate.y = 0;
        forwardAfterRotate.normalize();
        projectVector.projectOnPlane(forwardAfterRotate);
        // projectVector.projectOnPlane(forwardVector);
        //console.log

        //因为向上向量，无论相机朝向，同时垂直与水平面的平面永远都在其上
        //所以用投影向量，和向上向量的夹角就是相机倾斜的角度
        var angle = projectVector.angleTo(upVector);

        //判断相机倾斜的方向，通过上方向的顶点到左或右的距离，判断相机向左或右倾斜
        if (upVector.distanceTo(leftAfterRotate) < upVector.distanceTo(rightAfterRotate)) {
            angle = -1 * angle;
        }
        //所有sprite原来是跟随相机同时旋转的，为了使界面的元素保持和世界水平一致
        //因此相机倾斜的角度，就等于所有界面元素应该逆向旋转的角度
        return angle * 180 / Math.PI;
    },

    removeAllGuides: function() {
        for (var count = 0; count < window.that.scene.children.length;) {
            if (window.that.scene.children[count] instanceof window.that.CSS2DObject) {
                window.that.scene.remove(window.that.scene.children[count])
            } else count++;
        }
        if (window.that.sceneCopy) {
            for (var count = 0; count < window.that.sceneCopy.children.length;) {
                if (window.that.sceneCopy.children[count] instanceof window.that.CSS2DObject) {
                    window.that.sceneCopy.remove(window.that.sceneCopy.children[count])
                } else count++;
            }
        }



    },
    /**
     * 导航跳转
     */
    jumpTo: function(event) {
        var userData;
        guide.isJumping = true;

        if (event instanceof MouseEvent || event instanceof TouchEvent)
            userData = event.srcElement.parentElement.threeTarget.userData;
        else if (event instanceof window.that.CSS2DObject) {
            userData = event.element.threeTarget.userData;
        }
        // window.that.camera.lookAt(userData.position);
        // window.that.camera.lookAt(new window.THREE.Vector3(0,0,1))
        window.that.isReady = false;

        //同场景内相机跳转
        if (userData.type === "camera" || userData.type === "door") {
            if (window.that.allCenariosLoaded) { //所有场景已经加载完毕时候可以清除所有箭头
                guide.removeAllGuides();
            }
            window.that.sceneJumpMethods(userData.id, userData.position);
        }
    }
}