﻿/*
 http://remysharp.com/2007/03/19/a-few-more-jquery-plugins-crop-labelover-and-pluck/#labelOver
 */
jQuery.fn.labelOver = function(overClass) {
    return jQuery(this).each(function() {
        var label = jQuery(this);
        var f = label.attr('for');
        if (f) {
            var input = jQuery('#' + f);

            this.hide = function() {
                if (input.val() != '') label.css({ textIndent: -10000, visibility: 'hidden' });
                else label.css({ textIndent: 0, visibility: 'visible' });
            }

            this.show = function() {
            if (input.val() == '') label.css({ textIndent: 0, visibility: 'visible' })
            }

            // handlers
            input.focus(this.hide);
            input.keyup(function() {
                setTimeout(function() {
                if (input.val() != '') label.css({ textIndent: -10000, visibility: 'hidden' });
                else label.css({ textIndent: 0, visibility: 'visible' });
                }, 50);
            });
            input.change(function() {
                setTimeout(function() {
                if (input.val() != '') label.css({ textIndent: -10000, visibility: 'hidden' });
                else label.css({ textIndent: 0, visibility: 'visible' });
                }, 50);
            });
            input.blur(this.show);
            label.addClass(overClass).click(function() { input.focus() });

            if (input.val() != '') this.hide();
        }
    })
}
