function TItem() {
  this.id       = 0;
  this.SubItem  = new Array();
  this.caption  = "";
  this.url      = "";
  this.objLayer = "";
  this.timeout  = "";
  // indica se é titulo
  this.title    = false;

  this.AddSubItem = AddSubItem;
  this.ShowSubMenu = ShowSubMenu;
  this.HideSubMenu = HideSubMenu;
  this.CreateSubMenu = CreateSubMenu;
}

function TSubItem() {
  this.id        = 0;
  this.caption   = "";
  this.url       = "";
}

function TMenu(t) {
  this.Item = new Array();
  this.AddItem = AddItem;
  this.AddTitle = AddTitle;
  this.Build   = Build;
  this.ReloadPositions = ReloadPositions;
  this.type = t;  // tipo, que determina a cor do menu
  this.w    = null;
  this.SetSubMenuWidth = SetSubMenuWidth;
}

function AddItem(caption, url) {
//  this.Item.push(new TItem());
  this.Item[this.Item.length] = new TItem();

  this.Item[this.Item.length - 1].id      = this.Item.length - 1;
  this.Item[this.Item.length - 1].caption = caption;
  this.Item[this.Item.length - 1].url     = url;
  this.Item[this.Item.length - 1].parentMenu = this;  // Guarda o objeto "pai" (parent) deste TItem (que é um objeto do tipo TMenu)
}

function AddTitle(caption) {
//  this.Item.push(new TItem());
  this.Item[this.Item.length] = new TItem();

  this.Item[this.Item.length - 1].id      = this.Item.length - 1;
  this.Item[this.Item.length - 1].caption = caption;
//  this.Item[this.Item.length - 1].url     = url;
  this.Item[this.Item.length - 1].title     = true;
  this.Item[this.Item.length - 1].parentMenu = this;  // Guarda o objeto "pai" (parent) deste TItem (que é um objeto do tipo TMenu)
}

function AddSubItem(caption, url) {
//  this.SubItem.push(new TSubItem());
  this.SubItem[this.SubItem.length] = new TSubItem();

  this.SubItem[this.SubItem.length - 1].id      = this.SubItem.length - 1;
  this.SubItem[this.SubItem.length - 1].caption = caption;
  this.SubItem[this.SubItem.length - 1].url     = url;
}

function CreateSubMenu(menu_id, submenu) {
  var tam = '';
  if (this.parentMenu.w != null) tam = 'width:' + this.parentMenu.w + 'px; ';
  
  document.writeln('<DIV id="' + menu_id + '" class="shadow_menu" style="position:absolute; z-index:1; ' + tam + 'left:0px; top:0px; visibility: hidden;">');
  document.writeln('<TABLE id="table_submenu" width="100%" class="submenu" border="0" cellpadding="4" cellspacing="1" bgcolor="#FFFFFF">');

  for (i = 0; i < this.SubItem.length; i++) {
    document.writeln('  <TR>');
    document.writeln('    <TD id="td_sub_' + (this.id+1) + '_' + (i+1) + '" bgcolor="#DBEEFF" class="menu_' + this.parentMenu.type + '_td_normal" OnMouseOver="SubOver(' + this.id + ', ' + i + ', \'menu_' + this.parentMenu.type + '_td_over\')" OnMouseOut="SubOut(' + this.id + ', ' + i + ', \'menu_' + this.parentMenu.type + '_td_normal\')" OnClick="document.location.href=\'' + this.SubItem[i].url + '\'">');
    document.writeln('      <TABLE width="100%" border="0" cellspacing="0" cellpadding="0">');
    document.writeln('        <TR>');
//    document.writeln('          <TD width="4"><IMG src="/img/spacer.gif"></TD>');
    document.writeln('          <TD><FONT class="fontesMed"><A href="' + this.SubItem[i].url + '" class="menu_' + this.parentMenu.type + '_lnk">' + this.SubItem[i].caption + '</A></FONT></TD>');
    document.writeln('        </TR>');
    document.writeln('      </TABLE>');
    document.writeln('    </TD>');
    document.writeln('  </TR>');
  }
  
  document.writeln('</TABLE>');
  document.writeln('</DIV>');

}

function SubOver(id_item, id_sub, cl) {
  var td;
  clearTimeout(menu.Item[id_item].timeout);
  td = document.getElementById('td_sub_' + (id_item+1) + '_' + (id_sub+1));
  //td.bgColor = '#CAE0FF';
  td.className = cl;
  td.style.cursor = 'hand';
}

function SubOut(id_item, id_sub, cl) {
  var td;
  menu.Item[id_item].HideSubMenu(id_item);
  td = document.getElementById('td_sub_' + (id_item+1) + '_' + (id_sub+1));
  //td.bgColor = '#E1EDFF';
  td.className = cl;
  td.style.cursor = 'default';
}

function ShowSubMenu(idx) {
  if (this.timeout != '') clearTimeout(this.timeout);
  this.timeout = setTimeout(('menu.Item[' + idx + '].objLayer.style.visibility="visible"'), 300);
}

