//desktop
var isMobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i) != null;

var isiPad = navigator.userAgent.match(/iPad/i) != null;

var isiPhone = navigator.userAgent.match(/iPhone|iPod/i) != null;

//baidu
var isBaidu = navigator.userAgent.match('baidu') != null;

//QQ browser
var isQQ = navigator.userAgent.match('MQQBrowser') != null;

//Saferi Browser
var isFirefox = navigator.userAgent.match('FxiOS') != null;

// UC Browser
var isUC = navigator.userAgent.indexOf("UCBrowser") != -1;

// Chrome 1+
var isChrome = navigator.userAgent.match('CriOS') != null;

//xiaomi
var isXiaomi = navigator.userAgent.match('XiaoMi') != null;

var isLine = navigator.userAgent.match('Line') != null;

// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = navigator.userAgent.match('Safari') && !isBaidu && !isFirefox && !isQQ && !isChrome && !isUC && !isXiaomi;

var isAndroid = /android/i.test(navigator.userAgent || navigator.vendor || window.opera);

var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

// iPhone X、iPhone XS
var isIPhoneX = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 375 && window.screen.height === 812;
// iPhone XS Max
var isIPhoneXSMax = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 3 && window.screen.width === 414 && window.screen.height === 896;
// iPhone XR
var isIPhoneXR = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 414 && window.screen.height === 896;
// iPhone SE
var isIPhoneSE = /iphone/gi.test(window.navigator.userAgent) && window.devicePixelRatio && window.devicePixelRatio === 2 && window.screen.width === 320 && window.screen.height === 568;

/**解析網址參數 */
var requests = GetUrlRequest();

/**放置LOGO圖 */
var a = SetLogo();

function preventDefault(e) {
    e = e || window.event;
    if (e.preventDefault) {
        e.preventDefault();
    }
    e.returnValue = false;
}

function nopreventDefault(e) {
    e = e || window.event;
    if (e.preventDefault) {
        e.preventDefault();
    }
    e.returnValue = true;
}

function requestFullScreen() {
    let el = window.document.body;
    let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen;
    if (typeof rfs != "undefined" && rfs) {
        rfs.call(el);
    }
}

function cancelFullScreen() {
    let el = window.document;
    let rfs = el.exitFullscreen || el.mozCancelFullScreen || el.webkitExitFullscreen || el.msExitFullscreen;
    if (typeof rfs != "undefined" && rfs) {
        rfs.call(el);
    }
}

function isiPhoneX() {
    var t = window.devicePixelRatio || 1,
        e = {
            width: window.screen.width * t,
            height: window.screen.height * t
        };
    return iOS && 1125 === e.width && 2436 === e.height;
}

function SetLogo() {
    var lan = "zh-ch";
    if (requests["language"]) {
        lan = requests["language"];
    }
    var splash = document.getElementById("splash");
    if (!requests["logo"]) {
        splash.className = "default" + " L " + lan + " L pos";
    } else {
        splash.className = "default_" + requests["logo"] + " L pos";
    }
}

function GetUrlRequest() {
    var url = window.location.search;
    var theRequest = [];
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

function CheckWebPageBack() {
    if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) { // IE 
        if (history.length > 0) {
            window.history.go(-1);
        } else {
            window.close();
        }
    } else {
        //非IE瀏覽器 
        if (navigator.userAgent.indexOf('Firefox') >= 0 ||
            navigator.userAgent.indexOf('Opera') >= 0 ||
            navigator.userAgent.indexOf('Safari') >= 0 ||
            navigator.userAgent.indexOf('Chrome') >= 0 ||
            navigator.userAgent.indexOf('WebKit') >= 0) {
            if (window.history.length > 2) {
                window.history.go(-1);
            } else {
                window.close();
            }
        } else {
            //未知的瀏覽器 
            if (history.length > 1) {
                window.history.go(-1);
            } else {
                window.close();
            }
        }
    }
}

/**網頁的RESIZE判斷 */
/*window.onresize = function () {
    cc.view.setResizeCallback(this.checkGameCanvasWH());
}*/

