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="140" border="0" cellpadding="4" cellspacing="1">');
  
  for (i = 0; i < this.Item.length; i++) {
    // Eventos mouse-over e mouse-out dos menus
    
    // colocamos o if código abaixo para alterar a cor neste link específico
    if (this.Item[i].caption == '10 anos de história') {
      m_over = 'MenuOver(document.getElementById(\'td_menu_' + (i+1) + '\'), \'menu_' + 'congresso_10anos' + '_td_over\')';
      m_out  = 'MenuOut(document.getElementById(\'td_menu_' + (i+1) + '\'), \'menu_' + 'congresso_10anos' + '_td_normal\')';
    } else {
      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 + '\'"';
		
		// colocamos o if código abaixo para alterar a cor neste link específico		
		if (this.Item[i].caption == '10 anos de história')	{
      label   = '<A href="' + this.Item[i].url + '" class="menu_' + 'congresso_10anos' + '_lnk">' + this.Item[i].caption + '</A>';
    } else {
      label   = '<A href="' + this.Item[i].url + '" class="menu_' + this.type + '_lnk">' + this.Item[i].caption + '</A>';
	  }
	  
		// colocamos o if código abaixo para alterar a cor neste link específico
	if (this.Item[i].caption == '10 anos de história')	{
      css_class = 'menu_' + 'congresso_10anos' + '_td_normal';
    } else {
      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('  <TR>');
	document.writeln('    <TD id="td_menu_' + (i+1) + '" class="' + css_class + '"' + actions + '>');
    document.writeln('      <TABLE width="100%" border="0" cellspacing="0" cellpadding="0" align="center">');
    document.writeln('        <TR>');
    document.writeln('          <TD width="4"><IMG src="/img/spacer.gif"></TD>');
    document.writeln('          <TD><FONT class="fontesMed">' + label + '</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 findPosX(obj) // Vitor - função nova para obter a posição X dos objetos
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function ReloadPositions() {
  for (i = 1; 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);
	  
	  // Vitor (comentei a linha antiga)
	  // this.Item[i].objLayer.style.left = GetSubMenuLeft(i);
	  this.Item[i].objLayer.style.left = GetSubMenuLeft(i) + parseInt(this.Item[i].objLayer.style.width.replace("px","")) + 40;
	  //alert(document.getElementById('td_menu_7'));
	  // Vitor - Fim
  	
  	this.Item[i].objLayer.style.top  = GetSubMenuTop(i);
	}
  }
}

function GetSubMenuLeft(td_id) {
  var r_left = 0;
  var r_width = 0; // Vitor
    
  //r_left = document.getElementById('table_menu').offsetLeft; //Vitor - linha antiga
  r_left = findPosX(document.getElementById('table_menu'))-20; // Vitor - linha nova
  
  //r_width = document.getElementById('table_menu').offsetWidth + findPosX(document.getElementById('td_menu_7')); // Vitor
  
  //  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;
//AQUI O MAU COLOCOU "11" QUE É A COMPENSAÇÃO DO X(ZERO)
  //r_left = r_left + r_width; // Vitor (retirado o "+ 11" e adicionado r_width)
  return r_left;
}

function GetSubMenuTop(td_id) {
  var r_top = 0;
//  alert(document.getElementById('td_content').offsetTop);
  r_top = r_top + document.getElementById('td_content').offsetTop;
//  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).offsetTop;
  r_top = r_top + 3;
  return r_top;
}

function SetSubMenuWidth(w) {
  this.w = w;
}
