var DynamicInput = new Class({
    Implements: [Events, Options],

    options: {
        attribute: 'title',
        maxlength: true
    },

    initialize: function(elements, options) {
        // Set elements
        this.elements = $$(elements);
        // Set options
        this.setOptions(options);

        this.elements.each(function(el) {
            // Fix IE6
            if (Browser.Engine.trident && Browser.Engine.version <= 6) {
                this.fixIE6.run(el);
            }
            
            // Check if element has attribute
            if (!el.get(this.options.attribute)) return;
            // Replace initial value
            this.replace.run(el, this);
            // Add events
            el.addEvents({
                'focus': this.clear.pass(el, this),
                'blur': this.replace.pass(el, this)
            });
            // Clear default values on submit
            if (el.getParent('form') != null) {
                el.getParent('form').addEvent('submit', this.clear.pass(el, this));
            }
        }, this);
    },

    replace: function(el) {
        // Replace value to title
        if (el.get('value') == '') el.set('value', this.attribute.run(el, this));
    },

    clear: function(el) {
        // Clear replaced value
        if (el.get('value') == this.attribute.run(el, this)) el.set('value', '');
    },

    attribute: function(el) {
        // Get trunicated attribute
        var attribute = el.get(this.options.attribute);
        if (this.options.maxlength && el.get('maxlength').toInt() > 0) attribute = attribute.substring(0, el.get('maxlength').toInt());
        return attribute;
    },

    fixIE6: function(el) {
        el.getParent().addClass(el.getParent().get('class').split(' ').sort().join('-'));
    }
});