// ClearOnFocus JQuery plugin:
// Unobtrusive solution to have default texts on textboxes that will go away
// when clicked. (When the user doesn't have JavaScript enabled, the textbox
// will be blank.)
//
// Usage:
// <input type="text" rel="[default=Enter your email address]" />
// $($.clearOnFocus);
// input.default-text { color: #888; };
(function ($)
{
    $.clearOnFocus = function () 
    {
        // For each input box with the default in the rel field (mostly search boxes),
        // and has the value attribute:
        $('textarea[rel*=default],input[rel*=default]').each(function () 
        { 
            // See if it's in the right format. Don't continue if it's not            
            var match = (/\[default=([^\]]*)\]/).exec($(this).attr('rel'));
            if (!match) {
                return;
            }
            // Get the default text
            this.originalText = match[1];
            // When it's clicked...
            $(this).focus(function () 
            {
            	// And the text is the same as the default text...
                if (this.originalText == $(this).val()) 
                {
                    // Clear it, and remove the .default-text class.
                    field = $(this);
                    if (!$.clearOnFocus.animateOut) {
                        field.val('');
                        field.removeClass('default-text');
                    }
                    else 
                    {
                        if ($.clearOnFocus.timer) {
                            window.clearInterval($.clearOnFocus.timer);
                        }
                        $.clearOnFocus.offset = 2;
                        $.clearOnFocus.timer = window.setInterval(function () 
                        {
                            i = field.val().length - ($.clearOnFocus.offset);
                            $.clearOnFocus.offset += 3;
                            if (i < 0) {
                                i = 0;
                            }
                            field.val(field.val().substr(0, i) + ".");
                            if (field.val().length == 1) 
                            {
                                field.val('');
                                field.removeClass('default-text');
                                window.clearInterval($.clearOnFocus.timer);
                            }
                        }, 10);
                    }
                }
                // But if there's something in it...
                else {
                    // Select the whole thing
                    this.selectionStart = 0;
                    this.selectionEnd = this.value.length;
                }
            });
            // And when leaving the field...
            $(this).blur(function () 
            {
                // And there's nothing in it...
                if (('' == $(this).val()) || (this.originalText == $(this).val())) 
                {
                    // Switch it back to the default text.
                    field = $(this);
                    originalText = this.originalText;
                    if ((!$.clearOnFocus.animateIn) || (this.firstRun)) {
                        field.val(originalText);
                        field.addClass('default-text');
                        this.firstRun = 0;
                    }
                    else 
                    {
                        if ($.clearOnFocus.timer) {
                            window.clearInterval($.clearOnFocus.timer);
                        }
                        $.clearOnFocus.offset = 0;
                        field.addClass('default-text');
                        field.val('');
                        $.clearOnFocus.timer = window.setInterval(function () 
                        {
                            $.clearOnFocus.offset += 2;
                            field.val(originalText.substr(0, $.clearOnFocus.offset - 1) + "_");
                            if (field.val().length >= originalText.length) {
                                field.val(originalText);
                                window.clearInterval($.clearOnFocus.timer);
                            }
                        }, 10);
                    }
                }
            });
            // Run the blur code (bad and lazy)
            this.firstRun = 1;
            $(this).blur();
        });
        
        return $;
    };
})(jQuery);
//$.clearOnFocus.animateOut = 1;
//$.clearOnFocus.animateIn = 1;
$($.clearOnFocus);
