/*
* jQuery emptyonclick plugin
*
* Created by Andreas Creten (andreas@madewithlove.be) on 2008-06-06.
* Copyright (c) 2008 madewithlove. All rights reserved.
*
* Version: 1.0
*
* Changelog :
* Version 1.0 (06 Jun 2008):
*  - Original version
*/

jQuery.fn.extend({
    emptyonclick: function(options) {
        return this.each(function() {
            new jQuery.EmptyOnClick(this, options);
        });
    }
});

jQuery.EmptyOnClick = function(element, options) {
    var defaultValue = $(element).val();
    
    // Bind event handlers to the element
    $(element)
    // On Focus: Store the default value if it's not set, empty the field
    .bind("focus", function(e) {
        if(defaultValue == $(this).val())
            $(this).val('').addClass(options.changeClass);

    })
    // On Blur: if the field is empty, reset the default value
    .bind("blur", function(e) {
        if(!$(this).val()) {
            $(this).val(defaultValue).removeClass(options.changeClass);
        }
    });

    // If the form gets resetted, set the default value back
    $("form:has(#"+element.id+")").bind('reset', function(e) {
        $(element).val(defaultValue);
        $(element).removeClass(options.changeClass);
    }).bind('submit', function(e) {
        if($(element).val() == defaultValue)
            $(element).val('');
    });
};

$(document).ready(function(){
    $(".emptyonclick").emptyonclick({
        changeClass: 'focus'
    });    
});
