/*
 * Clears textfield placeholder text
 */
;(function($){

    $.fn.autoClear = function(fn) {

        if (typeof fn == "string") {
            switch(fn) {
                case "clear" : return clear(this); break;
                case "reset" : return reset(this); break;
            }

        } else if (arguments.length==0) {
            return this.each(function() {

                var $this = $(this);

                $this
                    .data("default-value", $this.val())
                    .bind("focus", function(event) {
                        var $target = $(event.currentTarget);
                        if ($target.val() == $target.data("default-value")) $target.val("");
                    })
                    .bind("blur", function(event) {
                        var $target = $(event.currentTarget);
                        if ($target.val() == "") $target.val($target.data("default-value"));
                    });

            });
        }

        function clear(_this) {
            return _this.each(function(){
                var $this = $(this);
                if ($this.val() == $this.data("default-value")) $this.val("");
            });
        }

        function reset(_this) {
            return _this.each(function(){
                var $this = $(this);
                if ($this.val() == "") $this.val($this.data("default-value"));
            });
        }

    };

})(jQuery)


/*
 * Takes text from label and adds it to a input as the default value. Optionally hides the label.
 */
;(function($){

    $.fn.labelsToDefaultValues = function(hideLabel) {
        var formItems = jQuery();
        $(this[0]).parents().find("form").bind("submit", function(){
            formItems.each(function(){
                var $this = $(this);
                if ( $this.val()==$this.data("default-value") ) $this.val("");
            });
        });
        return this.each( function() {

            var $this = $(this),
                formItem = $( "#" + $this.attr("for") ),
                formItemValue = formItem.val(),
                matched = false;

            if (formItem.is("select")) {
                var defaultItem = $("<option value=\"-1\">" + $this.text() + "</option>");
                if ( $( "option[selected='selected']" ).length==0 ) { defaultItem.attr("selected","selected"); }
                formItem.prepend( defaultItem );
                matched = true;
            } else if (formItem.is("input[type='password']")) {
                var $pwLabel = $("<input type=\"text\" value=\"" + $this.text() + "\">").addClass( formItem[0].className );
                formItem.after(
                    $pwLabel.data("pwFormItem", formItem)
                        .bind("focus", function(){
                            $(this).hide().data("pwFormItem").show().focus();
                            return false;
                        })
                );
                formItem.bind("blur", function(){
                   if ($(this).val()=="") {
                       $pwLabel.show();
                       $(this).hide();
                   }
                });
                if (formItemValue!="") { $pwLabel.hide(); } else { formItem.hide(); }
                matched = true;
            } else if (formItem.is("input, textarea")) {
                if (formItemValue=="") {
                    formItem.val( $this.text() );
                    formItems = formItems.add(formItem);
                    formItem.autoClear();
                }
                matched = true;
            }
            if (hideLabel && matched) $this.hide(); /* hide label */

        } );
    };

})(jQuery)

