﻿Type.registerNamespace('BIT.WebControls');

BIT.WebControls.HintBehavior = function(element) 
{ 
	BIT.WebControls.HintBehavior.initializeBase(this, [element]);

	this.addProperty('cssClass', null);
	this.addProperty('text', null);
	
	this._hintCtl = null;
	this._visibleBlock = false;
	this._timerId = null;
}

BIT.WebControls.HintBehavior.prototype = 
{
	initialize : function() 
	{
		BIT.WebControls.HintBehavior.callBaseMethod(this, 'initialize');

        var elm = this.get_element();
    
        this._hintCtl = document.createElement('div');
        this._hintCtl.innerHTML = this.text;
        
        this._hintCtl.className = (this.cssClass || '');
        this._hintCtl.style.display = 'none';
        

        
        Sys.UI.DomElement.enableSelection(this._hintCtl, false);
        
		$addHandlers(elm, 
					{ 
						'change' : this._onBlur,
						'focus' : this._onFocus,
						'blur' : this._onBlur 
					},
					this);

        while (typeof(Sys.UI.DomElement.getAttribute(elm, 'adapted')) != 'undefined') 
            elm = elm.parentNode;
        
        elm.parentNode.insertBefore(this._hintCtl, elm);

					
        $addHandlers(this._hintCtl, 
					{ 
						'click' : this._onClick
					},
					this);
					
		$setInterval(this, '_timerId', this._checkValue , 200);					
	},

	dispose : function() 
	{
		$clearInterval(this, '_timerId');
		
		$clearHandlers(this.get_element());
		
		$clearHandlers(this._hintCtl);
		$removeFromParent(this._hintCtl);
		
		this._hintCtl = null;
		
		BIT.WebControls.HintBehavior.callBaseMethod(this, 'dispose');
	},

    hide : function()
    {
		if (!this._visibleBlock)
			return;
			
		this._visibleBlock = false;
		
        if (this._hintCtl)
            this._hintCtl.style.display = 'none';
    },
	
	show : function()
    {
		if (this._visibleBlock)
			return;
			
		this._visibleBlock = true;
		
        if (this._hintCtl)
            this._hintCtl.style.display = '';
    },

    _onClick : function(e) 
	{
	    this.hide();
	    var elm = this.get_element();
	    
	    if (elm && elm.focus)
	    {
	        try
	        {
	            elm.focus();
	        }
	        catch(e)
	        {
	        }
	    }
	    
	    elm = null;
	},
	
	_onFocus : function(e) 
	{
	    this.hide();
	    
	    $clearInterval(this, '_timerId');
	},

	_checkValue: function()
	{
		var elm = this.get_element();
	    
	    if (elm)
	    {
	        if ((elm.value || '').length)
				this.hide();
			else
				this.show();
	    }
	    
	    elm = null;
	},
	
	_onBlur : function(e) 
	{
	    var elm = this.get_element();
	    
		this._checkValue();
		
		$setInterval(this, '_timerId', this._checkValue , 200);
		
		elm = null;
	}
}

BIT.WebControls.HintBehavior.registerClass('BIT.WebControls.HintBehavior', Sys.UI.Behavior);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();