jQuery.fn.delayedEvent = function(evtype, callback, delayby) {
    return this.each(function() {
        var timer;
        jQuery(this).bind(evtype, function() {
            var that = this;
            if (timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(function() {
                callback.apply(that)
            }, delayby);
        });
    });
}

jQuery(function($) {
    function doAjaxThing() {
        var input = $(this);
        var span = input.parent().find('span');
        if (span.length == 0) {
            span = $('<span></span>').insertAfter(input);
        }
        span.empty().addClass('loading');
        $.get('/checkname', {name: input.val()}, function(data) {
            span.text(data).attr('class', data);
        });
    }
    $('input#id_slug').delayedEvent('keyup', doAjaxThing, 500);
});

jQuery(function($) {
    // If the page contains an error, scroll to it
    var error = $('.error:first');
    if (error.length) {
        $('html,body').animate({scrollTop: error.offset().top}, 500);
    }
});

