// JavaScript Document
var POPUP_IE = 0;
var POPUP_MOZILLA = 1;


function ItemMenu()
{
  this.texto;
  this.tag = 0;
  this.hint = "";
  this.onclick;
  this.tr = null;
  this.tdesquerdo = null;
  this.tdcentro = null;
  this.tddireito = null;
  
  return this;
}

function PopUpMenu()
{
  this.itens = new Array();
  this.cordestaque = "#ABCDEF";
  this.corfundo    = "#E8E8E8";
  this.textoselecionado = "";
  this.selecao = null;
  this.range = null;
  this.browsermodo = -1;
  
  this.div = document.createElement('div');
  this.div.style.padding = "2px";
  this.div.style.display = "none";
  this.div.style.position = "absolute"; 
  this.div.style.backgroundColor = this.corfundo;
  this.div.style.borderLeft = "1px solid #FAFAFA";
  this.div.style.borderTop = "1px solid #FAFAFA";
  this.div.style.borderRight = "1px solid #E0E0E0";
  this.div.style.borderBottom = "1px solid #D0D0D0";
  
  //this.div.onblur = TrataOcultarMenu(this);
  document.body.appendChild(this.div);
  
  this.table = document.createElement('table');
  this.div.appendChild(this.table);
  this.table.cellPadding = 0;
  this.table.cellSpacing = 0;
  
  this.tbody = document.createElement('tbody');
  this.table.appendChild(this.tbody);
  
  this.table.style.font = "8pt tahoma";
  
  
  return this;
}

PopUpMenu.prototype.adiciona = PopUpMenuadiciona;
PopUpMenu.prototype.adicionaseparador = PopUpMenuadicionaseparador;
PopUpMenu.prototype.exibe = PopUpMenuexibe;
PopUpMenu.prototype.oculta = PopUpMenuoculta;


function PopUpMenuadicionaseparador()
{
  var tr = document.createElement('tr');
  this.tbody.appendChild(tr);
  var td = tr.insertCell(0);
  td.colSpan = 3;
  
  var hr = document.createElement('hr');
  
  td.appendChild(hr);
}

function PopUpMenuadiciona(texto, funcaotratadora)
{
  var item = new ItemMenu();
  item.texto = texto;
  item.onclick = funcaotratadora;
  item.tr = document.createElement('tr');
  this.tbody.appendChild(item.tr);  
  item.tdesquerdo = item.tr.insertCell(0);
  item.tdcentro = item.tr.insertCell(0);
  item.tddireito = item.tr.insertCell(0);
  
  item.tdesquerdo.style.width = "5px";
  item.tdesquerdo.style.height = "22px";
  item.tddireito.style.width = "4px";
    

  item.tr.style.cursor = "pointer";
  item.tdcentro.innerHTML = texto;
  item.tdcentro.style.padding = "0px 10px 0px 10px";
  item.tr.onclick = TrataMouseClickItem(this, item);
  item.tr.onmouseover = TrataMouseOverOutItem(this, item, this.cordestaque, true);
  item.tr.onmouseout = TrataMouseOverOutItem(this, item, this.corfundo, false);
  
  this.itens[this.itens.length] = item;
  
  return item;
}

function PopUpMenuexibe(x,y)
{
  if (document.selection)
  { 
    this.selecao = document.selection;
    this.range = document.selection.createRange();
    this.textoselecionado = this.range.text;
    this.browsermodo = POPUP_IE;
  }
  else
  {
    this.selecao = window.getSelection();
    this.range = this.selecao.getRangeAt(0).cloneRange();
    this.textoselecionado = this.range.toString();
    this.browsermodo = POPUP_MOZILLA;
  }  
  
  if (this.itens[this.itens.length - 1].texto != "Cancelar")
  {
    this.adicionaseparador();
    this.adiciona("Cancelar", null);
  }

  var i = 0;
  var largura = 0;
  this.div.style.left = x;
  this.div.style.top = y;
  this.div.style.display = "block";
  
  this.div.focus();
}

function PopUpMenuoculta()
{
  this.div.style.display = "none";
}

function TrataMouseClickItem(objeto, item)
{
  return function()
  {
    if (item.onclick) item.onclick(objeto, item, objeto.textoselecionado);
    objeto.oculta();
  }
}

function TrataMouseOverOutItem(objeto, item, cor, ativo)
{
  return function()
  {
    item.tr.style.backgroundColor = cor;
    item.tdesquerdo.style.backgroundImage = (ativo)?"url(http://premiumweb.jurid.com.br/cgi-bin/imagens/popups/popup-1.gif)":"url(nada)";
    item.tdcentro.style.backgroundImage = (ativo)?"url(http://premiumweb.jurid.com.br/cgi-bin/imagens/popups/popup-2.gif)":"url(nada)";
    item.tddireito.style.backgroundImage = (ativo)?"url(http://premiumweb.jurid.com.br/cgi-bin/imagens/popups/popup-3.gif)":"url(nada)";
  }
}

function TrataOcultarMenu(objeto)
{
  return function()
  {
    objeto.oculta();
  }
}
