/***************************************************
 * MbDhtmlMenu
 * 
 * Componente javascript para la creacion de menus
 * dinamicos dhtml.
 *
 * Mazbit S.L. 2006
 **************************************************/

var horSpacing = 1;
var verSpacing = 1;

/*
 * show
 */
function show(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);
  var p2 = p;
  var c2 = c;

  var top  = (c["at_position"] == "y") ? p.offsetHeight+verSpacing : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth+horSpacing : 0;
  var childWidth = 0;

  for (; p; p = p.offsetParent)
  {
    if (p.style.position != 'absolute')
    {
      left += p.offsetLeft;
      top += p.offsetTop;
    }
  }

  /*
   * Calculate the total width of the child
   */

  c2 = c2.offsetParent;
  for(; c2; c2 = c2.offsetParent) {
	    childWidth += c2.offsetLeft;
  }

  /*
   * Calculate the total width of the window
   */
 
  var windowWidth = 2000;
  if( window.innerWidth ) {
	windowWidth = window.innerWidth;
  } else if( document.documentElement.clientWidth ) {
	windowWidth = document.documentElement.clientWidth;
  } else if( document.body.clientWidth ) {
	windowWidth = document.body.clientWidth;
  }


  

  /*
   * Save the edges
   */


  c.style.position   = "absolute";

  //window.status = childWidth + " - " + windowWidth;
  if( (childWidth + left) > (windowWidth-200)) {
  	//window.status = Math.random()  + " left: " + childWidth + " width: " + c.offsetWidth + " pageXof: " +  window.pageXOffset;
	left = -left + (horSpacing/2);
	top = top + (verSpacing/2);
  }
  c.style.left       = left+'px';
  c.style.top        = top+'px';
  c.style.visibility = "visible";
}

/*
 * hide
 */
function hide(parent, child)
{
  document.getElementById(child ).style.visibility = "hidden";
}

/*
 * show_parent
 */
function show_parent()
{
  c = document.getElementById(this["at_child" ]);
  show(this.id, c.id);
  clearTimeout(c["at_timeout"]);
  return true;
}

/*
 * show_child
 */
function show_child()
{
  p = document.getElementById(this["at_parent"]);
  show(p.id, this.id);
  clearTimeout(this["at_timeout"]);
  return true;
}

/*
 * hide_parent
 */
function hide_parent()
{
  c = document.getElementById(this["at_child" ]);
  c["at_timeout"] = setTimeout("hide('"+this.id+"', '"+c.id+"')", 100);
  return true;
}

/*
 * hide_client
 */
function hide_client()
{
  p = document.getElementById(this["at_parent"]);
  this["at_timeout"] = setTimeout("hide('"+p.id+"', '"+this.id+"')", 100);
  return true;
}

/*
 * order
 */
function order(parent, child, position)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p["at_child"]    = c.id;
  c["at_parent"]   = p.id;
  c["at_position"] = position;

  p.onmouseover = show_parent;
  p.onmouseout  = hide_parent;
  c.onmouseover = show_child;
  c.onmouseout  = hide_client;
}


/*
 * create_menu_aux
 */
function create_menu_aux(parent, child, position, style, style_lvl2, style_opt, style_menu_opt )
{
  document.write('<div class="'+style+'" id="'+parent+'_child">');

  var n = 0;
  for (var i in child)
  {
    if (i == '-')
    {
      	document.getElementById(parent).href = child[i];
      	continue;
    }

    if (typeof child[i] == "object")
    {
      	document.write('<a class="'+style_menu_opt+'" id="'+parent+'_'+n+'">'+i+'</a>');
      	create_menu_aux(parent+'_'+n, child[i], "x", style_lvl2, style_lvl2, style_opt, style_menu_opt);
    } else 
  	document.write('<a class="'+style_opt+'" id="'+parent+'_'+n+'" href="'+child[i]+'">'+i+'</a>');
    n++;
  }

  document.write('</div>');

  order(parent, parent+"_child", position);
}


/*
 * create_menu
 */

/**
 * Parametros:
 * 	menu: La variable con la definicion del menu
 * 	style: Nombre del estilo para los divs de los menus
 * 	style_lvl2: Nombre del estilo para los divs de los menus de
 * 		    nivel 2 en adelante
 *	style_menu_opt: Nombre del estilo para las opciones de menu
 *			que contienen un submenu asociado
 */
function create_menu_vert(menu, style, style_lvl2, style_opt, style_menu_opt )
{
  style_lvl2 = (style_lvl2 == null) ? style : style_lvl2;
  style_opt = (style_opt == null) ? "" : style_opt;
  style_menu_opt = (style_menu_opt == null) ? "" : style_menu_opt;

  for (var i in menu) { 
	create_menu_aux(i, menu[i], "y", style, style_lvl2, style_opt, style_menu_opt );
  }
}

/*
 * create_menu
 */
/**
 * Parametros:
 * 	menu: La variable con la definicion del menu
 * 	style: Nombre del estilo para los divs de los menus
 * 	style_lvl2: Nombre del estilo para los divs de los menus de
 * 		    nivel 2 en adelante
 *	style_otp: Nombre del estilo para las opciones de menu que
 *		   no contienen un submenu asociado
 *	style_menu_opt: Nombre del estilo para las opciones de menu
 *			que contienen un submenu asociado
 */
function create_menu_horz(menu, style, style_lvl2, style_opt, style_menu_opt )
{
  style_lvl2 = (style_lvl2 == null) ? style : style_lvl2;
  style_opt = (style_opt == null) ? "" : style_opt;
  style_menu_opt = (style_menu_opt == null) ? "" : style_menu_opt;

  for (var i in menu) { 
	create_menu_aux(i, menu[i], "x", style, style_lvl2, style_opt, style_menu_opt );
  }
}
