﻿Type.registerNamespace("majax");

majax.CheckGroup = function() {
    majax.CheckGroup.initializeBase(this);
}

majax.CheckGroup.prototype = {

    initialize : function() {
        majax.CheckGroup.callBaseMethod(this, 'initialize');

        //  attach the plugin
        Sys.UI.Control.prototype.checkGroup = this._checkGrouped;
    },


    dispose : function() {

        majax.CheckGroup.callBaseMethod(this, 'dispose');
    },
 
    _checkGrouped : function(group) {
    
        var checkAllClickHandler = Function.createDelegate(this, function(){
            var checkAll = this.get_element();
            //  when the click occurs set the value of the
            //  checkboxes
            group.each(function(e){ e.get_element().checked = checkAll.checked; });
        });
        $addHandler(this.get_element(), 'click', checkAllClickHandler);
        
        var groupClickHandler = Function.createDelegate(this, function(){
            var checkAll = this.get_element();
            checkAll.checked = true;
            //  when the click occurs set the value of the
            //  checkboxes
            group.each(function(e){ checkAll.checked &= e.get_element().checked; });
        });        
    
        group.each(function(e){ $addHandler(e.get_element(), 'click', groupClickHandler); });
    }
}

//  register the class
majax.CheckGroup.registerClass('majax.CheckGroup', Sys.Component);

//  create the singleton
$create(majax.CheckGroup, null, null, null);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();