/*
 * File compatibility.js.
 * Theme Frontend script.
 */
(function() {
    if (typeof $ === 'undefined') $ = jQuery;
    $(function() {
        compatibility.init();
        console.log('Compatibility Script: loaded');
    } );
} )(jQuery);

var compatibility = new function() {

    this.init = function() {
        compatibility.check();
        setTimeout(function() {
            compatibility.show(); 
        }, 500);
    }

    // show or hide compatibility popup
    this.show = function() {
        // check if enabled
        if ($('.compatibility').length == 0) { 
            // console.log('stopped. disabled.');
            return false; 
        }
        // check preview
        var preview = JSON.parse(sessionStorage.getItem('compatibility_preview'));
        if (!preview) {
            // check compatibility
            if (!($("html").hasClass("not_compatible"))) {
                // console.log('stopped. compatible.');
                $('.compatibility').remove();
                return false;
            }
            // check data
            var data = JSON.parse(sessionStorage.getItem('compatibility'));
            if (data) {
                // console.log('stopped. data found.');
                $('.compatibility').remove();
                return false;
            } 
        }
        // show popup
        // console.log("open popup.");
        $('.compatibility').css('opacity', '1');
        $('.compatibility').css('pointer-events', 'all');
        $(document).on('click', ".compatibility_content .compatibility_button, .compatibility_content .vc_cbutton", compatibility.accept);
    }
    // hide popup
    this.accept = function() {
        // save data
        sessionStorage.setItem('compatibility', JSON.stringify( { "set": new Date().getTime() } ));
        // hide popup
        $('.compatibility').css('opacity', '0');
        $('.compatibility').css('pointer-events', 'none');
        setTimeout(function() {
            $('.compatibility').remove();
        }, 800);
    }

    // check browser
    this.check = function() {
        // console.log(navigator.userAgent);
        /*
        windows 7:
        firefox: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0
        chrome:  Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
        opera:   Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.64
        ie11:    Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
        windows 10:
        firefox: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0
        ie11:    Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)
        edge:    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
        OXS:
        safari:  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
        chrome:  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
        mobile: 
        simulated with chrome:
        iPhone:  Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1
        iPad:    Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1
        S5:      Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Mobile Safari/537.36
        simulated with firefox:
        iPhone:  Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/14D27 Safari/602.1
        iPad:    Mozilla/5.0 (iPad; CPU OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8L1 Safari/6533.18.5
        S5:      Mozilla/5.0 (Linux; Android 6.0.1; SM-G900V Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36
        */
        // dataset
        var dataset = {
            'system': '',
            'browser': '',
            'version': 0,
        };
        // system
        var deviceAgent = navigator.userAgent.toLowerCase();
        if (deviceAgent.match(/(iphone|ipod|ipad)/)) {
            //$("html").addClass("mobile");
            dataset.system = "ios";
        }
        else if (navigator.userAgent.search("Android") >= 0) {
            //$("html").addClass("mobile");
            dataset.system = "android";
        }
        else if (navigator.userAgent.search("Windows NT") >= 0) {
            dataset.system = "win";
        }
        else if (navigator.userAgent.search("Macintosh") >= 0) {
            dataset.system = "osx";
        }
        else {
            dataset.system = "unknown";
        }
        // browser
        if (navigator.userAgent.search("MSIE") >= 0 ||
            navigator.userAgent.search("Trident") >= 0) {
            dataset.browser = "ie";
            dataset.version = compatibility.get_version("MSIE");
        }
        else if (navigator.userAgent.search("Edge") >= 0) {
            dataset.browser = "edge";            
            dataset.version = compatibility.get_version("Edge");
        }
        else if (navigator.userAgent.search("OPR") >= 0) {
            dataset.browser = "opera";
            dataset.version = compatibility.get_version("OPR");
        }
        else if (navigator.userAgent.search("Opera") >= 0) {
            dataset.browser = "opera";
            dataset.version = compatibility.get_version("Opera");
        }
        else if (navigator.userAgent.search("Firefox") >= 0) {
            dataset.browser = "firefox";
            dataset.version = compatibility.get_version("Firefox");
        }
        else if (navigator.userAgent.search("Chrome") >= 0) {
            dataset.browser = "chrome";
            dataset.version = compatibility.get_version("Chrome");
        }
        else if (navigator.userAgent.search("Safari") >= 0) {
            dataset.browser = "safari";
            dataset.version = compatibility.get_version("Version");
        }
        else {
            dataset.browser = "unknown";
        }

        // filter browser version
        // https://caniuse.com/#search=var()
        // now support by css preprocessor
        // customizer still needs this
        // versions = { 
        //     "ie": 12,
        //     "edge": 15,
        //     "opera": 36,
        //     "firefox": 31,
        //     "chrome": 49,
        //     "safari": 10
        // }
        // https://caniuse.com/#feat=flexbox
        // basic support
        // versions = { 
        //     "ie": 12,
        //     "edge": 12,
        //     "opera": 17,
        //     "firefox": 28,
        //     "chrome": 29,
        //     "safari": 9
        // }
        // now with extended prefixed support by css preprocessor
        // versions = { 
        //     "ie": 11,
        //     "edge": 12,
        //     "opera": 12,
        //     "firefox": 2,
        //     "chrome": 4,
        //     "safari": 3
        // }
        // https://caniuse.com/#feat=calc
        // basic support with prefixes
        // versions = { 
        //     "ie": 10,
        //     "edge": 12,
        //     "opera": 15,
        //     "firefox": 4,
        //     "chrome": 19,
        //     "safari": 6
        // }
        // https://caniuse.com/#feat=css-filters
        // now with extended prefixed support by css preprocessor
        // not critical to support - hover effects are compromised
        // versions = { 
        //     "ie": 12,
        //     "edge": 13,
        //     "opera": 15,
        //     "firefox": 35,
        //     "chrome": 18,
        //     "safari": 6
        // }

        // final support
        // mix of prefixed flexbox and calc
        var versions = { 
            "ie": 11,
            "edge": 12,
            "opera": 15,
            "firefox": 4,
            "chrome": 19,
            "safari": 6
        }
        if ((typeof greyd_experiments !== 'undefined' && greyd_experiments === true) ||
            (typeof greyd_builder !== 'undefined' && greyd_builder === 'gg-gg')) {
            // mix of var() and css-filters
            versions = { 
                "ie": 12,
                "edge": 15,
                "opera": 36,
                "firefox": 35,
                "chrome": 49,
                "safari": 10
            }
        }

        if (dataset.browser == "ie" ||
            dataset.browser == "edge" ||
            dataset.browser == "opera" ||
            dataset.browser == "firefox" ||
            dataset.browser == "chrome" ||
            dataset.browser == "safari") {
            // filter complatibility
            if (compatibility.get_version_prim(dataset.version) < versions[dataset.browser]) 
                $("html").addClass("not_compatible");
        }
        
        // save data
        $("html").addClass(dataset.system);
        $("html").addClass(dataset.browser);
        $("html").data('device', dataset);
        // console.log(dataset.system+" "+dataset.browser+" v"+dataset.version);
    }

    this.get_version = function(name) {
        if (name == "MSIE") {
            if (navigator.userAgent.search("Trident") >= 0) {
                var tmp = navigator.userAgent.split("Trident/");
                var tmp1 = tmp[1].split("; ");
                if (tmp1[0].indexOf("6.") > -1) return "10.0";
                else if(tmp1[0].indexOf("7.") > -1) return "11.0";
            }
            else {
                var tmp = navigator.userAgent.split(MSIE+" ");
                var tmp1 = tmp[1].split("; ");
                return tmp1[0];
            }
        }
        else {
            var tmp = navigator.userAgent.split(name+"/");
            var tmp1 = tmp[1].split(" ");
            return tmp1[0];
        }
    }
    this.get_version_prim = function(version) {
        var tmp2 = version.split(".");
        return tmp2[0];
    }

}