﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace('TexAgs.Web.UI.Extenders');

TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior = function(element) {
    TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.initializeBase(this, [element]);
    
    this._key = "";
    this._IsPrimary = false;
    this._clickHandler = Function.createDelegate(this, this._onclick);
}
TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.prototype = {
    initialize : function() {
        TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.callBaseMethod(this, 'initialize');
        $addHandler(this.get_element(), "click", this._clickHandler);
    },
    
    dispose : function() {
        if (this._key) {
            var keys = TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.Keys;
            var ar = keys[this._key];
            Array.remove(ar, this);
            this._key = null;
        }
        if (this._clickHandler) {
            $removeHandler(this.get_element(), "click", this._clickHandler);
            this._clickHandler = null;
        }
        TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.callBaseMethod(this, 'dispose');
    },
    
    get_IsPrimary : function() {
        return this._IsPrimary;
    },
    set_IsPrimary : function(value) {
        if (this._IsPrimary != value) {
           this._IsPrimary = value;
           this.raisePropertyChanged('IsPrimary');
        }
    },    
    
    get_Key : function() {
        return this._key;
    },
    set_Key : function(value) {
        var keys = TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.Keys;
        if(value != this._key) {
            if(this._key) {
                var ar = keys[this._key];
                Array.remove(ar, this._key);
            }
            this._key = value;
            if(value) {
                var ar = keys[this._key];
                if(ar == null) {
                    ar = keys[this._key] = [];
                }
                Array.add(ar, this);
            }
        }
    },
    
    _onclick : function() {
        var element = this.get_element();
        var keys = TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.Keys;
        if(this._key)
        {
            var ar = keys[this._key];
            var t = this;
            var primaryElement = element;
            var foundCheckedNonPrimary = false;
            Array.forEach(ar, function(b)
				{
					if(b != t)
					{
						if (b._IsPrimary)
						{
							primaryElement = b.get_element();
						}
						if ((t._IsPrimary && element.checked && b.get_element().checked) ||
							(!t._IsPrimary && element.checked && b._IsPrimary && b.get_element().checked))
						{
							b.get_element().checked = false;
							$common.tryFireEvent(b.get_element(), "change");
						}
						else if (!b._IsPrimary && b.get_element().checked)
						{
							foundCheckedNonPrimary = true;
						}
						
					}
					else if (!t._IsPrimary && element.checked)
					{
						foundCheckedNonPrimary = true;
					}
	            });	           
	         
			if (!primaryElement.checked && !foundCheckedNonPrimary)
			{
				primaryElement.checked = true;
				$common.tryFireEvent(primaryElement, "change");	
			}   	            
	            
        } 
        
		this.raiseChecked(new TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxEventArgs(element, this._key, this._IsPrimary));
    },
    
    add_checked : function(handler) {
        this.get_events().addHandler('checked', handler);
    },
    remove_checked : function(handler) {
        this.get_events().removeHandler('checked', handler);
    },
    raiseChecked : function(eventArgs) {       
        var handler = this.get_events().getHandler('checked');
        if (handler) {
            handler(this, eventArgs);
        }
    }
}    
TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.registerClass('TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior', Sys.UI.Behavior);
TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxBehavior.Keys = {};

TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxEventArgs = function(checkbox, key, isPrimary) {
    TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxEventArgs.initializeBase(this);
    
    this._key = key;
    this._checkbox = checkbox;
    this._IsPrimary = isPrimary
}
TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxEventArgs.prototype = {
    get_checkbox : function() {
        return this._checkbox;
    },
    
    get_key : function() {
        return this._key;
    },
    
    get_IsPrimary : function() {
        return this._IsPrimary;
    }    
}
TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxEventArgs.registerClass('TexAgs.Web.UI.Extenders.PrimaryExclusiveCheckBoxEventArgs', Sys.EventArgs);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
