
var disqus_config;
var loginDisqus;
var logoutDisqus;
var load_disqus;
var onDisqusLogin;
var onDisqusLogout;
var onUserStateChange;

/* * * DON'T EDIT BELOW THIS LINE * * */
(function($, undefined) {
    // Parse script JSON configuration
    var getScriptConfig = function() {
        var thisScript;
        var scripts = document.getElementsByTagName('script');
        for (var i = scripts.length - 1; i >= 0; i--) {
            var script = scripts[i];
            var src = script.src.toLowerCase();
            if (src != '' && src.indexOf('disqus.js') > -1) {
                thisScript = script;
                break;
            }
        }

    if(thisScript) {
        var str = thisScript.innerHTML;
        if (str === "") {
            str = '""';
        }
        eval('var o');
        try {
            eval('o=' + str);
        } catch (e) {}
        if(typeof o != 'object') {
            o = {};
        }
        return o;
    } else {
        return {};
    }
}

    // All settings can be overriden with JSON config
    var settings = $.extend(true, {
        init: true,
        short_name: 'undefined',
        cookie_name: 'disqus_remote_auth_s3',
        token_ajax_url: undefined, 
        page: {
            api_key: undefined,
            author_s3: undefined,
            category_id: undefined,
            developer: undefined,
            identifier: undefined,
            language: undefined,
            remote_auth_s3: undefined,
            slug: undefined,
            title: undefined,
            url: undefined
        },
        sso: {
            name: undefined,
            button: undefined,
            url: undefined,
            logout: undefined,
            width: 740,
            height: 430
        }
    }, getScriptConfig());

    // Global var looked at by Disqus
    disqus_config = function() {
        // Update settings with remote_auth_s3 cookie
        settings.page.remote_auth_s3 = $.cookie(settings.cookie_name);
        // Set disqus configs
        this.page = settings.page;
        this.sso = settings.sso;
    }

    // Bind to Janrain login/logout global events (there are none...)
    onDisqusLogin = function(user) {
        USER = user;
        onUserStateChange();
    }
    onDisqusLogout = function() {
        USER = undefined;
        onUserStateChange();
    }
    onUserStateChange = function() {
        if(USER) {
            loginDisqus();
        } else {
            logoutDisqus();
        }
    }

// Authenticate with Disqus
loginDisqus = function() {
    if(!$.cookie(settings.cookie_name)) { // Only if not already logged into Disqus
        // With valid user signature, returns Disqus cookie
        // Pass token to Disqus SDK
        $.cookie(settings.cookie_name, USER.signature);
        refreshUI()
    } else {
        refreshUI();
    }
}

// Remove Disqus authentication cookie and refresh UI if refresh = true
logoutDisqus = function(refresh) {
    if(typeof variable_here === 'undefined'){
        refresh = true
    };
    if($.removeCookie(settings.cookie_name)) {
        if(refresh) {
            refreshUI();
        }
    }
}

// Refresh Disqus UI and pass configuration
var refreshUI = function() {
    try { // DISQUS.reset sometimes throws an error, it is a bug.
        if(DISQUS && DISQUS.reset) {
            DISQUS.reset({
                reload: true,
                config: disqus_config
            });
        } else {
            setTimeout(refreshUI, 500)
        }
    } catch(e) {setTimeout(refreshUI, 500)}
}

if(settings.init) {
    $(document).ready(function() {
        // Generic Disqus embed code
        // http://help.disqus.com/customer/portal/articles/472097-universal-embed-code
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'http://' + settings.short_name + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })
}

load_disqus = function load_disqus() {
        // Generic Disqus embed code
        // http://help.disqus.com/customer/portal/articles/472097-universal-embed-code
         var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
         dsq.src = 'http://' + settings.short_name + '.disqus.com/embed.js';
         (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            $(".show-comments-button").each(function(){
            $(this).remove()
        })



}
}(jQuery));


$(window).load(function() {
     // Login/logout based on janrain events.
     janrain.events.onProviderLoginComplete.addHandler(onDisqusLogin);
     janrain.events.onProviderLogoutStart.addHandler(onUserStateChange);

    // Not used
    // $(".show-comments-button").each(function(){
    // 	$(this).bind('click', load_disqus)
    // })

})