﻿(function($) {
    $.fn.CheckUserId = function(options) {
        // Set the options.
        var opts = $.extend({}, $.fn.CheckUserId.defaults, options);

        // Go through the matched elements and return the jQuery object.
        return this.each(function() {
            var userInput = $(this); // Input Element
            var userResult = $('<span/>');

            userInput.blur(function(ev) {
                if (userInput.val().length > 0) {
                    userResult.html('Searching...').insertAfter(userInput);
                    $.getJSON(AjaxRouteData.Account_CheckUserIdAvailability, "userId=" + userInput.val(), function(data) {
                        setTimeout(function() {
                            userResult.hide();
                            userResult.html(data);
                            userResult.fadeIn();
                        }, opts.delay);
                    });
                }
                else
                    userResult.hide();
            });
        });
    };
    
    // Public defaults.
    $.fn.CheckUserId.defaults = {
        delay : 400
    };
})(jQuery);
