// 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.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);
    },

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

    gray_out : function() {
        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_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));
    }

});