/*
#
# fancylabel.jquery.js
#
# by Ludwig Pettersson
# <http://luddep.se>
#
 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
*/
 
(function() {
 
    var speed = 80,
        startColor,
        fadedColor;
    
    // Sometimes the first color animation wont fire, so we fire off one.
    $('html').animate({'backgroundColor': $('html').css('background-color')}, 1);
 
    jQuery.fn.fancyLabel = function(_fadedColor){
        $('p', this).each(function() {
            
            var p = $(this);
            p.addClass('fancy-label');
            
            var input = $('input', p),
                label = $('label', p);
            
            startColor = label.css('color');
            fadedColor = _fadedColor ? _fadedColor : "#eee";
            
            // Safari takes a few ms to actually set focus on the input
            label.click(function() {
                input.focus();
                return false;
            });
            
            input
                .focus(function() {
                    label.animate({'color': fadedColor}, speed);
                })
                .blur(function() {
                    if ($(this).val().length == 0)
                    {
                        label
                            .show(0)
                            .animate({'color': startColor}, speed);
                    }
                })
                .keydown(function(){
                    //setTimeout(function(){
                    //    if (input.val().length > 0)
                            label.hide(0);
                    //}, 0.0);
                });
                /*.keyup(function(){
                    if (!$(this).val() && label.css('display') != 'block')
                        label.fadeIn(50);
                });*/
        });
    }
    
})();