
/*
 * Placeholder
 * @author Gennadiy Ukhanov
 * @version 0.0.1
 * @build 34 (30/06/2011 15:56)
 */
(function(_global){

    var $=jQuery;

    /**
     * @namespace Placeholder
     * @name Placeholder
     */
    bfm.utils.Placeholder = {

        /**
         * @public
         */
        create : function() {

            // To detect native support for the HTML5 placeholder attribute
            this.isPlaceholder = ("placeholder" in document.createElement("input"));

            if(!this.isPlaceholder) {
                var input = $('input');
                for(var i=0; i<input.length; i++) {
                    if($(input[i]).attr('placeholder')!=undefined) {
                        $(input[i]).val( $(input[i]).attr('placeholder'));
                        $(input[i]).addClass('-global-placeholder');
                        $(input[i]).focus($.proxy(this, 'onFocus'));
                        $(input[i]).blur($.proxy(this, 'onBlur'));
                    }
                }
            }
        },

        onFocus : function(e) { this.change(e); },
        onBlur : function(e) { this.change(e); },
        change : function(event) {
            var input = $(event.currentTarget);
            var placeholder = input.attr('placeholder');

            if(event.type == "focus") {
                if(input.val()==placeholder) {
                    input.val(null);
                    input.removeClass('-global-placeholder');
                }
            }
            if(event.type == "blur") {
                if(input.val()=="") {
                    input.val(placeholder);
                    input.addClass('-global-placeholder');
                }
            }

            return false;
        }

    }

    /**
     * Constructor
     */
    _global = bfm.utils.Placeholder;
    _global.isPlaceholder = true;

})(window || this);
