/**
 * mm8_Container.js
 * Autor: Matthias Eipeldauer, newmagic datensysteme gmbh
 * Datum: 19.9.2008
 *
 * Diese Klasse stellt eine Superklasse für Container dar.
 * Die Container können ohne Probleme in ein Array umgeformt werden (-> ToArray()).
 **/

include("mm8Atom.js");

function mm8Container(id) 
{
  // VERERBUNG
  this.superclass = new mm8Atom(id);
  for (x in this.superclass) (x!="superclass")?this[x]=this.superclass[x]:"";
  this.type.push("mm8Container");
  // VERERBUNG ENDE

  this.children = [];
  this.data = null;
  
  this.AppendChild = function(obj)
  {
    this.children[this.children.length] = obj;
  };
  
  // Diese Funktion "flatted" eine Objektstruktur und liefert alle Elemente als flaches Array zurück.
  // 
  this.ToArray = function()
  {
    this.returnValue = new Array();
    if (this.data != null) this.returnValue.push(this.data);
    for(iCount in this.children)
    {
      xChild = this.children[iCount];
      if (xChild.type && xChild.Is && xChild.Is("mm8Container"))
      {
        this.returnValue = this.returnValue.concat(xChild.ToArray());
      }
      else
      {
        this.returnValue.push(xChild);
      }
    }
    return this.returnValue;
  };
  
  this._ToString = this.ToString;
  this.ToString = function(param)
  {
    if (!param) return this._ToString();
    else
    {
      xTemp = this.ToArray();
      sReturn = "[";
      for(iItem in xTemp)
      {
        xItem = xTemp[iItem];
        sReturn += "{";
        // Jetzt alle Key-Value Pairs schreiben...
        for(sKey in xItem)
        {
          sReturn += "'" + sKey + "': '" + xItem[sKey] + "',";
        }
        rPos = sReturn.lastIndexOf(",");
        sReturn = (rPos > -1? sReturn.substring(0, rPos) : sReturn); // letzten "," killen
        sReturn += "},";
      }
      rPos = sReturn.lastIndexOf(",");
      sReturn = (rPos > -1? sReturn.substring(0, rPos) : sReturn); // letzten "," killen
      sReturn += "]";
      sReturn = sReturn.replace(/\{\},/g, "");
      return sReturn;
   }
  };
}


mm8Callback("mm8Container.js");