/**複寫達到功能縮放到一定程度不再縮放(尚未使用此功能) */
function overrideSetDesignResolutionSize(width, height, resolutionPolicy) {
    if (!(width > 0 || height > 0)) {
        cc.logID(2200);
        return;
    }
    cc.view.setResolutionPolicy(resolutionPolicy);
    var policy = cc.view._resolutionPolicy;
    policy && policy.preApply(this);
    cc.sys.isMobile && cc.view._adjustViewportMeta();
    cc.view._orientationChanging = true;
    cc.view._resizing || cc.view._initFrameSize();
    if (!policy) {
        cc.logID(2201);
        return;
    }
    cc.view._originalDesignResolutionSize.width = cc.view._designResolutionSize.width = width;
    cc.view._originalDesignResolutionSize.height = cc.view._designResolutionSize.height = height;
    var result = policy.apply(cc.view, cc.view._designResolutionSize);
    if (result.scale && 2 === result.scale.length) {
        cc.view._scaleX = result.scale[0] <= 1 ? result.scale[0] : 1;
        cc.view._scaleY = result.scale[1] <= 1 ? result.scale[1] : 1;
    }
    if (result.viewport) {
        result.viewport.width = result.viewport.width <= width ? result.viewport.width : width;
        result.viewport.height = result.viewport.height <= height ? result.viewport.height : height;
        var vp = cc.view._viewportRect,
            vb = cc.view._visibleRect,
            rv = result.viewport;
        vp.x = rv.x;
        vp.y = rv.y;
        vp.width = rv.width;
        vp.height = rv.height;
        vb.x = 0;
        vb.y = 0;
        vb.width = rv.width / cc.view._scaleX;
        vb.height = rv.height / cc.view._scaleY;
    }
    policy.postApply(cc.view);
    cc.winSize.width = cc.view._visibleRect.width;
    cc.winSize.height = cc.view._visibleRect.height;
    cc.visibleRect && cc.visibleRect.init(cc.view._visibleRect);
    cc.renderer.updateCameraViewport();
    this.emit("design-resolution-changed");
}

/**改變CANVAS尺寸後要維持2的倍率.不然容易在縮放網頁時出現圖片接縫(顯示卡繪圖因素) */
function checkGameCanvasWH() {
    /*if (document.getElementById("GameCanvas").width > 1422) {
        document.getElementById("GameCanvas").width = 1422;
        document.getElementById("GameCanvas").style.width = "1422px";
    }*/
    if (document.getElementById("GameCanvas").width % 2 != 0) {
        document.getElementById("GameCanvas").width -= 1;
    }
    /*if (document.getElementById("GameCanvas").height > 800) {
        document.getElementById("GameCanvas").height = 800;
        document.getElementById("GameCanvas").style.height = "800px";
    }*/
    if (document.getElementById("GameCanvas").height % 2 != 0) {
        document.getElementById("GameCanvas").height -= 1;
    }
}

window.addEventListener('load', function () {
    if (!iOS && !isAndroid) {
        return;
    }
    if (isiPhoneX()) {
        var r = document.querySelector("meta[name=viewport]");
        r.setAttribute("content", "width=device-width,user-scalable=no,initial-scale=1, minimum-scale=1,maximum-scale=1,viewport-fit=cover")
    }
    //SetSwipe();
    if (isAndroid) {
        document.body.addEventListener('touchmove', function (e) {
            e.preventDefault();
        }, {
            passive: false
        });
        requestFullScreen();
    } else if (iOS) {
        /*if (window.addEventListener) {
            window.addEventListener('scroll', onScrollEvent, false);
        } else {
            window.attachEvent('onscroll', onScrollEvent);
        }
        onScrollEvent();*/
    }
    //alert(window.navigator.userAgent)
    if (isUC) {
        alert("亲爱的玩家您好：\n建议您使用chrome或safari浏览器，以获得最佳游戏体验。")
    }
}, false);

function SetSwipe() {
    var lan = "zh-ch";
    if (requests["language"]) {
        lan = requests["language"];
    }
    if (isAndroid) {
        var fullScreenClick = document.getElementById("FullScreenClick");
        fullScreenClick.className = "default " + lan + " pos";
    } else if (iOS) {
        var fullScreenSwipe = document.getElementById("FullScreenSwipe");
        fullScreenSwipe.className = "default " + lan + " pos";
    }
}

function isFullScreen() {
    if (isLine) {
        return true;
    }
    if (iOS) {
        /*if (isPortraitMode()) {
            return window.innerHeight > document.documentElement.clientHeight;
        } else {
            return window.innerHeight >= document.documentElement.clientHeight;
        }*/
    } else {
        var doc = window.document;
        var isFullScreen = !(!doc.fullscreenElement && !doc.mozFullScreenElement && !doc.webkitFullscreenElement && !doc.msFullscreenElement);
        return isFullScreen;
    }
}

function onScrollEvent() {
    if (isFullScreen()) {
        displayGame();
    } else {
        displayFinger();
    }
    setTimeout(function () {
        window.scrollTo(0, 0);
        window.scroll(0, 0);
        if (document.documentElement.scrollTop) {
            document.documentElement.scrollTop = 0;
        }
        if (document.body.scrollTop) {
            document.body.scrollTop = 0;
        }
    }, 200);
}

function displayFinger() {
    document.getElementById("ScreenRotation").style.display = "none";
    /*if (isAndroid) {
        document.getElementById("FullScreenClick").style.display = "block";
    } else if (iOS) {
        document.getElementById("FullScreenSwipe").style.display = "block";
    }*/
}

function displayGame() {
    document.getElementById("ScreenRotation").style.display = "none";
    if (isAndroid) {
        document.getElementById("FullScreenClick").style.display = "none";
    } else if (iOS) {
        document.getElementById("FullScreenSwipe").style.display = "none";
    }
}