jQuery( document ).ready(function()
{
    var labels = jQuery('label.inside');

    labels.each(function()
	{
        var self = jQuery( this );
        var forId = self.attr('for');
        if (forId.length < 1)
        {
            return;
        }

        var labelText = jQuery.trim(self.text()).replace(/\:$/, '');
        this.textFields = jQuery( 'input[type=text]#'.concat(forId) ).add( 'textarea#'.concat(forId) ).add( 'input[type=password]#'.concat(forId) );
        if (this.textFields.size() > 0)
        {

            this.textFields.each(function()
        	{
        	    this.labelText = labelText;
                if (this.value == '')
                {
                    this.value = this.labelText;
                }
            });

        	this.textFields.focus(function()
        	{
        		if (this.value == this.labelText)
        		{
        			this.value = '';
        		}
        	});

        	this.textFields.blur(function()
        	{
        		if (this.value == '')
        		{
        			this.value = this.labelText;
        		}
        	});

        }

        this.selectFields = jQuery( 'select#'.concat(forId) );
        if (this.selectFields.size() > 0)
        {
            this.selectFields.each(function()
        	{
                if (this.options.length > 0)
                {
                    var firstOption = this.options[0];
                    if (firstOption.value == '')
                    {
                        firstOption.text = labelText;
                    }
                    // :TODO: create new empty option if not found
                }


        	});
        }

	});

	labels.parents('form').submit(function()
	{
	    var self = jQuery( this );
	    var labels = self.find('label.inside');
        labels.each(function()
        {
            var self = jQuery( this );
            var forId = self.attr('for');
            if (forId.length < 1)
            {
                return;
            }

            this.textFields.each(function()
        	{
                if (this.value == this.labelText)
                {
                    this.value = '';
                }
            });
        });

	});

});