function HideSubMenu(idx) {
  if (this.timeout != '') clearTimeout(this.timeout);
  this.timeout = setTimeout(('menu.Item[' + idx + '].objLayer.style.visibility="hidden"'), 300);
}

function MenuOver(td, cl) {
  td.className = cl;
  td.style.cursor = 'hand';
}

function MenuOut(td, cl) {
  td.className = cl;
  td.style.cursor = 'default';
}

function Build() {
  var i, m_over, m_out, src_seta, actions, label, css_class;

  document.writeln('<TABLE id="table_menu" width="100%" border="0" cellpadding="4" cellspacing="1">');
  document.writeln('  <TR>');
  for (i = 0; i < this.Item.length; i++) {
    // Eventos mouse-over e mouse-out dos menus
    m_over = 'MenuOver(document.getElementById(\'td_menu_' + (i+1) + '\'), \'menu_' + this.type + '_td_over\')';
    m_out  = 'MenuOut(document.getElementById(\'td_menu_' + (i+1) + '\'), \'menu_' + this.type + '_td_normal\')';
    src_seta = 'spacer.gif';
	if (this.Item[i].SubItem != null) {
		if (this.Item[i].SubItem.length > 0) {
		  src_seta = 'seta_menu_' + this.type + '.gif';
		  m_over += ';menu.Item[' + i + '].ShowSubMenu(' + i + ')';
		  m_out  += ';menu.Item[' + i + '].HideSubMenu(' + i + ')';
		}
	}
	
	if (!this.Item[i].title) {
		actions = ' onMouseOver="' + m_over + '" onMouseOut="' + m_out + '" onClick="document.location.href=\'' + this.Item[i].url + '\'"';
		label   = '<A href="' + this.Item[i].url + '" class="menu_' + this.type + '_lnk">' + this.Item[i].caption + '</A>';
		css_class = 'menu_' + this.type + '_td_normal';
	} else {
		label   = '<FONT color="#FFFFFF"><B>' + this.Item[i].caption + '</B></FONT>';
		css_class = 'menu_' + this.type + '_td_title';
	}

	document.writeln('    <TD id="td_menu_' + (i+1) + '" class="' + css_class + '"' + actions + ' width="' + Math.round(this.Item.length/3) + '%">');
    document.writeln('      <TABLE width="100%" border="0" cellspacing="0" cellpadding="0">');
    document.writeln('        <TR>');
    //document.writeln('          <TD width="4"><IMG src="/img/spacer.gif"></TD>');
    document.writeln('          <TD><FONT class="fontesMed"><center>' + label + '</center></FONT></TD>');
    //document.writeln('          <TD id="td_seta_' + (i+1) + '" width="1"><IMG name="seta_' + (i+1) + '" id="seta_' + (i+1) + '" src="/img/' + src_seta + '"></TD>');
    document.writeln('        </TR>');
    document.writeln('      </TABLE>');
    document.writeln('    </TD>');
    
  }
  document.writeln('  </TR>');

  document.writeln('</TABLE>');
  
  // Cria todos os layers de submenus:
  for (i = 0; i < this.Item.length; i++) {
    if (this.Item[i].SubItem != null) {
	if (this.Item[i].SubItem.length > 0) {
	  this.Item[i].CreateSubMenu('ly_submenu_' + (i+1), this.Item[i].SubItem);
	  this.Item[i].objLayer = document.getElementById('ly_submenu_' + (i+1));
	}
    }
  }

  document.close();
}

function ReloadPositions() {
  for (i = 0; i < this.Item.length; i++) {
    if (this.Item[i].SubItem.length > 0) {
	  //document.getElementById('ly_submenu_' + (i+1)).style.left = GetSubMenuLeft(i+1);
  	  //document.getElementById('ly_submenu_' + (i+1)).style.top  = GetSubMenuTop(i+1);
	  this.Item[i].objLayer.style.left = GetSubMenuLeft(i+1);
  	  this.Item[i].objLayer.style.top  = GetSubMenuTop(i+1);
	}
  }
}

function GetSubMenuTop(td_id) {
  var r_left = 20;
  r_left = r_left + document.getElementById('table_main').offsetTop;
  r_left = r_left + document.getElementById('table_menu').offsetTop;
//  r_left = r_left + document.getElementById('td_aux').offsetLeft;
//  r_left = r_left + document.getElementById('td_menu_' + td_id).offsetLeft;
//  r_left = r_left + document.getElementById('td_seta_' + td_id).offsetLeft;
  r_left = r_left + 11 + 92;
  return r_left;
}

function GetSubMenuLeft(td_id) {
  var r_top = 0;
//  alert(document.getElementById('td_content').offsetTop);
  r_top = r_top + document.getElementById('table_main').offsetLeft;
  r_top = r_top + document.getElementById('td_content').offsetLeft;
//  r_top = r_top + document.getElementById('table_main').offsetTop;
//  r_top = r_top + document.getElementById('table_menu').offsetTop;
  r_top = r_top + document.getElementById('td_menu_' + td_id).offsetLeft;
  r_top = r_top + 4;
  return r_top;
}

function SetSubMenuWidth(w) {
  this.w = w;
}