/**
* A text box watermark implementation similar to the one found in the
* AJAX Control Tooklit for ASP.net
*
* @author Artem Ploujnikov <artem@htc.ca>
*/
Watermark = Class.create(
    {
        /**
        * The text box with which this watermark is associated
        * 
        * @var HTMLElement
        */
        textBox: null,
        
        /**
        * The text to display in the element until the
        * user focuses on it
        */
        text: "Enter text...",
        
        /**
        * Indicates whether or not the watermark is currently
        * being displayed
        *
        * @var Boolean
        */
        displaying: false,
        
        /**
        * Creates a new watermark for a text box
        *
        * @param Object textBox a DOM object corresponding to a text input
        *   or the value of its "id" attribute
        *
        * @param String text the watermark text to be displayed (optional)
        */
        initialize : function(textBox, text) {
            this.textBox = $(textBox);
            if (text) {
                this.text = text;
            }
            this.textBox.title = this.text;
            this.attachEvents();
            this.show();
        },
        
        /**
        * Attaches event handlers to the text box
        */
        attachEvents : function()
        {            
            var watermark = this;
            Event.observe(
                this.textBox,
                'focus',
                function() {
                    watermark.hide();
                }
            );
            
            Event.observe(
                this.textBox,
                'blur',
                function() {
                    if (watermark.textBox.value == '') {
                        watermark.show();
                    }
                }
            );
            
            if (this.textBox.form) {
                Event.observe(
                    this.textBox.form,
                    'submit',
                    function() {
                        if (watermark.isDisplaying()) {
                            watermark.hide();
                        }
                    }
                );
            }
            
        },
        
        /**
        * Causes the text box to display a watermark
        */
        show: function()
        {
            this.textBox.addClassName(Watermark.CSS_CLASS);
            this.textBox.value = this.text;
            this.displaying = true;
        },
        
        /**
        * Hides the watermark and clears the text box
        */
        hide: function()
        {
            if (this.displaying) {
                this.textBox.removeClassName(Watermark.CSS_CLASS);
                this.textBox.clear();
                this.displaying = false;
            }
        },
        
        /**
        * Determines whether the watermark is currently
        * being displayed
        *
        * @return Boolean
        */
        isDisplaying : function()
        {
            return this.displaying;
        }
    }
);

Watermark.CSS_CLASS = 'watermark';

/**
* Adds watermark support for this text box
*
* @param Object textBox a DOM object corresponding to a text input
*   or the value of its "id" attribute
*
* @param String text the watermark text to be displayed (optional)
*/
Watermark.add = function(textBox, text) {
    new Watermark(textBox, text);
}
