/**
 * jQuery.fn.verify
 *
 * - url        The URL to send the POST/GET AJAX request to
 * - verifying  The CSS class to toggle while verification is taking place
 * - valid      The CSS class to toggle if verification is successful
 * - invalid    The CSS class to toggle if verification fails
 * - method     Set to either get or post
 *
 * The url should expect to receive either a GET or POST variable named
 * 'value'. In that will be the value of the input this is attached to. It
 * should then respond with either '1' or '0'. '1' means that the value is
 * valid, while '0' (or any non '1' response) means the value is invalid.
 *
 * Additionally, the classes verifying + 'Parent', valid + 'Parent', and
 * invalid + 'Parent' are toggled appropriately. This allows you to add a
 * background icon to the container element if you so wish for instance.
 *
 * @author      Joe Stump <joe@joestump.net>
 * @param       string      url
 * @param       array       options
 */
jQuery.fn.verify = function(url, options) {
    $(this).attr('autocomplete', 'off');

    function reset(input) {
        $(input).removeClass(input.settings.verifying)
                .removeClass(input.settings.valid)
                .removeClass(input.settings.invalid);

        $(input).parent().removeClass(input.settings.verifying + 'Parent')
                         .removeClass(input.settings.valid + 'Parent')
                         .removeClass(input.settings.invalid + 'Parent');
    }

    function response(resp, inpt) {
        reset(inpt);
        if (resp === 1) {
            $(inpt).addClass(inpt.settings.valid);
            $(inpt).parent().addClass(inpt.settings.valid + 'Parent');
        } else {
            $(inpt).addClass(inpt.settings.invalid);
            $(inpt).parent().addClass(inpt.settings.invalid + 'Parent');
        }
    }

    this.each(function() {
        var settings = jQuery.extend({
            url         : url,
            verifying   : "",
            valid       : "",
            invalid     : "",
            method      : "get"
        }, options || {});

        this.timer    = undefined;
        this.settings = settings;
        $(this).keyup(function() {
            if (this.timer) {
                clearTimeout(this.timer);
                this.timer = undefined;
            }

            if (this.value.length === 0) {
                reset(this);
                return;
            }

            var input = this;
            this.timer = setTimeout(function() {
                params = {
                    id      : $(input).attr('name'),
                    value   : $(input).val()
                };

                $(input).addClass(input.settings.verifying);
                if (input.settings.method === "get") {
                    $.get(input.settings.url, params, function(result) {
                        response(result, input);
                    });
                } else {
                    $.post(input.settings.url, params, function(result) {
                        response(result, input);
                    });
                }
            }, 1000);
        });
    });

    return this;
};


