var cColHigh32 = '#ffffff';
var cColHigh21 = '#fefeee';
var cColHigh10 = '#f5f4e2';
var cColShad32 = '#c2c1b2';
var cColShad21 = '#aeada0';
var cColShad10 = '#9c9b8f';
var cColLev0 = '#cccbbc';
var cColLev1 = '#e1e0cf';
var cColLev2 = '#f5f4e2';
var cColLev3 = '#fefeee';
var cColActive = '#ffcc33';
var gMenu = null;

function CBrowserInfo()
{
   this.code = 'unknown';
   this.version = 0;
   this.platform = 'Win';
   var ua = navigator.userAgent;
   var i = ua.indexOf('MSIE');
   if (i >= 0) {
      this.code = 'MSIE';
      this.version = parseFloat(ua.substring(i+5, i+9));
   } else {
      i = ua.indexOf('Opera');
      if (i >= 0) {
         this.code = 'OP';
         i = ua.indexOf(') ');
         this.version = parseFloat(ua.substring(i+2, i+6));
      } else {
         i = ua.indexOf('Mozilla/');
         if (i >= 0) {
            this.code = 'NS';
            this.version = parseFloat(ua.substring(i+8, i+12));
         }
      }
   }
   if (ua.indexOf('Mac') >= 0) {this.platform = 'Mac';}
   if (ua.indexOf('OS/2') >= 0) {this.platform = 'OS/2';}
   if (ua.indexOf('X11') >= 0) {this.platform = 'UNIX';}
   var c = this.code;
   var v = this.version;
   this.canCache = ((c == 'NS') && (v >= 3)) || ((c == 'MSIE') && (v >= 4)) || ((c == 'OP') && (v >= 3.2));
   this.canOnMouseOut = this.canCache;
   this.canFocus = ((c == 'NS') && (v >= 3)) || ((c == 'MSIE') && (v >= 3)) || ((c == 'OP') && (v >= 3.2));
   this.mustMoveAfterLoad = this.canCache;
   if (((c == 'NS') && (v >= 3)) || ((c == 'MSIE') && (v >= 4))) {
      this.reloadMethod = 'replace';
   } else {
      this.reloadMethod =  ((c == 'NS') && (v == 2.01) && (this.platform != 'Win')) ? 'timeout' : 'href';
   }
   this.hasDHTML = ((c == 'NS') && (v >= 4)) || ((c == 'MSIE') && (v >= 4));
   this.DHTMLRange = (c == 'MSIE') ? '.all' : '';
   this.DHTMLStyleObj = (c == 'MSIE') ? '.style' : '';
   this.DHTMLDivHeight = (c == 'MSIE') ? '.offsetHeight' : '.clip.height';
   this.hasCookie = (this.code == 'MSIE' && this.version < 4.0 && location.protocol != 'http:') ? false : true;
   this.hasDynamicImages = this.version > "3.02";
   this.hasAllObject = ((c == 'MSIE') && (v >= 4));
}
function CMenuEntry( mother, display, myURL, indent, outputWindow, newItem, parent, key )
{
   this.mother = mother;
   this.display = display;
   this.URL = myURL;
   this.indent = indent;
   this.outputWindow = outputWindow;
   this.newItem = newItem;
   this.parent = parent;
   this.key = key;   
}
function CCookie( window, expire )
{
   this.GetValue = CCookie_GetValue;
   this.SetValue = CCookie_SetValue;
   this.Read = CCookie_Read;
   this.ReadCookieValue = CCookie_ReadCookieValue;
   this.Write = CCookie_Write;
   this.values = new Array();
   this.window = window;
   this.expire = expire;
   this.CurrState = "currState";
   this.Selected = "selected";
   this.Notes = "notes";
   this.values[this.CurrState] = "";
   this.values[this.Selected] = "-1";
   this.values[this.Notes] = "0";
   this.Read();
}
function CCookie_GetValue( key )
{
   return this.values[key];
}
function CCookie_SetValue( key, value )
{
   this.values[key] = value;
}
function CCookie_Read()
{
   for ( var propName in this.values )
   {
      this.values[propName] = this.ReadCookieValue( propName );
   }
}
function CCookie_ReadCookieValue( key )
{
   var keyPattern = key + "=";
   var   keyLen = keyPattern.length;
   var   cLen = this.window.document.cookie.length;
   var   i = 0;
   while ( i < cLen )
   {
      var j = i + keyLen;
      if ( this.window.document.cookie.substring( i, j ) == keyPattern )
      {
         var cEnd = this.window.document.cookie.indexOf( ";", j );
         if ( cEnd == -1 )
         {
            cEnd = this.window.document.cookie.length;
         }
         return unescape( this.window.document.cookie.substring( j, cEnd ) );
      }
      ++i;
   }
   return this.values[key];
}
function CCookie_Write()
{
   var   expire = new Date();
   expire.setTime( expire.getTime() + this.expire );
   for ( var propName in this.values )
   {
      if ( propName != "length" )
      {
         this.window.document.cookie =
            propName + "=" + escape( this.values[propName] ) +
            "; expires=" + expire.toGMTString();
      }
   }
}
function CMenu( menuName, window )
{
   this.Add = CMenu_Add;
   this.Toggle = CMenu_Toggle;
   this.Explode = CMenu_Explode;
   this.Contract = CMenu_Contract;
   this.Sync = CMenu_Sync;
   this.ToggleNotes = CMenu_ToggleNotes;	   
   this.SelectEntry = CMenu_SelectEntry;
   this.SelectEntryIdx = CMenu_SelectEntryIdx;
   this.NextEntry = CMenu_NextEntry;
   this.PrevEntry = CMenu_PrevEntry;
   this.Initialize = CMenu_Initialize;
   this.GenerateTree = CMenu_GenerateTree;
   this.DetermineImage = CMenu_DetermineImage;
   this.OnMouseOver = CMenu_OnMouseOver;
   this.GetUrl = CMenu_GetUrl;
   this.GetGlobalUrl = CMenu_GetGlobalUrl;
   this.Close = CMenu_Close;
   this.NodeState = CMenu_NodeState;
   this.Reload = CMenu_Reload;
   this.SetLocation = CMenu_SetLocation;
   this.SetWindow = CMenu_SetWindow;
   this.Setbgcolor = CMenu_Setbgcolor;
   this.browserInfo = new CBrowserInfo;
   this.lastColor = "";
   this.total=0;
   this.db = new Array();
   this.window = window;
   this.menuName = menuName;
   this.lastParents = new Array();
   this.lastParents[0] = -1;
   this.imagePath = "../../images/";
   for(i=0; i < top.frames.length; ++i) {
      if (top.frames[i].name == "SAP_TEXT" || top.frames[i].name == "content" ) {
         this.contentFrame = top.frames[i];
      }
      else {
         for (j=0; j < top.frames[i].frames.length; j++) {
            if (top.frames[i].frames[j].name == "SAP_TEXT" || top.frames[i].frames[j].name == "content") {
               this.contentFrame = top.frames[i].frames[j];
            }
         }
      }
   }
   this.globalPrefix = "../../";
   this.normalPrefix = "../../";
   this.keyTable = new Array();
   this.notFound = this.normalPrefix + "global/unknown.htm";
   this.cookie = new CCookie( this.window.parent, 7*24*60*60*1000 );
}
function CMenu_SetWindow( window)
{
   this.window = window;
}
function CMenu_NodeState( idx )
{
   return this.cookie.GetValue( this.cookie.CurrState ).substring( idx, idx + 1 );
}
function CMenu_Close()
{
   top.location =    this.contentFrame.location;
}
function CMenu_OnMouseOver( idx )
{
   window.status = this.db[idx].display;
   return true;
}
function CMenu_Add(mother,display,myURL,indent,outputWindow,newItem,key)
{
   if ( mother )
   {
      if ( indent >= 0 )
      {
         this.lastParents[indent + 1] = this.total;
      }
   }
   this.keyTable[key] = this.total;
   this.db[this.total++] = new CMenuEntry(
      mother, display, myURL, indent, outputWindow, newItem, this.lastParents[indent], key
   );
}
function CMenu_Toggle(n)
{
   if (n >= 0)
   {
      var oldString = this.cookie.GetValue( this.cookie.CurrState );
      var newString = "";
      var expanded = parseInt( this.NodeState( n ) );
      newString += oldString.substring( 0, n );
      newString += (expanded ^ 1);
      newString += oldString.substring( n + 1, oldString.length );
      this.cookie.SetValue( this.cookie.CurrState, newString );
      this.cookie.Write();
   }
}
function CMenu_Explode()
{
   var   str = "";
   for ( var i = 0; i < this.db.length; ++i )
   {
      str += "1"
   }
   this.cookie.SetValue( this.cookie.CurrState, str );
   this.cookie.Write();
   this.Reload();
}
function CMenu_Contract()
{
   var   str = "";
   for ( var i = 0; i < this.db.length; ++i )
   {
      str += "0"
   }
   this.cookie.SetValue( this.cookie.CurrState, str );
   this.cookie.Write();
   this.Reload();
}
function CMenu_Sync()
{
	var	str = "";
	for ( var i = 0; i < this.db.length; ++i ) { 
		str += "0"; 
	}
	this.cookie.SetValue( this.cookie.CurrState, str );
    this.cookie.SetValue( this.cookie.Selected, -1 );
	this.cookie.Write();
    this.contentFrame.history.go(0);
}
function CMenu_ToggleNotes()
{
   if (this.cookie.GetValue( this.cookie.Notes ) == "0") {
       this.cookie.SetValue( this.cookie.Notes, "1" );   
   } 
   else {
       this.cookie.SetValue( this.cookie.Notes, "0" );
   }
   this.cookie.Write();
   this.contentFrame.history.go(0);
}
function CMenu_DetermineImage( idx )
{
   var imgName = this.imagePath;
   var currEntry = this.db[idx];   
   if ( currEntry.mother ) {
      imgName += (this.NodeState( idx ) == '1') ? "open_book" : "closed_book";
   }
   else {
      if (currEntry.outputWindow != "") {
         imgName += "change_book";
      }
      else {
         imgName += "page";
      }
   }
   return imgName + ".gif";
}
function CMenu_SelectEntry( key )
{
   var idx = this.keyTable[key];
   var selEntry = parseInt( this.cookie.GetValue( this.cookie.Selected ) );
   if (isNaN(selEntry) || selEntry < 0 || selEntry >= this.db.length || this.db[selEntry].key != key || selEntry != idx) {
      this.SelectEntryIdx(idx);
   }
}
function CMenu_SelectEntryIdx(idx)
{
   var lastSelectedEntry = parseInt( this.cookie.GetValue( this.cookie.Selected ) );
   if ( lastSelectedEntry != -1 ) {
      if ( this.browserInfo.hasAllObject ) {
         var obj = eval("this.window.document.all.line" + lastSelectedEntry);
         if ( obj )  obj.style.backgroundColor = this.lastColor;
      }
      else if ( this.browserInfo.hasDynamicImages ) {
         var obj = this.window.document["im"+lastSelectedEntry];
         if (obj) obj.src =  this.imagePath + "nselect.gif";
      }
   }
   if ( idx != null && this.GetUrl( idx ) != "" )
   {
      this.cookie.SetValue( this.cookie.Selected, idx );
      this.cookie.Write();
      if ( this.browserInfo.hasAllObject ) {
         var obj = eval("this.window.document.all.line" + idx);
         if (obj) {
            this.lastColor = obj.style.backgroundColor;
            obj.style.backgroundColor = cColActive; 
         }
      }
      else if ( this.browserInfo.hasDynamicImages )
      {
         var obj = this.window.document["im"+idx];
         if (obj) obj.src =  this.imagePath + "select.gif";
      }
      var   expandedFlag = false;
      var   i = this.db[idx].parent;
      while ( i != -1 )
      {
         if ( this.NodeState( i ) == '0' )
         {
            this.Toggle( i );
            expandedFlag = true;
         }
         i = this.db[i].parent;
      }
      if ( expandedFlag )
      {
         this.Reload();
      }
   }
   else
   {
      this.cookie.SetValue( this.cookie.Selected, -1 );
      this.cookie.Write();
   }
   if ( ! this.browserInfo.hasDynamicImages && this.browserInfo.hasCookie )
   {
      this.Reload();
   }
}
function CMenu_GetUrl( idx )
{
   var   url = this.notFound;
   if ( idx >= 0 && idx < this.db.length )
   {
      url = this.normalPrefix + this.db[idx].URL;
   }
   return url;
}
function CMenu_GetGlobalUrl( idx )
{
   var   url = this.notFound;
   if ( idx >= 0 && idx < this.db.length )
   {
      url = this.globalPrefix + this.db[idx].URL;
   }
   return url;
}
function CMenu_SetLocation( idx )
{
   var target = this.db[idx].outputWindow;
   this.SelectEntryIdx(idx);   
   if ( target == '' )
   {
      this.contentFrame.location = this.GetGlobalUrl( idx );
   }
   else
   {
      if ( target.substring( 0, 1 ) == '_' )
      {
         target = target.substring( 1, target.length );
      }
      top[target].location = this.GetGlobalUrl( idx );
   }
}
function CMenu_NextEntry()
{
   var idx = parseInt( this.cookie.GetValue( this.cookie.Selected ) ) + 1;
   if ( idx != -1 )
   {
      while ( idx < this.total )
      {
         if ( this.db[idx].URL != "" )
         {
            break;
         }
         ++idx;
      }
   }
   if ( idx < this.total )
   {
      this.SetLocation( idx );
   }
}
function CMenu_PrevEntry()
{
   var idx = parseInt( this.cookie.GetValue( this.cookie.Selected ) ) - 1;
   while ( idx > 0 )
   {
      if ( this.db[idx].URL != "" )
      {
         break;
      }
      --idx;
   }
   if ( idx >= 0 )
   {
      this.SetLocation( idx );
   }
}
function CMenu_Initialize()
{
   var isInit = true;
   var idxDef = -1;
   var str = this.cookie.GetValue( this.cookie.CurrState );
   if (this.contentFrame && this.contentFrame.GetLoio) {
      idxDef = this.keyTable[this.contentFrame.GetLoio()];
      var idxCur = parseInt( this.cookie.GetValue( this.cookie.Selected ) );
      if (idxDef != idxCur) str = "";
   }
   if ( str == "" || str.length != this.db.length )
   {
      var first = true;
      str = "";
      for ( i = 0; i < this.db.length; ++i )
      {
         if (first && this.db[i].mother) {
            str += "1";
            first = false;
         }
         else {
            str += "0";
         }
      }
      this.cookie.SetValue( this.cookie.CurrState, str );
      isInit = false;
   }
   str = this.cookie.GetValue( this.cookie.Selected );
   if ( !isInit || str == "" || str == "-1" )
   {
      this.cookie.SetValue( this.cookie.Selected, idxDef );
   }
   else
   {
   }
}
function CMenu_Reload()
{
   if ( this.browserInfo.hasCookie )
   {
      var url = this.window.parent.window.location.href;
      this.window.parent.window.location.href = url;
   }
}
function CMenu_GenerateTree()
{
   this.window.document.open();
   var Outline="";
   var   prevIndentDisplayed = 0;
   var   showMyDaughter = 0;
   var   lastIndent = 0;
   var   maxIndent = 3;
   for (var i = 0; i < this.db.length; i++) {
      var currEntry = this.db[i];
      var currIndent = currEntry.indent;
      var expanded = this.browserInfo.hasCookie ? this.NodeState( i ) : '1';
      if ((currIndent == 0 || currIndent <= prevIndentDisplayed ||
         (showMyDaughter == 1 && (currIndent - prevIndentDisplayed == 1)))) {
         if (currIndent > maxIndent) maxIndent = currIndent;
         prevIndentDisplayed = currIndent;
         showMyDaughter = expanded;
      }
   }
   var selIndex = parseInt( this.cookie.GetValue( this.cookie.Selected ) );
   Outline += "<TABLE width=100% border=0 cellpading=0 cellspacing=0 rightmargin=0>";
   for (var i = 0; i < this.db.length; i++)
   {
      var currEntry = this.db[i];
      var currIndent = currEntry.indent;
      var expanded = this.browserInfo.hasCookie ? this.NodeState( i ) : '1';
      var currTop = currEntry.outputWindow;
      if (currTop == "") { currTop = this.contentFrame.name; }
      if (
         (currIndent == 0 || currIndent <= prevIndentDisplayed ||
         (showMyDaughter == 1 && (currIndent - prevIndentDisplayed == 1)))
      )
      {
         Outline += this.Setbgcolor(currIndent, lastIndent, maxIndent, i, selIndex);
         Outline += pad( currIndent );
         imgName = this.DetermineImage( i );
         if ( ! currEntry.mother )
         {
            Outline += "<IMG SRC='" + this.imagePath + "blank.gif' BORDER=0>";
            Outline += "<IMG SRC='" + this.imagePath + "space.gif' BORDER=0>";
            Outline += "<IMG SRC='" + imgName + "' BORDER=0>";
         } else
         {
            Outline += "<A HREF='javascript:" + this.menuName + ".Reload()' onMouseOver='window.parent.status=";
            if ( expanded == '1' )
            {
               Outline += "\"Klicken Sie hier, um den Teilbaum zuzuklappen\";return true;'";
               Outline += "onClick='" + this.menuName + ".Toggle(" + i + ");'>";
               Outline += "<IMG SRC='" + this.imagePath + "arrow_open.gif' BORDER=0>";
               Outline += "<IMG SRC='" + this.imagePath + "space.gif' BORDER=0>";
               Outline += "<IMG SRC='" + imgName + "' BORDER=0>";
            } else
            {
               Outline += "\"Klicken Sie hier, um den Teilbaum aufzuklappen\";return true;'";
               Outline += "onClick='" + this.menuName + ".Toggle(" + i + ");'>";
               Outline += "<IMG SRC='" + this.imagePath + "arrow_closed.gif' BORDER=0>";
               Outline += "<IMG SRC='" + this.imagePath + "space.gif' BORDER=0>";
               Outline += "<IMG SRC='" + imgName + "' BORDER=0>";
            }
            Outline += "</A>";
         }
         if (this.browserInfo.hasAllObject) {
            Outline += "&nbsp;";
         }
         else if ( i == selIndex) {
            Outline += "<IMG SRC='" + this.imagePath + "select.gif' BORDER=0 name='im" + i + "'>";
         }
         else {
            Outline += "<IMG SRC='" + this.imagePath + "nselect.gif' BORDER=0 name='im" + i + "'>";
         }
         if (currEntry.URL == "" || currEntry.URL == null)
         {
            Outline += " " + currEntry.display;
         } else
         {
            Outline += "<A NAME='ref" + i + "' HREF='javascript:onClick=" + this.menuName +".SetLocation(" + i + ")'" ;
            Outline += " onMouseover='return " + this.menuName + ".OnMouseOver(" + i + ")'";
            Outline += ">" + currEntry.display + "</A>";
         }
         Outline += "</TD></TR>\n";
         prevIndentDisplayed = currIndent;
         showMyDaughter = expanded;
         if (this.db.length > 25)
         {
            this.window.document.write(Outline);
            Outline = "";
         }
         lastIndent = currIndent;
      }
   }
   Outline += "</TABLE>";
   Outline += "</FONT>";
   this.window.document.write(Outline);
   this.window.document.close();
}
function CMenu_Setbgcolor(cur, last, max, idx, selIndex)
{
   var result = "";
   var cIndent = cur - (max - 3);
   var lIndent = last - (max - 3);
   if (cIndent < 0) cIndent = 0;
   if (lIndent < 0) lIndent = 0;
   if (cIndent != lIndent) {
      result += "<TR><TD height=1 width=100% nowrap bgcolor=";
      if (cIndent > lIndent) {
         switch(cIndent) {
         case 1:
            result += cColShad10; break;
         case 2:
            result += cColShad21; break;
         case 3:
            result += cColShad32; break;
         default:
            result += cColLev0; break;
         }
      }
      else {
         switch(cIndent) {
         case 0:
            result += cColHigh10; break;
         case 1:
            result += cColHigh21; break;
         case 2:
            result += cColHigh32; break;
         default:
            result += cColLev0; break;
         }
      }
      result += "></TD></TR>\n";
   }
   else if (cur != last) {
      result += "<TR><TD height=1 width=100% nowrap bgcolor=";
      result += cColShad10 + "></TD></TR>\n";
      result += "<TR><TD height=1 width=100% nowrap bgcolor=";
      result += cColHigh10 + "></TD></TR>\n";
   }   
   result += "<TR><TD width=100% nowrap id=line" + idx + " bgColor=";
   if ( this.browserInfo.hasAllObject && idx == selIndex) {
      result += cColActive;
      switch(cIndent) {
      case 3:
         this.lastColor = cColLev3; break;
      case 2:
         this.lastColor = cColLev2; break;
      case 1:
         this.lastColor = cColLev1; break;
      default:
         this.lastColor = cColLev0 ; break;
      }
   }
   else {
      switch(cIndent) {
      case 3:
         result += cColLev3; break;
      case 2:
         result += cColLev2; break;
      case 1:
         result += cColLev1; break;
      default:
         result += cColLev0; break;
      }
   }
   result += ">";
   return result;
}
function pad( n )
{
   var result = "";
   for ( var i = 1; i <= n; ++i )
   {
      result += "&nbsp;&nbsp;&nbsp;&nbsp;";
   }
   return result;
}
function CreateMenu( window )
{
   gMenu = new CMenu("parent.gMenu", window);
}
