
/**
 * @name Placeholder
 * @description Placeholder plugin for text inputs.
 * 
 * @author Artur Grigor
 * @version 1.0
 */

(function( $ ) {

    var methods = {
        
        /**
         * Check if HTML5 Placeholder attribute is supported by the browser.
         *
         * @return Boolean
         */
        _testAttribute: function () {
            var test = document.createElement('input');
            var ret = ('placeholder' in test);
            test = null;
            return ret;
        },
        _focusIn: function ($this, placeholder) {
            if ($this.val() === placeholder) {
                $this.val('');
                $this.removeClass('placeholder');
            }
        },
        _focusOut: function ($this, placeholder) {
            if ($.trim($this.val()) === '') {
                $this.val(placeholder);
                $this.addClass('placeholder');
            }
        },
        _change: function ($this, placeholder) {
            if ($.trim($this.val()) === '') {
                $this.val(placeholder);
                $this.addClass('placeholder');
            } else {
                $this.removeClass('placeholder');
            }
        },
        
        init: function (options) {
            
            /*
             * We don't need to mess up with HTML5 Placeholder attribute.
             */
            if (methods._testAttribute()) {
                return false;
            }
            
            var settings = {
                'live': false
            };
            
            if ( options ) {
                $.extend( settings, options );
            }
            
            return this.each( function () {
                
                var $this = $(this);
                var placeholder = $this.attr('placeholder');
                
                if ( settings.live ) {
                    
                    $this.live( 'focusin.placeholder', function () {
                        methods._focusIn($this, placeholder);
                    });
                
                    $this.live( 'focusout.placeholder', function () {
                        methods._focusOut($this, placeholder);
                    });

                    $this.live( 'change.placeholder', function () {
                        methods._change($this, placeholder);
                    });

                } else {
                    
                    $this.bind( 'focus.placeholder', function () {
                        methods._focusIn($this, placeholder);
                    });
                
                    $this.bind( 'blur.placeholder', function () {
                        methods._focusOut($this, placeholder);
                    });

                    $this.bind( 'change.placeholder', function () {
                        methods._change($this, placeholder);
                    });
                }
                
                methods._focusOut($this, placeholder);

            });
        },
        
        destroy: function () {
            
            return this.each( function () {
                
                $(window).unbind('.placeholder');
                
            });
            
        }
        
    };

    $.fn.placeholder = function ( method ) {
        
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' + method + ' does not exist on jQuery.placeholder' );
        }
        
    };
    
})( jQuery );

$(document).ready(function () {
	$('input[placeholder]').placeholder();
});
