/*  
 * (C)2010 Jan Bartel <barteljan@yahoo.de>
    GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007: http://www.gnu.org/licenses/gpl.html
*/

// functions for adding modules to a mootools-class implementing events and options
var Moodulize = {

  // a Hash containing all modules for registrated classes (one array for each class)
  classModuleHash : new Hash({})
  
  // a Hash containing all instances for registrated classes (one array for each class)
  ,classInstanceHash : new Hash({})
  
  // registers an instance of a class and modify it's options 
  // and events with all options all modules  
  ,registerInstance : function(className,instance)
  {
    //check if keys for this class exist,
    //create new array if key for this class is empty
    if(!Moodulize.classInstanceHash.get(className))
    {
      Moodulize.classInstanceHash.set(className,[]);
    }
    
    var contained = Moodulize.classInstanceHash.get(className).contains(instance);
    
    //push instance to the instance hash 
    //if it is not contained
    Moodulize.classInstanceHash.get(className).include(instance);
    
    //modfie instances options and events by all modules for this class
    if((Moodulize.classModuleHash.get(className)!=null) && (!contained))
    {
      Moodulize.classModuleHash.get(className).each(function(module)
      {
          instance.setOptions(module);
      });
    }
    
  }

  // registers a module ( a typical mootools option object)
  // of a class and modify the options and events
  // of all registered instances of this class 
  ,registerModule : function(className,module)
  {
    //check if keys for this class exist,
    //create new array if key for this class is empty
    if(!Moodulize.classModuleHash.get(className))
    {
      Moodulize.classModuleHash.set(className,[]);
    }
    
    var contained = Moodulize.classModuleHash.get(className).contains(module);
    
    //push module to the module hash 
    //if it is not contained
    Moodulize.classModuleHash.get(className).include(module);
      
    //modfie the options and events of all instances with this module 
    if((Moodulize.classInstanceHash.get(className)!=null) && (!contained) )
    {
      Moodulize.classInstanceHash.get(className).each(function(instance)
      {
        instance.setOptions(module);
      });
    }
  }

}
