function show_hide_sublist(id)
{
  var lst = document.getElementById(id);
  if(!lst) return;
  
  if(lst.style.display == "none") lst.style.display = "block";
  else lst.style.display = "none";
}

var menu_info = new Object();

var pattern = /([\s\S]*)\{item_begin\}([\s\S]*)\{item_end\}([\s\S]*\{aitem_begin\}([\s\S]*)\{aitem_end\})*([\s\S]*)/im;
//-------------------------------------------------------
function mitem_over(item)
{
  var oitem = $(item);
  mtype = oitem.attr("menu_type");
  oitem.addClass(mtype + '_menu_active_item');
  $("#tx_" + oitem.attr("id")).addClass(mtype + '_menu_active_text');

  if(menu_info[oitem.attr("menu_id")].nopopups) return;
  
  // menu is active, do not close popups
  menu_info[oitem.attr("menu_id")]["close_popups"] = "0";
  
  hide_other_submenus(oitem);

  if(oitem.attr("item_type") == "submenu" ||
    (menu_structure[oitem.attr("item_id")] && menu_structure[oitem.attr("item_id")].length > 0))
  {
    show_submenu(oitem);
  }
} // mitem_over
//-------------------------------------------------------
function mitem_out(item)
{
  var oitem = $(item);
  
  // menu is no more active, cset pending status to close popups
  // if the menu will not be enteren in X seconds it will be closed
  menu_info[oitem.attr("menu_id")]["close_popups"] = "1";
  setTimeout("close_popups('" + oitem.attr("menu_id") + "')", 1200);

  if(oitem.attr("is_current") == "1" || oitem.attr("active") == "1")
  {
    return;
  }
  
  mtype = oitem.attr("menu_type");
  oitem.removeClass(mtype + '_menu_active_item');
  $("#tx_" + oitem.attr("id")).removeClass(mtype + '_menu_active_text');
} // mitem_out
//-------------------------------------------------------
function close_popups(menu_id)
{
  if(menu_info[menu_id]["close_popups"] == "1")
  {
    if(menu_info[menu_id]["active_submenu"])
      hide_submenus(menu_info[menu_id]["active_submenu"]);
  }
} // close_popups
//-------------------------------------------------------
function hide_other_submenus(oitem)
{
  id = oitem.attr("item_id");
  parent_id = oitem.attr("parent");
  var ln = menu_structure[parent_id].length;
  for(var i = 0; i < ln; i++)
  {
    var current_item = menu_structure[parent_id][i];
    
    if(current_item.id == null || current_item.id == id) continue;
    
    //var msg = current_item.id + ":" + current_item.type;
    //if(menu_structure[current_item.id]) msg += "children:" + menu_structure[current_item.id].length;
    //alert(msg);
    
    if(current_item.type == "submenu" ||
       (menu_structure[current_item.id] && menu_structure[current_item.id].length > 0)
      )
    {
      //alert("parent:" + parent_id + ", " + i + " of " + ln + ", hiding:" + current_item.id);
      var citem = $("#" + oitem.attr("menu_id") + '_' + current_item.id);
      if(citem.get(0)) hide_submenus(citem);
    }
  }
} // hide_other_submenus
//-------------------------------------------------------
function hide_submenus(oitem)
{
  var submenu = $("#submenu_" + oitem.attr("id"));
  submenu.css("display", "none");

  //alert("hiding:" + oitem.attr("id"));
  
  if(!menu_structure[oitem.attr("item_id")]) return;
  
  var ln = menu_structure[oitem.attr("item_id")].length;
  for(var i = 0; i < ln; i++)
  {
    var current_item = menu_structure[oitem.attr("item_id")][i];
    
    if(current_item.id == null) continue;

    //alert(i);
    
    if(current_item.type == "submenu" ||
       (menu_structure[current_item.id] && menu_structure[current_item.id].length > 0))
    {
      var citem = $("#" + oitem.attr("menu_id") + '_' + current_item.id);
      if(citem.get(0)) hide_submenus(citem);
    }
  }
  
  oitem.attr("active", "0");
  
  if(oitem.attr("is_current") == "1") return;
  
  mtype = oitem.attr("menu_type");
  oitem.removeClass(mtype + '_menu_active_item');
  $("#tx_" + oitem.attr("id")).removeClass(mtype + '_menu_active_text');
} // hide_submenus
//-------------------------------------------------------
function get_corrections(item, orientation)
{
  //return { x : 0, y : 0 };
  
  var mtype = item.attr("menu_type");

  var brd = item.css("borderLeftWidth");
  brd = brd.replace(/px/, "");
  var xx = 0;
  var yy = 0;
  
  if(navigator.appName == "Netscape" || 
     navigator.appName == "Opera")
    yy = Math.round(brd/2);

  if(navigator.appName == "Netscape" || 
     navigator.appName == "Opera")
    xx = Math.round(brd/2);
    
  if(navigator.appName == "Microsoft Internet Explorer" && 
    (orientation == "left" || orientation == "right") &&
    mtype != "p"
    )
    yy = Math.round(brd/2);

  if(navigator.appName == "Microsoft Internet Explorer" && 
    (orientation == "top" || orientation == "bottom")
    )
    xx = Math.round(brd/2);
    
  return { x : xx, y : yy };
}
//-------------------------------------------------------

