﻿FindYontooClient = {
    _installCookieName: 'INSTALLER-STATUS',
    _clientIdCookieName: "Y2INSTALLID",
    _createCookie: function (name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    },

    _readCookie: function (name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    },

    _eraseCookie: function (name) {
        this._createCookie(name, "", -1);
    },

    GetInstalledFromCookie: function () {
        var val = this._readCookie(this._installCookieName);
        if (val != null)
            return true;
        else
            return false;

    },

    GetSwidgetEnabledFromCookie: function () {

        var val = this._readCookie(this._installCookieName);
        if (val != null && val == 'true') {
            return true;
        }
        else {
            return false;
        }


    },

    SetInstallCookie: function (installed, enabled) {
        if (installed) {
            this._createCookie(this._installCookieName, enabled + '', 10);
        }
        else {
            this._eraseCookie(this._installCookieName);
        }
    },

    GetClientIdFromCookie: function () {
        var ClientId = this._readCookie(this._clientIdCookieName);
        if (ClientId == null) {
            ClientId = this.GetClientId();
            if (ClientId != null && ClientId != '') {
                this.SetClientIdCookie(ClientId);
            }
        }
        return ClientId;
    },

    SetClientIdCookie: function (clientId) {
        this._createCookie(this._clientIdCookieName, clientId, 10);
    },

    GetClientId: function () {
        try {
            if (typeof (YontooClient) == "undefined") {
                try {
                    if (window.ActiveXObject) {
                        yontooAPI = new ActiveXObject("YontooIEClient.Api");
                    }
                }
                catch (ex) {
                }
            }

            if (typeof (YontooClient) != "undefined") {
                var id = YontooClient.getInstallId();
                return id;
            } else if (typeof (getInstalledClientId) !== "undefined") {    //chrome
                return getInstalledClientId();
            } else {
                var idElement = $("#YontooInstallID");
                return idElement.html();
            }
        }
        catch (ex) {
        }
        return "";


    },

    IsSwidgetInstalled: function () {
        if (typeof (YontooClient) !== 'undefined' && YontooClient !== null && 
        !(typeof (YontooClient.Swidget) === 'undefined' || YontooClient.Swidget === null))
            return true;
        else
            return false;
    },

    CheckInstallId: function (countCheck, successFunction, failFunction) {
        // countCheck = (countCheck == undefined || countCheck == "" || countCheck == null) ? 20 : countCheck;
        try {
            if (countCheck > 0) {
                var installId = this.GetClientId();

                if (installId === 0 || installId === null || installId === "" || installId === 'undefined' || typeof (installId) == "undefined") {
                    //console.log(retry, installId);
                    countCheck = countCheck - 1;
                    var that = this;
                    setTimeout(function () { that.CheckInstallId(countCheck, successFunction, failFunction) }, 100);
                } else {
                    //console.log(successFunction + "('" + installId + "');");
                    if (successFunction != undefined && successFunction != "" && successFunction != null) {
                        //console.log(successFunction + "('" + installId + "');");
                        setTimeout(successFunction + "('" + installId + "');",2000);
                    }
                    return true;
                }
            } else {
                if (failFunction != undefined && failFunction != "" && failFunction != null) {
                    //console.log(failFunction + "('" + installId + "');");
                    eval(failFunction + "();");
                }
            }
        } catch (ex) {
            //alert(ex);
        }
    },

    CheckForClient: function (count) {
        var clientId = this.GetClientId();
        if (clientId != null && clientId != '') {
            this.SetClientIdCookie(clientId);
            if (!this.GetInstalledFromCookie())
                this.SetInstallCookie(true, false);
            this.CheckForSwidget(count);
            return;
        }
        count = count - 1;
        if (count > 0) {
            this.ScheduleClientCheck(count);
        }
        else {
            this.HandleNoFind(false);
        }
    },

    CheckForSwidget: function (count) {
        if (this.IsSwidgetInstalled()) {
            this.SetInstallCookie(true, true);
            $('#marquee_internal_download').hide();
            return;
        }

        count = count - 1;
        if (count > 0) {
            this.ScheduleSwidgetCheck(count);
        }
        else {
            this.HandleNoFind(true);
        }
    },

    HandleNoFind: function (isInstalled) {
        if (!isInstalled) {
            this._eraseCookie(this._clientIdCookieName);
            this._eraseCookie(this._installCookieName);
            $('#marquee_internal_download').show();
        } else {
            this.SetInstallCookie(true, false);
        }

    },


    ScheduleClientCheck: function (count) {
        var code = this;
        setTimeout(function () { code.CheckForClient(count); }, 300);

    },

    ScheduleSwidgetCheck: function (count) {
        var code = this;
        setTimeout(function () { code.CheckForSwidget(count); }, 300);

    }
};

$(window).load(
function () {
    FindYontooClient.CheckForClient(35);
});
           
