Type.registerNamespace('ordoMANIA.Web.UI.Controls');

ordoMANIA.Web.UI.Controls.ListSelectionBehavior = function(element) {

    ordoMANIA.Web.UI.Controls.ListSelectionBehavior.initializeBase(this, [element]);
    this._listid = null;
    this._entityid = null;
    
    //the controls id
    this._iconid = null;
    this._checkboxid = null;
    this._labelid = null;
    
    //The webservice info
    this._servicePath = null;
    this._serviceMethod = null;
    
    //The control themself
    this._checkbox = null;
    this._icon = null;
    this.label = null;
    
    //Delagetes
    this._clickHandler = null;
    this._checkHandler = null;
    
}

ordoMANIA.Web.UI.Controls.ListSelectionBehavior.prototype = {


    // the list id of the check box.
    get_listid: function() {
        return this._listid;
    },
    set_listid: function(value) {
        this._listid = value;
    },
    //The curent entityid
    get_entityid: function() {
        return this._entityid;
    },
    set_entityid: function(value) {
        this._entityid = value;
    },
    
    //the client id of the checkbox
    get_checkboxid: function() {
        return this._checkboxid;
    },
    set_checkboxid: function(value) {
        this._checkboxid = value;
    },
    
    //The clientid of the icon
    get_iconid: function() {
        return this._iconid;
    },
    set_iconid: function(value) {
        this._iconid = value;
    },
    
    //the client id of the label
    get_labelid: function() {
        return this._labelid;
    },
    set_labelid: function(value) {
        this._labelid = value;
    },
    
    //The web service info
    get_servicepath : function() {
        return this._servicePath;
    },
    set_servicepath : function(value) {
        this._servicePath = value;
    },
    get_servicemethod : function() {
        return this._serviceMethod;
    },
    set_servicemethod : function(value) {
        this._serviceMethod = value;
    },
    

    //events
    //Event used when toggling the check box
    clickHandler : function(){
        //We toggle the checkbox
        this._checkbox.checked = !this._checkbox.checked;
        //we call the server
        this.updateServer(this._listid,this._entityid,this._checkbox.checked);
    },
    
    onChangeHandler : function(){
        //We call the server
        this.updateServer(this._listid,this._entityid,this._checkbox.checked);
    },
    
    updateServer : function(listid,entityid,checked){
        Sys.Net.WebServiceProxy.invoke(this._servicePath, this._serviceMethod, false,
            {listid:listid,entityid:entityid,checked:checked},
            Function.createDelegate(this, this.onMethodCompleted), Function.createDelegate(this, this.onMethodError));
    },
    
    onMethodCompleted : function(result, userContext, methodName){
        //alert('success');
    },
    
    onMethodError : function(webServiceError, userContext, methodName){
        //alert('error');
    },
    
    initialize: function() {

        ordoMANIA.Web.UI.Controls.ListSelectionBehavior.callBaseMethod(this, 'initialize');
        //Create the delegate for the icon and the label
        this._clickHandler = Function.createDelegate(this, this.clickHandler);
        //Create the delaegate for the checkbox
        this._checkHandler = Function.createDelegate(this, this.onChangeHandler);
        //Initialize the controls
        if (this._checkboxid) {
            this._checkbox = $get(this._checkboxid, this.get_element());
            if (this._checkbox) {
                Sys.UI.DomEvent.addHandler(this._checkbox, 'click', this._checkHandler);}}
        if (this._iconid) {
            this._icon = $get(this._iconid, this.get_element());
            if (this._icon) {
                Sys.UI.DomEvent.addHandler(this._icon, 'click', this._clickHandler);}}
        if (this._labelid) {
            this._label = $get(this._labelid, this.get_element());
            if (this._label) {
                Sys.UI.DomEvent.addHandler(this._label, 'click', this._clickHandler);}}      
        
    },
    
    // Release resources before control is disposed.
    dispose: function() {
        ordoMANIA.Web.UI.Controls.ListSelectionBehavior.callBaseMethod(this, 'dispose');
    }

    
    
}
ordoMANIA.Web.UI.Controls.ListSelectionBehavior.registerClass('ordoMANIA.Web.UI.Controls.ListSelectionBehavior', Sys.UI.Control);

// 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();