﻿$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;

        // fix any watermarks
        if ($(this).attr('watermark') !== undefined) {
            if (this.value === "") {
                this.value = $(this).attr("watermark");
                if(!($(this).hasClass('watermark'))) {
                    $(this).addClass("watermark");
                }
            }
        }        
    });
};


$(document).bind("ready", function() {
    // button effects
    $('button').filter('.ui-state-default').hover(
	            function() {
	                if (!$(this).hasClass('ui-state-disabled')) {
	                    $(this).addClass("ui-state-hover");
	                }
	            },
	            function() {
	                if (!$(this).hasClass('ui-state-disabled')) {
	                    $(this).removeClass("ui-state-hover");
	                }
	            });

    // watermarks and row highlights
    $("[watermark]").each(function() {
        if (this.value === "") {
            this.value = $(this).attr("watermark");
            $(this).addClass("watermark");
        }
    })
    .focus(function() {
        if (this.value === $(this).attr("watermark")) {
            this.value = "";
            $(this).removeClass("watermark");
        }
        // add row highlight
        //$(this).parents(".formitem").css("background-color", "#ddd");
    })
    .blur(function() {
        if (this.value === "") {
            this.value = $(this).attr("watermark");
            $(this).addClass("watermark");
        }
        //remove row highlight
        //$(this).parents(".formitem").css("background-color", "");
    });
    //remove watermark when form is submitted
    $("#button_submit").click(function() {
        $("[watermark]").each(function() {
            if (this.value === $(this).attr("watermark")) {
                this.value = "";
            }
        });
    });
    // tooltips                
    $(":text[hint]").focus(function() {
        var field = $(this);
        field.after("<p class='ui-widget ui-state-highlight ui-corner-all' id='tooltip'><span class='ui-icon ui-icon-info' style='float: left; margin-right: 0.3em;'>&nbsp;</span>" + field.attr('hint') + "</p>");
        var position = field.offset();
        $("#tooltip")
                    .css("top", (position.top - field.height() - $("#tooltip").height() - 16) + "px")
                    .css("left", (position.left /*+ field.width()+ 10*/) + "px")
                    .fadeIn("slow");
    })
            .blur(function() {
                $("#tooltip").remove();
            });
    
});
