﻿(function($) {   
 $.fn.TextBoxHoverBehavior = function(options) {   
  
    var defaults = {   
        ignoredClass: 'Ignored',   
        overClass: 'Over',   
        focusedClass: 'Focused',   
        emptyClass: 'Empty',   
        emptyText: ''        
    };   
  
    var options = $.extend(defaults, options); 
    
    return this.each(function() { 
    
        var obj = $(this);
        var parent = obj.parent();
        
        var isEmpty = true;     
        var hasFocus = false;
        
        var modes = { 
            Ignored: 0, 
            Over:1, 
            Focused:2
        }
        
        initialize();
        
        function initialize() {
            ensureObjectType();
            registerHandlers();
            
            obj.attr('value', options.emptyText);
            changeStyle(modes.Ignored);
        }    
        
        function changeStyle(mode) {
        
            parent.removeClass();
            
            switch (mode) {
            
                case modes.Ignored:
                
                    parent.addClass(options.ignoredClass);
                    
                    if (isEmpty)
                        parent.addClass(options.emptyClass);
                
                    break;
                    
                case modes.Over:
                
                    parent.addClass(options.overClass);
                    
                    if (isEmpty)
                        parent.addClass(options.emptyClass);
                        
                    break;
                    
                case modes.Focused:
                    parent.addClass(options.focusedClass);
                    break;            
            }
        }
        
        function ensureObjectType() {
            
            if (obj.attr('nodeName') != "INPUT")                
                alert("[TextBoxHoverBehavior] Wrong object type, should be an 'input' element.");
        }     
        
        function registerHandlers() {
        
            // http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-MouseEvent          
            
            obj.bind('mouseover', function() {
                if (!hasFocus)
                    changeStyle(modes.Over);               
            });
            
            obj.bind('mouseout', function() {
                if (!hasFocus)
                    changeStyle(modes.Ignored);    
            });
            
            obj.bind('focus', function(e) {
                hasFocus = true;
                
                if (isEmpty)
                    obj.attr('value', '');
                    
                changeStyle(modes.Focused);                
            });
            
            obj.bind('blur', function() {
                hasFocus = false; 
                
                if (isEmpty)
                    obj.attr('value', options.emptyText);
                
                changeStyle(modes.Ignored)
            });
            
            obj.bind('change', function() {
                isEmpty = (obj.attr('value')) ? false : true;                             
            });
        }       
    });   
 };   
})(jQuery);  

jQuery(document).ready(function($) {   
    $('#searchBox').TextBoxHoverBehavior();
});   
