/*
 * jQuery "place label on input as a default value like on yandex.ru" plugin
 * receives optional settings object, you can change default positioning
 * and class used by label; 
 * usage: 
 * $('#inputIdGoesHere).hideLabel({
 *     'changingProperty':'top',
 *     'initialValue':'0',
 *     'hiddenValue':'-9999px',
 *     'inputClass':'.someClass'    
 * });
 * 
**/
(function($) {
    $.fn.hideLabel = function(settings) {
        var options = {
            'changingProperty':'display',
            'initialValue':'block',
            'hiddenValue':'none',
            'inputClass':''
        };


        if (settings) {
            // if not present in the string, add ".", so we can use it as a selector
            if (settings.inputClass) {
                /* validate for css class */
                var regex = /\.[A-Za-z_0-9\s\-]*/,
                    si = settings.inputClass;
                settings.inputClass = (regex.test(si)) ? si : '.'+si;
            }

            $.extend(options, settings);
        }


        this.each(function() {
            //label
            var $this = $(this),
                $input = $this.siblings(["input",options.inputClass].join(''));

            if ($input.val()!=''){
                $this.css(options.changingProperty,options.hiddenValue);
            }
    
            $input
                .focus(function(){
                    $this.css(options.changingProperty,options.hiddenValue);
                })
                .blur(function(){
                    if ($input.val()=='') {
                        $this.css(options.changingProperty,options.initialValue);
                    } else {
                        $this.css(options.changingProperty,options.hiddenValue);
                    }
                });
        });

        return this;

    };
})(jQuery);

