
 //desplazamiento en los childNodos dependiente del browser
 //este valor es compartido entre las distintas instancias de los objetos "HermesSimpleMenu"
 var tdPos;

 //constantes
 var vertical = 0;
 var horizontal = 1;

//---------------------------------------------------------------------------


 function gotoLink(tdElem) {
  var tbody = tdElem;

  while (tbody.nodeName != 'TBODY')
	tbody = tbody.parentNode;

  var tds = tbody.getElementsByTagName('TD');

  for (i=0; i<tds.length; i++)
	if (tdElem == tds[i]) {
		//cual link es
                //alert( this.urls[i] );
		window.location = this.urls[i];
		return
	}

  alert('Sin link');
}

 function IncluirLink(celda, nombre, url) {
	var linkElem = document.createElement('a');
	linkElem.appendChild(document.createTextNode(nombre));
	linkElem.href = url;
	celda.appendChild(linkElem)
 }

  function IncluirImagen(celda, imagen) {
  	if (imagen == null)
		return;

	var img = document.createElement('img');
	img.src = imagen;
	//celda.appendChild(img)
	celda.insertBefore(img, celda.childNodes[0]);
 }


function cambiarEstilo(elem, estilo){
	elem.className = estilo;
}

//retorna una referencia a la única celda de fila insertada
function duplicarRow(tbody) {
	var primeraFila, nuevaFila = null;

	primeraFila = tbody.firstChild;
	nuevaFila = primeraFila.cloneNode(true);
	tbody.appendChild(nuevaFila);
	return nuevaFila.childNodes[tdPos];
}

//retorna una referencia a la celda insertada
function duplicarCelda(tbody) {
	var primeraFila, celda, nuevaCelda = null;

	primeraFila = tbody.firstChild;

	//buscar la última celda
	for(var i=0; i<primeraFila.childNodes.length; i++) {
		if (primeraFila.childNodes[i].nodeName == 'TD')
		  celda = primeraFila.childNodes[i];
	 }

	if (celda.nodeName != 'TD')
		alert('celda no es celda:'+celda.nodeName);

	nuevaCelda = celda.cloneNode(true);

	//primeraFila.appendChild(nuevaCelda);
	primeraFila.appendChild(nuevaCelda, celda);
	return nuevaCelda;
}

//mostrar el menu según la configuración establecida en el "new"
function showMenu() {
	var fila, celda;

	if (this.nombres) {
		tdPos = (this.tbody.childNodes[0].childNodes[0].nodeName == "TD" ? 0 : 1); //IE no tiene el elemento #text que si tiene Mozila

		for(var i=0; i<this.nombres.length; i++) {
			if (this.direccion == vertical)
				celda = duplicarRow(this.tbody);
			else
				celda = duplicarCelda(this.tbody);

			//IncluirLink(celda, this.nombres[i], this.urls[i]);
			celda.innerHTML = this.nombres[i];
			IncluirImagen(celda, this.imgs[i]);
		}

		if (this.direccion == vertical)
			this.tbody.removeChild(this.tbody.firstChild);
		else
			this.tbody.firstChild.removeChild(this.tbody.firstChild.childNodes[tdPos]);
	}
}

//constructor HermesSimpleMenu(direccion, idTabla)
	//direccion: vertical | horizontal
	//idTabla: string con el id de la tabla
function HermesSimpleMenu(direccion, idTabla) {

	this.showMenu = showMenu;
	
	if (document.getElementById(idTabla)) {
		
		this.duplicarCelda = duplicarCelda;
		this.duplicarRow = duplicarRow;
		//this.cambiarEstilo = cambiarEstilo;
		this.IncluirImagen = IncluirImagen;
		this.IncluirLink = IncluirLink;
		this.gotoLink = gotoLink;

		this.direccion = direccion;
		this.tbody = document.getElementById(idTabla).lastChild;
		this.nombres = eval( idTabla + '_nombres' );
		this.urls = eval( idTabla + '_urls' );
		this.imgs = eval( idTabla + '_imgs' )
	}
}
