// Namespace
var iOffer = iOffer ? iOffer : {}

// Used on Inputs. If there is no text entered add the +initial+
// text as grayed out. On click, clear out and update color;
iOffer.DefaultInput = Class.create({

    initialize : function(input, initial, options) {
        if (!options || options == undefined)
            options = {};

        this.set_initial_value =
        ( options.setToInitial == undefined ? true : options.setToInitial );
        
        this.keyup      = options.onKeyUp || Prototype.emptyFunction;
        this.after_init = options.afterInitialize || Prototype.emptyFunction;
        this.password   = options.password || false;

        this.input    = $(input);
        this.initial  = initial;
        this.register_events();

        if(this.set_initial_value)
            this.set_to_initial();

        this.check_state(true);

        // Hook Method
        this.after_init(this);
    },

    // Gay IE doesn't allow you to change the type of an input programatically
    // so we need to create a new field with the same attributes.
    update_input_type : function(type) {
        if ( this.input.readAttribute('type') == type )
            return;

        var new_input = new Element( 'input', {
            id    : this.input.readAttribute('id'),
            size  : this.input.readAttribute('size'),
            style : this.input.readAttribute('style'),
            name  : this.input.readAttribute('name'),
            type  : type
        });
        
        this.input.classNames().each( function(classname) {
            new_input.addClassName(classname);
        });

        this.input.replace(new_input);
        this.input = $(new_input);
        
        if (type == 'password')
            this.input.focus();
        
        this.register_events();
    },

    black_out : function() {
        this.input.setStyle({
            color : '#000000'
        });

        if (this.password)
            this.update_input_type('password');
    },

    gray_out : function() {
        if (this.password) {
            this.input.value = '';
            this.update_input_type('text');
        }
        
        this.input.setStyle({
            color : '#999999'
        }).value = this.initial;
    },

    check_state : function(skip_clear) {
        if (this.is_default_value()) {
            if (skip_clear != true) {
                this.input.value = '';
                this.black_out();
            } else {
                this.gray_out();
            }
        } else {
            this.black_out();
        }
    },

    is_default_value : function() {
        var value   = this.input.getValue().strip().toLowerCase();
        var initial = this.initial.strip().toLowerCase();
        return value == initial
    },

    is_blank : function() {
        return this.input.getValue().blank();
    },

    set_default_to_blank : function() {
        if (this.is_default_value())
            this.input.value = '';
    },

    set_blank_to_default : function() {
        if (this.is_blank())
            this.set_to_initial();
    },

    set_to_initial : function() {
        this.input.value = this.initial;
    },

    register_events : function() {
        this.input.observe('blur',  function() {
            if (this.is_blank())
                this.set_to_initial();
            this.check_state(true);
        }.bind(this));

        this.input.observe('focus', function() {
            this.check_state();
        }.bind(this));

        this.input.observe('keyup', function() {
            this.keyup(this);
        }.bind(this));
    }

});