/**
 * 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>,
 * 				Minor modifications by Tristan Blease <tristan@bleasedesign.com>
 * @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(response, input) {
        reset(input);
        if (response == 1) {
            $(input).addClass(input.settings.valid);
            $(input).parent().addClass(input.settings.valid + 'Parent');
			if (eval("typeof " + input.settings.valid + "Callback == 'function'")) {
				eval(input.settings.valid + 'Callback(input)');
			}
        } else {
            $(input).addClass(input.settings.invalid);
            $(input).parent().addClass(input.settings.invalid + 'Parent');
			if (eval("typeof " + input.settings.invalid + "Callback == 'function'")) {
				eval(input.settings.invalid + 'Callback(input)');
			}

        }
    }

    this.each(function() {
        var settings = jQuery.extend({
            url         : url,
            verifying   : "",
            valid       : "",
            invalid     : "",
            method      : "get"
        }, options || {});
        
        this.timer    = undefined;
        this.settings = settings;
        $(this).keyup(function(e) {

			var keyCode = e.keyCode || window.event.keyCode;
			if (keyCode == 13) //enter was pressed
				return;

            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('id'),
                    value   : $(input).val() 
                };

                $(input).addClass(input.settings.verifying);
				if (eval("typeof " + input.settings.verifying + "Callback == 'function'")) {
					eval(input.settings.verifying + 'Callback(input)');
				}
                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;
};


