function writeMenu( data, class_name)
{
  o = eval( class_name+"=new Menu( '"+class_name+"');");
  o.menuDef = data["items"];
  var _Menu_PreloadImages = new Array();
  i = 0;
  for ( i2 in data["preload"])
  {
    _Menu_PreloadImages[i] = new Image();
    _Menu_PreloadImages[i++].src = data["preload"][i2];
  }
  o.e_gif = data["e_gif"];
  o.printMenu();
}

function Menu_setStatus( p, v){this.menu_items = this.menu_items.substr( 0, p) + v + this.menu_items.substr( p-(-1));this.setCookie( this.name+"menu_items", this.menu_items);}
function Menu_getStatus( p, default_){ 
	if ( this.menuDef[p]["on"] && default_ ) {
		return ( this.menuDef[p]["on"] == "1" )?1:0;
	}
	return this.menu_items.substr( p, 1);
}

function Menu_setCookie(name, value){var curCookie = name + "=" + escape(value);document.cookie = curCookie}
function Menu_getCookie(name){var prefix = name + "=";var cookieStartIndex = document.cookie.indexOf(prefix);if (cookieStartIndex == -1) return null;var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));}
function Menu_changeColor( color, k){c = this.getRGB( color);c[0] = c[0]*k;c[1] = c[1]*k;c[2] = c[2]*k;return this.makeRGB( c);};
function Menu_toHex( val, max){ val=Math.ceil( val); if ( max ) val = ( val > max)?max:val;res="";while( val > 0 ){ch = val%16;val = val>>>4;res = (( ch > 9 )?String.fromCharCode( ch+55):ch) + res;}return res;};
function Menu_makeRGB( color){res="";res = "#"+this.toHex( color[0], 255);res += this.toHex( color[1], 255);return res+this.toHex( color[2], 255);};
function Menu_getRGB( color){r = parseInt( color.substr( 1,2), 16);g = parseInt( color.substr( 3,2), 16);b = parseInt( color.substr( 5,2), 16);return new Array( r, g, b);};
function Menu_strReplace(t, r, s){value = "";pos0 = 0;pos1 = 0;if ( s.length > 0 ){while( 1 ){ pos1 = s.indexOf(t, pos0);if ( pos1 < 0 ) break;value += s.substring( pos0, pos1) + r;pos0 = pos1 + t.length;}value += s.substring( pos0, s.length);}return value;}

