
// JJBM, 11/06/2007, intellisense.js
// JJBM, 25/06/2007, barra espaciadora no inserte palabra

var ddl = {
	Creado: false,
	Activo: false,
	popup: null,
	append: null,
	posX: 0,
	posY: 0,
	prefijo: "",
	idx: 0,
	Lst: null,
	Seleccionar: null,
	ObtenerPalabra: null,
	FormatearInfo: null,
	RutaImg: "/web/img/",
	seleccionar: function() {
		if( ddl.Seleccionar != null && ddl.Lst != null && ddl.idx < ddl.Lst.length )
			ddl.Seleccionar( ddl.Lst[ddl.idx] )
		ddl.Ocultar()
	},
	Inicializar: function() {
		if( ddl.Creado )
			return

		var markup = '<table cellspacing="0" cellpadding="0" border="0" style="position: relative; z-index: 1; background-color: white"><tr><td></td></tr></table>';
		if(document.all && !window.opera)
			markup = '<iframe id="ddlFrm" style="position: absolute; width: 1000; height: 1000; margin: 0" frameborder="0"></iframe>' + markup
		ddl.popup = document.createElement('div');
		ddl.popup.setAttribute('id', 'ddlPopup');
		ddl.popup.style.overflow = 'hidden'
		ddl.popup.style.position = 'absolute'
		ddl.popup.style.display = 'none';
		ddl.popup.style.zIndex = 100
		ddl.popup.innerHTML = markup;
		document.body.appendChild(ddl.popup);
		ddl.append = ddl.popup.getElementsByTagName('td')[0];
		ddl.Creado = true
	},
	SeleccionEn: function(i) {
		if( ddl.Activo && typeof( document.getElementById("ddlIMG" + i) ) == "object" ) {
			if( typeof( document.getElementById("ddlIMG" + ddl.idx) ) == "object" )
				document.getElementById("ddlIMG" + ddl.idx).src = ddl.RutaImg + "c"
			document.getElementById("ddlIMG" + i).src = ddl.RutaImg + "mas.gif"
			ddl.idx = i
		}
	},
	ActualizarLista: function( lst ) {
		ddl.Inicializar()
		ddl.idx = 0
		if( lst.length <= 0 || lst[0].length == 0 ) {
			ddl.Ocultar()
			return
		}
		var s = "", i, txt, info
		ddl.Lst = []
		for( i = 0; i < lst.length; i++ ) {
			ddl.Lst[i] = lst[i]
			if( ddl.ObtenerPalabra == null )	txt = lst[i]
			else								txt = ddl.ObtenerPalabra( lst[i] )
			if( ddl.FormatearInfo == null )		info = "&nbsp;"
			else								info = ddl.FormatearInfo( lst[i] )
			s += "<tr>"
			s += "<td style='border-bottom:1px solid black;cursor:pointer'"
			s += " onmousedown='ddl.seleccionar()' onmouseover='ddl.SeleccionEn("+i+")'>"
			s += "<img id='ddlIMG" + i + "' src='" + ddl.RutaImg + "c' width=11 height=11>"
			s += "&nbsp;<b>" + ddl.prefijo.toUpperCase() + "</b>" + txt.substr( ddl.prefijo.length ).toUpperCase() + "&nbsp;"
			s += "</td>"
			s += "<td align='right' style='border-bottom:1px solid black;border-left:1px solid black;cursor:pointer'"
			s += " onmousedown='ddl.seleccionar()' onmouseover='ddl.SeleccionEn("+i+")'>" + info + "</td>"
			s += "</tr>"
		}
		ddl.append.innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black'>" + s + "</table>"
		ddl.Activar()
		ddl.SeleccionEn(0)
	},
	Activar: function() {
		if(!ddl.Activo) {
			ddl.popup.style.left = ddl.posX + 'px'
			ddl.popup.style.top  = ddl.posY + 'px'
			ddl.popup.style.display = 'none'
			ddl.popup.style.display = 'block'
			ddl.Activo = true
			idx = 0
		}
	},
	Ocultar: function() {
		var ocultar = function() {
			ddl.popup.style.display = 'none'
			ddl.Activo = false
		}
		if( ddl.Activo )
			setTimeout(ocultar, 100)
	},
	OnKeyDown: function(e) {
		if(e.keyCode) {
			switch(e.keyCode) {
				case 32: // ESPACIO
					if( !ddl.Activo )
						return true
					ddl.Ocultar()
					return true
				case 13: // ENTER
					if( !ddl.Activo )
						return true
					ddl.seleccionar()
					return false;
				case 27: // ESC
					if( !ddl.Activo )
						return true
					ddl.Ocultar()
					return false;
				case 38: // UP
					if( !ddl.Activo )
						return true
					ddl.SeleccionEn( ( ddl.idx + ddl.Lst.length - 1 ) % ddl.Lst.length )
					return false;
				case 9: // TAB
				case 40: // DOWN
					if( !ddl.Activo )
						return true
					ddl.SeleccionEn( ( ddl.idx + 1 ) % ddl.Lst.length )
					return false;
				default:
					break;
			}
		}
		return true
	},
	Posicion: function(x,y) {
		ddl.posX = x
		ddl.posY = y
	}

}
