/****************************************

File: foodGroups.js
Created Friday, 09 August 2002 at 9:30 PM
By Hawkeye King a.k.a. The Loser At Work At 9PM Friday Night


  There are two objects that need to be implemented as groups: Object Groups and Layers.

  We don't actually have to implement layers for the prototype but when we do we can implement them with the fun name of "foodGroup"!

  As for ObjectGroups we need to cross-reference grouped objects.  I don't think it's such a good idea to do this referencing within the SVG realm.  I think that abstracting that to JS is a better idea.
=========================================
Modified 8-21 by Hawkeye

* Store/Recall functions have been moved to refrigerator.js
* Fixed ObjectGroup copyconstructor calls.
* Append function now inserts at front of group.

=========================================
Modified 9-4 by Hawkeye

* a group is now able to handle events.  This was necessary for overwriting
some inheritance of event handlers.

*****************************************/

ObjectGroup.prototype   = new EventHandler();
ObjectGroup.constructor =  ObjectGroup;
ObjectGroup.superclass  = EventHandler.prototype;

function ObjectGroup() {
  if ( arguments.length > 0){
    this.copyconstructor( arguments[0] );
  } else if (svgDoc){
    this.init();
  }
}
ObjectGroup.prototype.init = function() {
  //  debug("init objGroup");
  this.objectList = new Array();
  this.svgGroup = svgDoc.createElement("g");
  drawingGroup.appendChild( this.svgGroup );
}

ObjectGroup.prototype.addObject = function ( obj ){
  //  debug("obj:"+obj);
  if ( obj.group ){
    obj.group.removeObject( obj );
  }

  this.objectList.push( obj );
  obj.group = this;
  obj.svgNode.parentNode.removeChild( obj.svgNode );
  this.svgGroup.insertBefore( obj.svgNode , this.svgGroup.firstChild );
}
ObjectGroup.prototype.removeObject = function( obj ){
  for(var i=0;i<this.objectList.length;i++) {
    if (this.objectList[i] == obj ){
      this.objectList.splice( i, 1);
      return 1;
    }
  }
  return 0;
}
ObjectGroup.prototype.copyconstructor = function( svgNode ){
  this.objectList = new Array();
  this.svgGroup = svgNode;
 	// drawingGroup.appendChild( this.svgGroup );
}
ObjectGroup.prototype.destructor = function(){
  for (var d=this.objectList.pop();d;d=this.objectList.pop()){
    d.destructor();
  }
  delete this.objectList;
  delete this.svgGroup;
}

// GroupTool is necessary for rebuilding a group wrapper from SVG data. 
function GroupTool(){
}

GroupTool.prototype.copyconstructor = function( svgNode){
  var rebuildGroup = new ObjectGroup(svgNode); // The class function calls the copy constructor.
  return rebuildGroup;
}
var gTool = new GroupTool( "true" );


