

// ****************

function sideSpacer(id, parent){
  this.write = sideSpacerWrite;
  this.id = id;
  this.parent = parent;
  this.count  = 0;
}


function sideItemWrite(classname){
  var result;
  result = this.owner.btns["Out"].body(this, classname);
  return result;

}


// ****************

function sideItemSetOwner(owner){
  var item;
  this.owner = owner;
  if (this.owner != null) this.owner.allItems[this.id] = this;
  for ( item in this.items){
    this.items[item].setOwner(owner);
  };
}


function sideItemAddItem(item){
  this.items[item.id] = item;
  item.parent = this;
  item.setOwner(this.owner);
  this.count = this.count + 1;
}


function sideItem(href, target, text, id){
  this.items = new Array();
  this.addItem = sideItemAddItem;
  this.write = sideItemWrite;
  this.setOwner = sideItemSetOwner;

  this.href = href;
  this.target = target;
  this.text = text;
  this.id = id;
  this.count = 0;
  this.parent = null;
  this.owner = null;
  this.status = 0;
}


//**********************
//**********************
//**********************

function recDepth(i, depth){

  var md = depth;
  var d;
  var item;

  for( item in i.items){

	 d =  recDepth(i.items[item], depth +1);
	 if (d > md ) md = d;
   };

   return md;

}

function sideBarDepth(){
  var i = this.allItems["ROOT"];

  return recDepth(i, 0);

}


function recMap(i, depth, maxCols){

var x = -1;
var before = depth;
var after  = maxCols - 4 - depth;
var item;
var result = ""
  
  for( item in i.items){
     if (x != -1)  result = result + "<td colspan=\""+maxCols+"\" height=\"5px\"></td></tr>";
     result = result + "<tr>";
     for (x = 0; x < before; x++) result = result + "<td>&nbsp</td>";
     
     result = result + "<td colspan=\"4\" class=\"maplevel"+depth+"\"><a href=\""+i.items[item].href+'?menueid='+i.items[item].id+"\"";
     if (i.items[item].target != '') result = result + ' target="'+i.items[item].target+'"';
     result = result + ">"+i.items[item].text+"</a></td>";

     if(after != 0) result = result + "<td colspan=\""+ after+"\">&nbsp</td>";
     result = result +"</tr>";

//******
     result = result +  this.map_helper(i.items[item], depth +1, maxCols);
   };
  return result;
}


function sideBarMap(){


  var x, mcols;
  var i = this.allItems["ROOT"];
  var tiefe =  this.depth(i, 0);
  var mcols =  4 + tiefe -1;
  var result = "";


  result = result + "<Table border=0 >";

  for(x = 0; x < mcols; x++) result = result + "<td width=\"50px\"></td>";
  result = result + "</tr>";
  result = result + this.map_helper(i, 0, mcols);
  result = result + "</table>";
  
  return result;

}


function sideBarSetCurrentMenue(itemID){
   var doc;

   if (itemID == "")  itemID = this.currentItem.id;

  

   if(this.allItems[itemID].count > 0){               
    this.currentItem.status = 0;
      this.currentItem = this.allItems[itemID];
   this.currentItem.status = 1;

      this.currentMenue = this.allItems[itemID]; //.parent;        
      if (this.menueOut != "") {
         eval(this.menueOut+'.history').go(0) ;
      } 
      else { 
	history.go(0);
      };
   } 
   else {
     if(this.allItems[itemID].parent != this.currentMenue){
       this.currentItem.status = 0;
       this.currentItem = this.allItems[itemID];
       this.currentItem.status = 1;
       this.currentMenue = this.allItems[itemID].parent;
       if (this.menueOut != "") {
         eval(this.menueOut+'.history').go(0) ;
       } 
       else { 
	 history.go(0);
       };
     }
     else {
       setCurrentItem(itemID, this.menueOut);
     }
  }   
}


function sideBarWrite(itemID){
  var i;
  
  var item;
  var pmen = "";
  var doc;
  

  if (this.menueOut != "") { 
     doc = eval(this.menueOut+'.document') 
  }
  else { 
    doc = document;
  }

  i =  this.allItems[itemID];
  // -- doc.write(this.owner.btns["Out"].top(i));
  doc.write(this.owner.btns["Out"].top(i))


  for (p = i.parent; p.parent != null; p = p.parent){
    //doc.write(p.text)
    if (pmen != "") pmen = this.owner.btns["Out"].between() + pmen;
    pmen = p.write() + pmen;
  }
  doc.write(pmen);

  doc.write(this.btns["Out"].horLine());
  doc.write(i.write('selected'));  
  doc.write(this.btns["Out"].horLine());

  for ( item in i.items){    
    doc.write(i.items[item].write());
    doc.write(this.owner.btns["Out"].between())
  };  
  doc.write(this.owner.btns["Out"].bottom(i));
}


function sideBarAddItem(item){
  this.items[item.id] = item;
  item.parent = this;
  item.setOwner(this.owner);
  this.items[item.id].setOwner(this.owner);
  this.owner.allItems[item.id] = item;
  if (this.currentItem == null){ 
     this.currentItem = item; 
     item.status = 1;
  }
  this.count = this.count + 1;
}


// ****************

function sideBar(href,target,text){

   this.items = new Array();
   this.allItems = new Array();
   this.btns = new Array();

   this.addItem = sideBarAddItem;
   // this.addSpacer = sideBarAddSpacer;
   this.write = sideBarWrite;
   this.depth = sideBarDepth;
   this.map = sideBarMap;
   this.map_helper = recMap;
   this.setCurrentMenue = sideBarSetCurrentMenue;

   this.width = "";
   this.howToSetOwner=""
   this.currentItem = null;
   this.currentMenue = null;

   this.menueOut = ""
   this.subMenueImage ="";
   this.count = 0;
   this.parent = null;
   this.owner = this;
   this.id = 'ROOT';
   this.allItems["ROOT"] = this;
}



