Type.registerNamespace("CZScripts");

CZScripts.RecyclingEditor = function(element) {    
    CZScripts.RecyclingEditor.initializeBase(this, [element]);   
    this.RubbishInputs = "";
    this.RubbishMultipliers = "";
    
    this.RecycleInputs = "";
    this.RecycleProportions = "";

    this._RubbishInputs = [];
    this._RubbishMultipliers = [];

    this._RecycleInputs = [];
    this._RecycleProportions = [];

    this.RecycleCO2Factor = 0.0;
}

CZScripts.RecyclingEditor.prototype = {

    initialize: function() 
    {       
      CZScripts.RecyclingEditor.callBaseMethod(this, 'initialize');

      // subscribe to recycling update events      
      this.addRecycleHandlers();
      
      // subscribe to input update events      
      this.addRubbishHandlers();
      
      // trigger updates to synch co2 values
      this.onRubbishUpdated(null);
      this.onRecycleUpdated(null);

    },
    
    // Release resources before control is disposed.
    dispose: function() {   
        this.removeRecycleHandlers();
        this.removeRubbishHandlers();        
        CZScripts.RecyclingEditor.callBaseMethod(this, 'dispose');
    },   

    get_RubbishInputs: function() { return ""; },
    set_RubbishInputs: function(value)
    {
      this._RubbishInputs = value.split(";");
    },

    get_RubbishMultipliers: function() { return ""; },
    set_RubbishMultipliers: function(value)
    {
      this._RubbishMultipliers = value.split(";");
    },

    get_RecycleInputs: function() { return ""; },
    set_RecycleInputs: function(value)
    {
      this._RecycleInputs = value.split(";");
    },
    
    get_RecycleProportions: function() { return ""; },
    set_RecycleProportions: function(value)
    {
      this._RecycleProportions = value.split(";");
    },
    
    addRubbishHandlers: function()
    {
       // create delegate for event, so we get the right this pointer
       this.kgDelegate = Function.createDelegate(this, this.onRubbishUpdated);

       if (this._RubbishInputs == null) 
       {
         alert("no inputs specified for recycling");            
         return;
       }


       for (var i=0; i < this._RubbishInputs.length; i++)
       {
           $find(this._RubbishInputs[i]).add_updated(this.kgDelegate);
       }
    },
    
    removeRubbishHandlers: function()
    {  
       if (this._RubbishInputs == null) 
       {
         alert("no inputs specified for recycling");            
         return;
       }
          
          
       for (var i=0; i < this._RubbishInputs.length; i++)
       {
         var src = $find(this._RubbishInputs[i]);
         if (src != null) src.remove_updated(this.kgDelegate);
       }
    },   
   
    addRecycleHandlers: function()
    {
       // create delegate for event, so we get the right this pointer
       this.recDelegate = Function.createDelegate(this, this.onRecycleUpdated);

       if (this._RecycleInputs == null) 
       {
         alert("no inputs specified for recycling");            
         return;
       }

       for (var i=0; i < this._RecycleInputs.length; i++)
       {
         $addHandler($get(this._RecycleInputs[i]), 'click', this.recDelegate);
       }
    },
    
    removeRecycleHandlers: function()
    {  
       if (this._RecycleInputs == null) 
       {
         alert("no inputs specified for recycling");            
         return;
       }

       for (var i=0; i < this._RecycleInputs.length; i++)
       {
         $removeHandler($get(this._RecycleInputs[i]), 'click', this.recDelegate);
       }
       
       delete this.recDelegate;
    },   

    onRubbishUpdated: function(e) {       
       this.recalc();
    },
    
    onRecycleUpdated: function(e) 
    {                   
      this.recalc();
    },
    
    recalc: function()
    {
       var proportion = 0;
       for (var i=0; i < this._RecycleInputs.length; i++)
       {
         if ($get(this._RecycleInputs[i]).checked)
         {
            proportion = this._RecycleProportions[i];
         }
       }

       var calcCo2 = 0.0;
       
       for (var j=0; j < this._RubbishInputs.length; j++)
       {
         var editor = $find(this._RubbishInputs[j]);
         if (editor.get_visible())
         {
            var val = this.parseDouble(editor.get_InputValue());
            if (!isNaN(val))
               calcCo2  = calcCo2 + (this._RubbishMultipliers[j] * val * proportion * this.RecycleCO2Factor ); 
         }
       }
       

      if (calcCo2 == 0)
         this.set_CO2(Number.NaN);
      else
         this.set_CO2(calcCo2);
    }    

}

CZScripts.RecyclingEditor.registerClass('CZScripts.RecyclingEditor', CZScripts.CO2Editor);