function Menu_showHide( o, type)
{
  isEI = (document.all && navigator.userAgent.indexOf("Opera")==-1)?1:0;
  if ( o.style.display == "none" )
  {
    if ( isEI )
    {
      o.style.display = "inline";
    }
    else
    {
      if ( type == "tr" ) o.style.display = "table-row";
      else if ( type == "td" ) o.style.display = "table-cell";
    }
  } else
  {
    o.style.display = "none";
  }
}
function Menu_overOut( o, type)
{
  id = o.id.substr( 0, o.id.lastIndexOf( "_"));
  /*
  this.showHide( document.getElementById( id+"_topLight"), "tr");
  this.showHide( document.getElementById( id+"_topDark"),  "tr");

  this.showHide( document.getElementById( id+"_leftLight"), "td");
  this.showHide( document.getElementById( id+"_leftDark"),  "td");
  this.showHide( document.getElementById( id+"_leftOrig"),  "td");
  this.showHide( document.getElementById( id+"_rightOrig"), "td");
  */
}
function Menu_mouseClick( o, num)
{
  id = o.id.substr( 0, o.id.lastIndexOf( "_"));
  index = parseInt( o.id.substr( o.id.indexOf( "_")-(-1)));
  index2 = index;
  if ( num == 1 && this.menuDef[index]["action"] != "" )
  {
    level = this.menuDef[index]["level"];
    index_top = index;
    while( 1 )
    {
      index++;
      cur=this.menuDef[index];
      if ( !cur || cur["level"] == level ) break;
      this.open( index);
    }
    eval( this.menuDef[index_top]["action"] );
  }
  else num = 2;
  if ( num == 2 && this.menuDef[index-(-1)] && (this.menuDef[index-(-1)]["level"] == this.menuDef[index]["level"]-(-1)) )
  {
    but = document.images[id.substr( 0, id.lastIndexOf( "_"))+"_button"];
    opn = (this.getStatus( index-(-1))==1)?true:false;
    if ( opn )
      but.src = this.menuDef[index]["but_icon_close"];
    else
      but.src = this.menuDef[index]["but_icon_open"];
    level = this.menuDef[index]["level"];
    index++;
    t = 1;
    while( 1 )
    {
      cur=this.menuDef[index];
      if ( !cur || cur["level"] <= level ) break;
      clearTimeout( this.timeoutIds[index]);
      if ( cur["level"] != level-(-1) && !opn ) { index++; continue;};
      if ( opn )
        if ( this.getStatus( index) == 1 )
          this.timeoutIds[index] = setTimeout( this.name+".close( '"+index+"');", this.timeout*t);
        else
          t--;
      else
        if ( this.getStatus( index) == 0 )
          this.timeoutIds[index] = setTimeout( this.name+".open( '"+index+"');", this.timeout*t);
        else
          t--;
      index++;
      t++;
    }
  }
}
function Menu_open( id)
{
  if ( this.getStatus( id) == 1 ) return;
  this.showHide( document.getElementById( this.name+"_"+id+"_main"), "tr");
  this.setStatus( id, "1");
}
function Menu_close( id)
{
  if ( this.getStatus( id) == 0 ) return;
  img = this.name+"_"+id+"_button";
  but = document.images[img];
  if ( but )but.src = this.menuDef[id]["but_icon_close"];
  this.showHide( document.getElementById( this.name+"_"+id+"_main"), "tr");
  this.setStatus( id, "0");
}
function Menu_printMenu()
{
  mDef = this.menuDef;
  //this.setCookie( this.name+"menu_items", "")
  this.menu_items = this.getCookie( this.name+"menu_items");
  for ( i in mDef) this.menuDefLen++;
  if ( !this.menu_items || ( this.menu_items.length != this.menuDefLen ) )
  {
    this.menu_items = "";
    for ( i in mDef) this.menu_items += (mDef[i]['level']==0)?'1':'0';
  }
  this.setCookie( this.name+"menu_items", this.menu_items);

  mouse_move = "onMouseOver=\"this.className='$$class_over$$';$$name$$.overOut( this, 'over');\" onMouseOut=\"this.className='$$class$$';$$name$$.overOut( this, 'out');\"";
  menu = "";
  for( i in mDef)
  {
    cur = mDef[i];
    if ( mDef[i-(-1)] )
      next = mDef[i-(-1)];
    else
      next = 0;
    dark_color = this.changeColor( cur["bgcolor"], 0.5);
    light_color = this.changeColor( cur["bgcolor"], 1.2);
    opn = ( mDef[i-(-1)] && (mDef[i-(-1)]["level"] == mDef[i]["level"]-(-1)) && this.getStatus( i-(-1))==1)?true:false;
    if ( next && cur["level"] < next["level"])
    {
      but1 = this.menuBut;
      but1 = this.strReplace( "$$td_attr$$", "width='100%' height='"+cur["height"]+"'", but1);
      but1 = this.strReplace( "$$content$$", cur["text"], but1);
      but1 = this.strReplace( "$$space_td$$", this.spaceTd, but1);
      but1 = this.strReplace( "$$mousemove$$", mouse_move, but1);  
      but1 = this.strReplace( "$$num$$", "1", but1);
      but1 = this.strReplace( "$$imaggg$$", "<img name='$$name$$_$$i$$_button' src='"+(opn?cur["but_icon_open"]:cur["but_icon_close"])+"' border=0>", but1);
      /*
      but2 = this.menuBut;
      but2 = this.strReplace( "$$td_attr$$", "width='1' height='"+cur["height"]+"'", but2);
      but2 = this.strReplace( "$$content$$", "<img name='$$name$$_$$i$$_button' src='"+(opn?cur["but_icon_open"]:cur["but_icon_close"])+"' border=0>", but2);
      but2 = this.strReplace( "$$content_attr$$", "align=center", but2);
      but2 = this.strReplace( "$$space_td$$", "", but2);
      but2 = this.strReplace( "$$colspan$$", "4", but2);
      but2 = this.strReplace( "$$mousemove$$", mouse_move, but2);  
      but2 = this.strReplace( "$$num$$", "2", but2);
      */
      tr = this.menuTr;
      tr = this.strReplace( "$$td$$", but1/*+but2*/, tr);
    } else
    {
      but1 = this.menuBut;
      but1 = this.strReplace( "$$td_attr$$", "width='100%' colspan=2 height='"+cur["height"]+"'", but1);
      but1 = this.strReplace( "$$content$$", cur["text"], but1);
      but1 = this.strReplace( "$$space_td$$", this.spaceTd, but1);
      but1 = this.strReplace( "$$mousemove$$", mouse_move, but1);  
      but1 = this.strReplace( "$$num$$", "1", but1);
       but1 = this.strReplace( "$$imaggg$$", "",but1);
      tr = this.menuTr;
      tr = this.strReplace( "$$td$$", but1, tr);
    }
    tr = this.strReplace( "$$i$$", i, tr);
    tr = this.strReplace( "$$name$$", this.name, tr);
    tr = this.strReplace( "$$name$$", this.name, tr);
    tr = this.strReplace( "$$class$$", cur["class"], tr);
    tr = this.strReplace( "$$class_over$$", cur["class_over"], tr);
    tr = this.strReplace( "$$height$$", cur["height"], tr);
    tr = this.strReplace( "$$bgcolor$$", cur["bgcolor"], tr);
    tr = this.strReplace( "$$light_color$$", light_color, tr);
    tr = this.strReplace( "$$dark_color$$", dark_color, tr);
    tr = this.strReplace( "$$content_attr$$", "", tr);
    tr = this.strReplace( "$$space$$", "<img src='"+this.e_gif+"' width=1 height=1 border=0>", tr);
    tr = this.strReplace( "$$colspan$$", "5", tr);
    tr = this.strReplace( "$$closed$$",  (this.getStatus( i, 1)==1)?"":"style='display:none;'", tr);
    tr = this.strReplace( "$$left_icon$$", cur["left_icon"], tr);

    menu += tr;
  }
  menu = this.strReplace( "$$content$$", menu, this.menuTable);
  document.write( menu);
}
function Menu( name)
{
  this.menuDef;
  this.e_gif;
  this.menuDefLen = 0;
  this.menu_items;
  this.name      =name;
  this.timeout = 50;
  this.timeoutIds = new Array();
  this.menuTable = "<table width='100%' border='0' cellspacing='0' cellpadding='0'>\n  $$content$$\n</table>";
  this.menuTr    = "<tr ID='$$name$$_$$i$$_main' $$closed$$>\n$$td$$\n</tr>\n";
  //this.spaceTd = "<td ID='$$name$$_$$i$$_$$num$$_spacetd' class='$$class$$' bgcolor='$$bgcolor$$' $$mousemove$$ onClick=\"$$name$$.mouseClick( this, '$$num$$');\"><img src='$$left_icon$$' border=0></td>\n";
  this.menuBut   = "<td $$td_attr$$>\n\
                       <table width='100%' height='$$height$$' border='0' cellspacing='0' cellpadding='0' class='$$class$$'>\n\
                         <tr>\n\
                           <td height='$$height$$' ID='$$name$$_$$i$$_$$num$$_content'  class='$$class$$'  $$mousemove$$ onClick=\"$$name$$.mouseClick( this, '$$num$$');\"><span>$$content$$</span>$$imaggg$$</td>\n\
                         </tr>\n\
                       </table>\n\
                    </td>\n";

  this.printMenu  = Menu_printMenu;
  this.strReplace = Menu_strReplace;
  this.getRGB = Menu_getRGB;
  this.toHex = Menu_toHex;
  this.makeRGB = Menu_makeRGB;
  this.changeColor = Menu_changeColor;
  this.overOut = Menu_overOut;
  this.showHide = Menu_showHide;
  this.mouseClick = Menu_mouseClick;
  this.setStatus = Menu_setStatus;
  this.getStatus = Menu_getStatus;
  this.setCookie = Menu_setCookie;
  this.getCookie = Menu_getCookie;
  this.close = Menu_close;
  this.open = Menu_open;
}
