var cookieAllow = false;
var isEU = { "isEU": false };
var triggerConsent = { "trigger": true };

var EUCode = ["AT", "BE", "BG", "CY", "CZ", "DE", "DK", "EE", "ES", "FI",
    "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LT", "LU", "LV",
    "MT", "NL", "PL", "PT", "RO", "SE", "SI", "SK"];

console.log("step1");

$(document).ready(function () {
    console.log("step2");
    UserInfo.getInfo(function (data) {
        console.log("step3");
        country = data.country.code
        console.dir(EUCode.indexOf(country));
        if (EUCode.indexOf(country) >= 0) { //判斷是否為歐盟國家IP
            isEU.isEU = true; //是的話則把isEU全域變數設為true
        }
        else {
            isEU.isEU = false;
            var cookie = $.cookie("cookieconsent_status");
                var p;
            if (typeof(cookie) == "undefined") {
                cookieconsent.initialise({
                    "palette": {
                        "popup": {
                            "background": "#5a5858",
                        },
                        "button": {
                            "background": "#d0343a"
                        }
                    },
                    "theme": "classic",
                    "position": "top",
                    "static": true,
                    "showLink": false,
                    "content": {
                        "message": "<div style=' width:90%; font-size:18px'> By continuing to use this site you consent to the use of cookies on your device as described in our " + "<a style='color:#d0343a;' href='https://www.adlinktech.com/en/CookiePolicy.aspx' target='_blank'>cookie policy </a>" + " unless you have already disabled cookies. You can change your " + "<a style='color:#d0343a;' href='https://www.adlinktech.com/en/CookiePolicy.aspx' target='_blank'>cookie settings</a>" + " at any time, but our site requires cookies to function properly and provide you with an optimal experience.</div> ",
                        "dismiss": "OK",
                        "allow": "OK",
                        "deny": " "
                    },
                    onPopupOpen: function () {
                        this.close();
                    },
                    onPopupClose: function () {
                    },
                    onInitialise: function (status) {
                        p.close();
                    },
                    onStatusChange: function (status) {
                        if(status== "allow")
                            p.close();
                    },
                    onRevokeChoice: function () {
                    },
                }, function (popup) {
                    p = popup;
                }, function (err) {
                    console.error(err);
                });
                p.setStatus(cookieconsent.status.allow);
                p.close();
            }
            if (typeof (p) != "undefined") {
                p.close();
            }
            
        }
        //判斷cookie consent狀態
        var consentAllow = $.cookie("cookieconsent_status");
        if (typeof (consentAllow) == "undefined") {
            ShowCookieConsent();
        }
        else if (consentAllow == "allow") {
            cookieAllow = true;

        }
        else if (consentAllow == 'dismiss') {
            cookieAllow = false;
        }
    });
})


function ShowCookieConsent() {
    //20180528 Andy 修改content 的顯示內容
    window.addEventListener("load", function () {
        window.cookieconsent.initialise({
            "palette": {
                "popup": {
                    "background": "#5a5858",
                },
                "button": {
                    "background": "#d0343a"
                }
            },
            "theme": "classic",
            "position": "top",
            "static": true,
            "type": "opt-in",
            "showLink":false,
            "content": {
                "message": "<div style=' width:90%; font-size:18px;'> By continuing to use this site you consent to the use of cookies on your device as described in our " + "<a style='color:#d0343a;' href='https://www.adlinktech.com/en/CookiePolicy.aspx' target='_blank'>cookie policy </a>" + " unless you have already disabled cookies. You can change your " + "<a style='color:#d0343a;' href='https://www.adlinktech.com/en/CookiePolicy.aspx' target='_blank'>cookie settings</a>" + " at any time, but our site requires cookies to function properly and provide you with an optimal experience.</div>",
                "dismiss": "QQ",
                "allow": "OK",
                "deny": " "
            },
            onInitialise: function (status) {
                console.dir(status);

            },
            onPopupOpen: function () {
                console.dir("popupopen");
            },
            onPopupClose: function () {
                console.dir("close");
            }
        })

        //20180528 Andy 不顯示解除按鈕
        var dismiss = document.getElementsByClassName("cc-dismiss");
        dismiss[0].style.display = "none";
        
    });
}


//watch
if (!Object.prototype.watch) {
    Object.defineProperty(
        Object.prototype,
        "watch", {
            enumerable: false,
            configurable: true,
            writable: false,
            value: function (prop, handler) {
                var old = this[prop];
                var cur = old;
                var getter = function () {
                    return cur;
                };
                var setter = function (val) {
                    old = cur;
                    cur =
                        handler.call(this, prop, old, val);
                    return cur;
                };

                // can't watch constants
                if (delete this[prop]) {
                    Object.defineProperty(this, prop, {
                        get: getter,
                        set: setter,
                        enumerable: true,
                        configurable: true
                    });
                }
            }
        });
}