﻿function TrackPage() {
    $.trackPage('UA-5849760-5', { onload: false })
}

var googleanalytics;

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function checkCookie(c_name) {
    var cookie = getCookie(c_name);
    if (cookie != null && cookie != "") {
        return true;
    }
    else {
        return false;
    }
}

function OptInDecision(allow) {
    if (allow) {
        TrackPage();
        setCookie("opto_optinout", true, 1);
    }
    else {
        setCookie("opto_optinout", false, 1);
    }
}

//Checks the opt-in status of the user
function OptInCheck() {
    if (checkCookie("opto_optinout") == false) {
        /*
        Delete OptInDecision(true);, make OptInModal() visible and
        in OptInDecision(allow) method set '1' to '365' on setcookie 
        methods when ready for EU cookie change.
        */
        //OptInModal();
        OptInDecision(true);
    }
    else {
        var optin = getCookie("opto_optinout");
        if (optin == "true") {
            //Set google analytics JS
            googleanalytics = true;
        }
        else {
            //Do not set google analytics JS
            googleanalytics = false;
        }
    }
}

function OptInModal() {
    $('#optinmodal').modal({
        persist: true,
        containerCss: { backgroundColor: "#EEEEEE", padding: 0 },
        overlayCss: { backgroundColor: "#000" },
        opacity: 40,
        escClose: false,
        onOpen: function (dialog) {
            dialog.overlay.fadeIn('slow', function () {
                dialog.data.hide();
                dialog.container.slideDown('slow', function () {
                    dialog.data.fadeIn('slow');
                });
            });
        },
        onClose: function (dialog) {
            dialog.data.fadeOut('slow', function () {
                dialog.container.slideUp('slow', function () {
                    dialog.overlay.fadeOut('slow', function () {
                        $.modal.close(); // must call this!
                    });
                });
            });
        }
    });

    return false;
}
