if (window.disqus === undefined) {
    var disqus = {};
}

(function() {
    // HACK: allow use of same-domain ajax calls
    window.$._ajax = window.$.ajax;

    if (context.disqusDomain != window.location.host) {
        var queue = [];
        // Queue ajax calls until our cross-domain jQuery tunnel
        // is open
        $.ajax = function(options) {
            queue.push(options);
        };

        var onload = function() {
            window.$.ajax = tunnel.contentWindow.jQuery.ajax;
            
            // Flush queued $.ajax calls
            $.each(queue, function(_, options) {
                $.ajax(options);
            });
            queue = null;
        };

        var tunnel = $('<iframe>')
            .attr('src', context.disqusUrl + '/tunnel/')
            .load(onload)
            .appendTo('head')[0];
    }
})();

(function () {

    disqus.showLoginBox = function(el) {
        $.facebox.settings.opacity = 0.9;
        $.get(context.disqusUrl + '/auth/login.html', function(data) {
            var service;
            if(context.forumUrl === undefined) {
                service = 'profile';
            } else {
                service = 'admin';
            }

            $.facebox('<h3>Log into your existing account</h3>' + data);
            // Set redirect url if provided, otherwise redirect to current page
            var next = (utils.query().next) ? utils.query().next : window.location;
            $('form#login-form').attr('action', context.loginUrl + '?next=' + next);
            $('form#login-form input[type=text]')[0].focus();
            $.facebox.settings.opacity = 0; // Reset opacity
        });
    };

    // turn off autogrow. it sucks.
    disqus.autogrow = function (object) {
        return;
    };

})();

$(document).ready(function () {

    // Set login link
    $('a[rel=login]').click(function () {
        disqus.showLoginBox(this);
        return false;
    });
});