function show_submenu(oitem)
{
  if(!menu_structure[oitem.attr("item_id")]) return;

  var orientation = oitem.attr("orientation");
  var mtype = oitem.attr("menu_type");
  
  if(mtype != "p")
  {
    menu_info[oitem.attr("menu_id")]["active_submenu"] = oitem;
  }
  
  oitem.attr("active", "1"); 
  oitem.addClass(mtype + '_menu_active_item');
  $("#tx_" + oitem.attr("id")).addClass(mtype + '_menu_active_text');

  // reuse if already exists create if no
  var div = $("#submenu_" + oitem.attr("id"));
  if(div.get(0) == null)
  {
    var menu_html = '<div style="z-index:9000" id="submenu_' + oitem.attr("id") + '">' + get_menu_html("p", oitem.attr("menu_id"), oitem.attr("item_id"), menu_info[oitem.attr("menu_id")].active_item) + '</div>';
    
    div = $(menu_html);
    div.css('position', 'absolute'); 
    div.prependTo(document.body);
    //  top and bottom are important only for first level menu
    var tmp = orientation;
    if(tmp == "bottom") tmp = "leftbottom";
    else if(tmp != "right") tmp = "left";
    add_menu_events("p", tmp, oitem.attr("menu_id"), oitem.attr("item_id"), "");
  }
  else
  {
    div.css('display', 'block'); 
  }

  var offset = oitem.offset();
  var left = offset.left;
  var top = offset.top;
  
  var corrections = get_corrections(oitem, orientation);
  var opts = {margin:true};
 
  if(orientation == "left")
  {
    //left = offset.left + oitem.width();
    left = offset.left + oitem.outerWidth(opts);
    top -= corrections.y;
    left += corrections.x;
  }
  
 
  if(orientation == "right")
  {
    //left = offset.left - div.width();
    left = offset.left - div.outerWidth(opts);
    top -= corrections.y;
    left -= corrections.x;
  }
  
  if(orientation == "top")
  {
    //top = offset.top + oitem.height(); 
    top = offset.top + oitem.outerHeight(opts); 
    top += corrections.y;
    left -= corrections.x;
  }
  
  if(orientation == "bottom")
  {
    //top = offset.top - div.height(); 
    top = offset.top - div.outerHeight(opts); 
    top -= corrections.y;
    left -= corrections.x;
  }
  if(orientation == "leftbottom")
  {
    //left = offset.left + oitem.width();
    left = offset.left + oitem.outerWidth(opts);
    //top = offset.top - div.height() + oitem.height(); 
    top = offset.top - div.outerHeight(opts) + oitem.outerHeight(opts); 
    top += corrections.y;
  }

  div.css('left', left); 
  div.css('top', top); 
} // show_submenu
//-------------------------------------------------------
function mitem_clicked(item)
{
  var oitem = $(item);
  if(oitem.attr("item_type") == "link")
  {
    if(menu_info[oitem.attr("menu_id")]["active_submenu"])
      hide_submenus(menu_info[oitem.attr("menu_id")]["active_submenu"]);
  }
  
  if(oitem.attr("url"))
  {
    if(oitem.attr("newwnd") == 1)
    {
      window.open(oitem.attr("url"));
    }
    else
    {
      document.location.href = oitem.attr("url");
    }
  }
} // mitem_clicked
//-------------------------------------------------------
function get_menu_html(menu_type, menu_id, start_id, current_id)
{
  var menu_html = "";
  
  if(menu_type == "h")
    sample = h_menu_sample;
  else if(menu_type == "v") 
    sample = v_menu_sample;
  else if(menu_type == "p") 
    sample = p_menu_sample;
  else
    return menu_html;   
  
  var result = pattern.exec(sample);
  if(result && typeof menu_structure[start_id] != "undefined")
  {
    menu_html += result[1];
    
    var ln = menu_structure[start_id].length;
    var ord_class = "";
    for(var i = 0; i < ln; i++)
    {
      var item = menu_structure[start_id][i];
      
      // if a submenu has a link to an item that is active but invisible
      // mark this submenu as active      
      if(!menu_hierarchy[current_id] && item.linkto != '' && current_id == item.linkto)
      {
        menu_info[menu_id].active_item = item.id;
        current_id = item.id;
      }

      var is_active = (item.id == current_id);
      var has_children = (menu_structure[item.id] && menu_structure[item.id].length > 0);
      
      if(item.id == current_id && result[4] != "")
        menu_body = result[4];
      else  
        menu_body = result[2];
      
      pict = "";

      // icon for concrete active item with children
      if(is_active && has_children && pict == "") pict = item.active_c_picture;
      // icon for concrete active item 
      if(is_active && pict == "") pict = item.active_picture;
      // icon for concrete item with children
      if(has_children && pict == "") pict = item.c_picture;
      // icon for concrete item 
      if(pict == "") pict = item.picture;
      
      // icon for active item with children of concrete menu type 
      if(is_active && has_children && pict == "") pict = menu_img["active_" + menu_type + "_c"];
      // icon for active item of concrete menu type 
      if(is_active && pict == "") pict = menu_img["active_" + menu_type];
      // icon for item with children of concrete menu type 
      if(has_children && pict == "") pict = menu_img[menu_type + "_c"];
      // icon for item of concrete menu type 
      if(pict == "") pict = menu_img[menu_type];

      // icon for active item with children for all menu types 
      if(is_active && has_children && pict == "") pict = menu_img.active_a_c;
      // icon for active item for all menu types 
      if(is_active && pict == "") pict = menu_img.active_a;
      // icon for item with children for all menu types 
      if(has_children && pict == "") pict = menu_img.a_c;
      // icon for item for all menu types 
      if(pict == "") pict = menu_img.a;

      menu_body = menu_body.replace(/\{pict\}/gi, pict);
      menu_body = menu_body.replace(/\{id\}/gi, menu_id + '_' + item.id);
      menu_body = menu_body.replace(/\{iid\}/gi, item.id);
      menu_body = menu_body.replace(/\{name\}/gi, item.name);
      
      ord_class = "";
      if(i == 0)
      {
        ord_class = "first_menu_item";
      }
      else if(i == ln-1) 
      {
        ord_class = "last_menu_item";
      }
      
      menu_body = menu_body.replace(/\{ord\}/gi, ord_class);
      
      menu_html += menu_body;
    }
    menu_html += result[5];

    menu_html = menu_html.replace(/\{mid\}/gi, menu_id);
  }  
  
  return menu_html;
} // get_menu_html
//-------------------------------------------------------
function add_menu_events(menu_type, orientation, menu_id, start_id, current_id)
{
  if(!menu_structure[start_id]) return;
  
  var ln = menu_structure[start_id].length;
  for(var i = 0; i < ln; i++)
  {
    var item = menu_structure[start_id][i];
    var oitem = $("#" + menu_id + '_' + item.id);
    
    var item_type = item.type;
    if(menu_structure[item.id] && menu_structure[item.id].length > 1) item_type = "submenu";
    
    oitem.attr("item_id", item.id);
    oitem.attr("menu_id", menu_id);
    oitem.attr("parent", start_id);
    oitem.attr("item_type", item_type);
    oitem.attr("menu_type", menu_type);
    oitem.attr("orientation", orientation);
    oitem.attr("url", item.url);
    oitem.attr("newwnd", item.newwnd);
    
    oitem.click(
    function() { mitem_clicked(this); }
    );
    
    if(item.id == current_id || item.id == menu_info[menu_id].active_item) 
    {
      oitem.attr("is_current", "1");
    }
    
    oitem.hover(
    function() { mitem_over(this); }, 
    function() { mitem_out(this); }
    );
  } // for
} // add_menu_events
//-------------------------------------------------------
function draw_main_menu(menu_id, menu_type, orientation, start_id, current_page_id, nopopups)
{
  // strategy of marking active
  // if the page or its child is active mark the menu item active
  
  var current_id = current_page_id;
  //alert(menu_id + ":" + current_id + ", parent:");
  //alert(menu_structure[current_id]);
  while(menu_hierarchy[current_id] != null &&
        menu_hierarchy[current_id] != start_id)
  {
    current_id = menu_hierarchy[current_id];
  }
  
  menu_info[menu_id] = new Object();
  menu_info[menu_id].active_item = current_page_id;
  menu_info[menu_id].nopopups = nopopups;

  var menu_html = get_menu_html(menu_type, menu_id, start_id, current_id);
  
  document.write(menu_html);
  
  add_menu_events(menu_type, orientation, menu_id, start_id, current_id);
} // draw_main_menu
//-------------------------------------------------------
