﻿Type.registerNamespace('SI3SI.SocleTechnique.Web.Ajax');

SI3SI.SocleTechnique.Web.Ajax.ControlAsContextKeyBehavior = function(element) {

    SI3SI.SocleTechnique.Web.Ajax.ControlAsContextKeyBehavior.initializeBase(this, [element]);

    this._targetBehaviorValue = null;
    this._useMultiTargetControlId = null;
    this._otherTargetControlId = null;
    this._targetControlsContextKeyName = null;
    this._arrTargetControlsContextKeyName = null;
    this._otherTargetControl = null;
    this._delimiterCharacters = '|';
    
    this._changeHandler = null;
}

SI3SI.SocleTechnique.Web.Ajax.ControlAsContextKeyBehavior.prototype = {

    initialize : function() {
        SI3SI.SocleTechnique.Web.Ajax.ControlAsContextKeyBehavior.callBaseMethod(this, 'initialize');

        this._changeHandler = Function.createDelegate(this, this._onChange);

        this._otherTargetControl = new Array();

        var element = this.get_element();
        if (element)
        {
            this._otherTargetControl[0] = element;
            $addHandler(element, "change", this._changeHandler);
        }
        
        this._initOtherTargetControl();
        
        this._onChange(); //fire once for init
    },

    dispose : function() {
        var element = this.get_element();
        if (element) {
            $removeHandler(element, "change", this._changeHandler);
        }
        
        this._targetBehaviorValue = null;
        
        this._changeHandler = null;

        SI3SI.SocleTechnique.Web.Ajax.ControlAsContextKeyBehavior.callBaseMethod(this, 'dispose');
    },
    
    _onChange: function() {
        var contextKeyContent = this._getContextKeyContent();
        
        if (this._targetBehaviorValue)
        {
            var targetBehavior = $find(this._targetBehaviorValue);
            
            if (targetBehavior)
            {
                targetBehavior.set_contextKey(contextKeyContent);
            }
        }
    },
    
    _initOtherTargetControl : function() {
        if (this._useMultiTargetControlId && this._otherTargetControlId)
        {
            var arrOtherControlId = this._otherTargetControlId.split(',');
            
            this._arrTargetControlsContextKeyName = this._targetControlsContextKeyName.split(',');
            
            var i = 0;
            for (i=0; i < arrOtherControlId.length; i++)
            {
                var currentOtherControlId = arrOtherControlId[i];
                var currentOtherControl = $get(currentOtherControlId);
                this._otherTargetControl[i+1] = currentOtherControl;
                
                if (currentOtherControl)
                {
                    $addHandler(currentOtherControl, "change", this._changeHandler);
                }
            }
        }
    },

    _getContextKeyContent : function() {
        if (!this._useMultiTargetControlId)
        {
            var element = this.get_element();
            if (element)
            {
                var textTarget = element.value;
                if (textTarget && textTarget.length > 0)
                {
                    var varName = "";
                    if (this._targetControlsContextKeyName!=null && this._targetControlsContextKeyName!="")
                        varName = this._targetControlsContextKeyName + "=";
                    return varName + textTarget;
                }
            }
        }
        
        if (this._useMultiTargetControlId && this._otherTargetControl)
        {
            var contextKeyContent = '';
            var i = 0;

            for (i=0; i < this._otherTargetControl.length; i++)
            {
                var currentOtherControl = this._otherTargetControl[i];
                
                if (currentOtherControl)
                {
                    var currentOtherControlName = this._arrTargetControlsContextKeyName[i];
                    var currentOtherControlValue = currentOtherControl.value;
                    
                    if (contextKeyContent && contextKeyContent.length > 0)
                        contextKeyContent = contextKeyContent + this._delimiterCharacters;
                    
                    contextKeyContent = contextKeyContent + currentOtherControlName + '=';
                    
                    if (currentOtherControlValue)
                    {
                        contextKeyContent = contextKeyContent + currentOtherControlValue;
                    }
                }
            }
            
            return contextKeyContent;
        }
        
        return null;
    },
    get_TargetBehavior : function() {
        return this._targetBehaviorValue;
    },
    set_TargetBehavior : function(value) {
        this._targetBehaviorValue = value;
    },
    get_UseMultiTargetControlId : function() {
        return this._useMultiTargetControlId;
    },
    set_UseMultiTargetControlId : function(value) {
        this._useMultiTargetControlId = value;
    },
    get_OtherTargetControlId : function() {
        return this._otherTargetControlId;
    },
    set_OtherTargetControlId : function(value) {
        this._otherTargetControlId = value;
    },
    get_TargetControlsContextKeyName : function() {
        return this._targetControlsContextKeyName;
    },
    set_TargetControlsContextKeyName : function(value) {
        this._targetControlsContextKeyName = value;
    },
    get_DelimiterCharacters : function() {
        return this._delimiterCharacters;
    },
    set_DelimiterCharacters : function(value) {
        this._delimiterCharacters = value;
    }
}

SI3SI.SocleTechnique.Web.Ajax.ControlAsContextKeyBehavior.registerClass('SI3SI.SocleTechnique.Web.Ajax.ControlAsContextKeyBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();