﻿/*
 * FormFocus - simple jQuery plugin for contact forms
 * Version: 1.0 (09/22/2010)
 * Copyright (c) 2010 David Dierking
 * Requires: jQuery v1.3+
*/

/*
 * Installation Instructions
 * Install the following on your form page to initialize the plugin
    $(document).ready(function(){
        $("#divFields").formfocus();
    });
    
 * for advanced options
   $(document).ready(function(){
        $("#divFields").formfocus({
            focus : true|false,
            blur : true|false,
            keyup : true|false,
            label : true|false
        });
    });
 *Be sure to include blank.gif in your /images directory. This prevents the dynamically created labels from being selectable in IE.
*/

(function ($) {
    $.fn.formfocus = function(options)
    {
        var defaults = {
            focus : true,
            blur : true,
            keyup : true,
            label : true
        };
        var options = $.extend(defaults, options);
        this.find("input[type=text], textarea")
        .focus(function()
        {
            if(options.focus && !options.label)
            {
                $txt = $(this);
                if($txt.val() == $txt.attr("Default"))
                    $txt.val("");
            }
        })
        .blur(function()
        {
            if(options.blur && !options.label)
            {
                $txt = $(this);
                if($txt.val() == "")
                    $txt.val($txt.attr("Default"));
            }
        })
        .keyup(function(event)
        {
            if(options.keyup && options.label)
            {
                $txt = $(this);
                if($txt.val() == "")
                    $txt.prev().css("display", "block");
                else
                    $txt.prev().css("display", "none");
            }
        })
        .each(function()
        {
            $txt = $(this);
            if(options.label)
            {
                $txt.before("<label class='txtLabel' style='position: absolute; z-index: 0; display: none;'>" + $txt.attr("Default") + "</label>");
                $txt.css({"z-index" : "1", "position" : "relative", "background" : "url(/images/blank.gif)"});
                if($txt.val() == "")
                    $txt.prev().css("display", "block");
            }
            else
            {
                if($txt.val() == "")
                    $txt.val($txt.attr("Default"));
            }
        });
    }
})(jQuery);
