﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace('TCGHL.Framework.Web.WebControls');

TCGHL.Framework.Web.WebControls.FocusExtenderBehavior = function(element) {
    TCGHL.Framework.Web.WebControls.FocusExtenderBehavior.initializeBase(this, [element]);
    this._labelControlID = null;  
    this._hintControlID = null;
    
    this._labelControl = null;
    this._hintControl = null;
    
    this._labelControlCssClass = null;
    this._hintControlCssClass = null;
    this._targetControlCssClass = null;
    
    this._labelControlFocusCssClass = null;
    this._hintControlFocusCssClass = null;
    this._targetControlFocusCssClass = null;
    
    this._focusHandler = null;
    this._blurHandler = null; 
    
   }
TCGHL.Framework.Web.WebControls.FocusExtenderBehavior.prototype = {

    initialize : function() {
        
        TCGHL.Framework.Web.WebControls.FocusExtenderBehavior.callBaseMethod(this, 'initialize');
        
        var e = this.get_element(); 
        this._targetControlCssClass = e.className;
 
        if(this._labelControlID)
        {
            this._labelControl =  $get(this._labelControlID);
             if (!this._labelControl) 
             {
                throw Error.argument('LabelControlID', "No this control ID!", this._labelControlID);
             }
             else
             {
                this._labelControlCssClass = this._labelControl.className;
             }
        }
        if(this._hintControlID)
        {
            this._hintControl =  $get(this._hintControlID);
             if (!this._hintControl) 
             {
                throw Error.argument('HintControlID', "No this control ID!", this._hintControlID);
             }
             else
             {
                this._hintControlCssClass = this._hintControl.className;
             }
        }
        
        var focused = false;
        
        var clientState = TCGHL.Framework.Web.WebControls.FocusExtenderBehavior.callBaseMethod(this, 'get_ClientState');
        if (clientState != null && clientState != "") {
            focused = (clientState == "Focused");
            TCGHL.Framework.Web.WebControls.FocusExtenderBehavior.callBaseMethod(this, 'set_ClientState', null);
        }

        this._focusHandler = Function.createDelegate(this, this._onFocus);
        this._blurHandler = Function.createDelegate(this, this._onBlur);
        
        $addHandler(e, "focus", this._focusHandler);
        $addHandler(e, "blur", this._blurHandler);

        if (focused) {
            this._onFocus();
        } else {
            e.blur();
            this._onBlur();
        }
   },

   dispose : function() {
        var e = this.get_element(); 

        // Unhook from Sys.Preview.UI.TextBox if present
        if(e.control && this._propertyChangedHandler) {
            e.control.remove_propertyChanged(this._propertyChangedHandler);
            this._propertyChangedHandler = null;
        }        
        if (this._focusHandler) 
        {
            $removeHandler(e, "focus", this._focusHandler);
            this._focusHandler = null;
        } 
        if (this._blurHandler) 
        {
            $removeHandler(e, "blur", this._blurHandler);
            this._blurHandler = null;
        }         
        TCGHL.Framework.Web.WebControls.FocusExtenderBehavior.callBaseMethod(this, 'dispose');        
    },

   _onFocus : function() { 
        var e = this.get_element(); 
        if(this._targetControlFocusCssClass)
        {
            e.className = this._targetControlFocusCssClass;
        }
        if(this._hintControl && this._hintControlFocusCssClass)
        {
            this._hintControl.className = this._hintControlFocusCssClass;
        }
        if(this._labelControl && this._labelControlFocusCssClass)
        {
            this._labelControl.className = this._labelControlFocusCssClass;
        } 

        if(e.tagName == "SELECT")
        {
            e.click();
        }
        
    }, 

   _onBlur : function(){
        var e = this.get_element(); 
        if(this._targetControlFocusCssClass)
        {
            e.className = this._targetControlCssClass;
        }
        if(this._hintControl && this._hintControlFocusCssClass)
        {
            this._hintControl.className = this._hintControlCssClass;
        }
        if(this._labelControl && this._labelControlFocusCssClass)
        {
            this._labelControl.className = this._labelControlCssClass;
        } 
    } ,
   
    get_labelControlID : function(){ return this._labelControlID;},
    set_labelControlID : function(value){ this._labelControlID = value;},
    
    get_hintControlID : function(){ return this._hintControlID;},
    set_hintControlID : function(value){ this._hintControlID = value;},
    
    get_labelControlFocusCssClass : function(){ return this._labelControlFocusCssClass;},
    set_labelControlFocusCssClass : function(value){ this._labelControlFocusCssClass = value;},
    get_hintControlFocusCssClass : function(){ return this._hintControlFocusCssClass;},
    set_hintControlFocusCssClass : function(value){ this._hintControlFocusCssClass = value;},
    get_targetControlFocusCssClass : function(){ return this._targetControlFocusCssClass;},
    set_targetControlFocusCssClass : function(value){ this._targetControlFocusCssClass = value;}
}

TCGHL.Framework.Web.WebControls.FocusExtenderBehavior.registerClass('TCGHL.Framework.Web.WebControls.FocusExtenderBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();