(function($) {
    $.fn.cd_formlabel = function(o) {
        return this.each(function() {
            new $cd_formlabel(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
    	notclass: false
    };

    /**
     * The cd_formlabel object.
     *
     * @constructor
     * @name $.cd_formlabel
     * @param Object e The element to create the slideshow for.
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/cd_formlabel
     */
    $.cd_formlabel = function(e, o) {
        this.options    = $.extend({}, defaults, o || {});
        
        this.form_element = $(e);

        var self = this;

        self.setup();
    };

    // Create shortcut for internal use
    var $cd_formlabel = $.cd_formlabel;

    $cd_formlabel.fn = $cd_formlabel.prototype = {
        cd_formlabel: '0.1'
    };

    $cd_formlabel.fn.extend = $cd_formlabel.extend = $.extend;

    $cd_formlabel.fn.extend({
        /**
         * Setups the slideshow.
         *
         * @name setup
         * @type undefined
         * @cat Plugins/cd_formlabel
         */
        setup: function() {
        	var self = this;
        	
        	var label = $('label[for='+self.form_element.attr("id")+']');
        	if(self.options.notclass) {
        		label = label.not(self.options.notclass);
        	}
        	
        	if(self.form_element.is('select')) {
  				label.data("origValue", label.text());
        		self.form_element.css("cursor", "pointer");
        		self.form_element.change(function() {
        			if(this.value == "") {
        				label.text(label.data("origValue"));
        			} else {
	        			label.text(this.value);	
        			}
        		});
        	} else {
        		
        	}
        	
			if(self.form_element[0].value !== "") {
				if(self.form_element.is('select')) {
	        		label.hide();
  				} else {
  					label.hide();
				  	//label.text(self.form_element[0].value);
  				}
        	}
        	self.form_element.focus(function() {
				if(self.form_element.is('select')) {
  				} else {
	        		label.fadeOut(200);
  				}
        	});
        	self.form_element.blur(function() {
        		if(this.value == "") {
        			label.fadeIn(200);
        		}
        	});
        }
    });

    $cd_formlabel.extend({
        /**
         * Gets/Sets the global default configuration properties.
         *
         * @name defaults
         * @descr Gets/Sets the global default configuration properties.
         * @type Hash
         * @param Hash d A set of key/value pairs to set as configuration properties.
         * @cat Plugins/jCarousel
         */
        defaults: function(d) {
            return $.extend(defaults, d || {});
        }
    });

})(jQuery);
