//============================================================================
// CLASE PARA MANEJO DE CINTAS DE IMAGEN HORIZONTAL EN MC
// ============================================================================

function TextHRibbon(id,nivel)
{
	// ==============================================
	// = CONSTANTES
	// ==============================================
		this.HRDerecha=1;
		this.HRIzquierda=-1;
	// ==============================================
	// = PROPIEDADES
	// ==============================================
		this.nombre="arbNivel" + id;
		this.nCurItem=0;
		this.nItemWidth=1;
		this.nScrollDefault = 0;
		this.nRealWidth = 0;
		this.nNivel = nivel;
		// punteros a objetos -----------
		this.oBefore = document.getElementById(("arbAntes" + id));
		this.oMatRibbon=document.getElementById("arbMarco" + id);
		this.oMatRow=document.getElementById("arbListado" + id);
		this.oAfter = document.getElementById(("arbDespues" + id));
	// ==============================================
	// = METODOS
	// ==============================================
		this.initRibbon = function()
			{
				if(this.oMatRow.children.length>0)
				{
					//Por default oculto las flechas
					this.oBefore.style.display = "none";
					this.oAfter.style.display = "none";
					//Obtengo el scroll por default
					this.nScrollDefault = this.getScrollDefault();
					this.nRealWidth = this.getRealWidth();

					//Si es nivel 1 por default seteo el primer item del arbol como fijo
					if (this.nNivel == 1) {
						oArbolPath.setFijo(this.getObjectLink(0));
					}

					//alert("Nivel: " + this.nNivel + "\nSeleccionado: " + this.oMatRow.seleccionado);

					//Verifico cual es el id seleccionado por default
					if (typeof this.oMatRow.seleccionado != "undefined") {
						var aIds = this.oMatRow.seleccionado.split("|");
						//Selecciono el primero solamente
						if (aIds.length > 1) {
							this.select(aIds[0]);
							//Remuevo el primero
							aIds.shift();
							//Llamo al submenu correspondiente
							arbolDo.llamarSubMenu(this.oMatRow.children[this.nCurItem],aIds.join("|"));
						} else if (aIds.length == 1) {
							if (typeof this.oMatRibbon.nuevaInstancia == "undefined") {
								this.click(aIds[0]);
							}
							/*
							//Si existe algo seleccionado y es distinto de lo que hay en el array entonces
							//le doy importancia a lo que ya estaba seleccionado
							var nIdSelected = this.getIdFromSelected(true);
							alert("IdFromSelected: " + nIdSelected + "\nSeleccionado: " + aIds[0]);
							if (nIdSelected == null || nIdSelected == aIds[0]) {
								this.click(aIds[0]);
							} else {
								alert("Me voy al: " + this.getSelected());
								this.click(this.getSelected());
							}
							*/
						} else {
							//nCurItem
							this.nCurItem=0;
							this.click(this.indexToId(this.nCurItem));
						}
					} else {
						//nCurItem
						this.nCurItem=0;
						this.click(this.indexToId(this.nCurItem));
					}
				}

				cintaAncho= this.getWidth(this.oMatRibbon);
				inicioWin = this.oMatRibbon.scrollLeft;
				finWin = inicioWin + cintaAncho;

				if (inicioWin > 0 ) {
					this.oBefore.style.display = "";
				}
				if (finWin < this.nRealWidth) {
					this.oAfter.style.display = "";
				}

				//Busco si existe este atributo, sino lo agrego. Este atributo me permite identificar si el objeto ya esta siendo usado
				//o es la primera vez que se instancia
				if (typeof this.oMatRibbon.nuevaInstancia == "undefined") {
					this.oMatRibbon.setAttribute("nuevaInstancia","false");
					//this.oMatRibbon.nuevaInstancia = "false";
				} else {
					//marco como current el seleccionado
					this.nCurItem = this.oMatRibbon.getAttribute("lastSelected");
					if (this.nCurItem == null) {
						this.nCurItem = 0;
					}
					this.checkVisibilidad()
					//Actualizo el path del arbol
					if (this.nNivel == 1 && !oArbolPath.isSetFijo()) {
						oArbolPath.setFijo(this.getObjectLink(this.nCurItem));
					} else {
						oArbolPath.setPath(this.nNivel,this.getObjectLink(this.nCurItem));
					}
				}
			};
		this.getCurrentName = function()
			{
				return(this.oMatRow.cells[this.nCurItem].firstChild.innerHTML);
			};
		this.cambiar = function(direccion)
			{
				if(this.oMatRow.children.length>0)
				{
					var nJumpTo = 0;
					if(direccion>0) //va hacia la derecha --------
					{
						nJumpTo = this.oMatRibbon.scrollLeft - this.nScrollDefault;
						if (nJumpTo <= 0) {
							nJumpTo = 0;
							//Oculto la flecha
							this.oBefore.style.display = "none";
						} else {
							this.oBefore.style.display = "";
						}
						this.oAfter.style.display = "";
					}
					else //va hacia la izquierda -----------------
					{
						nJumpTo = this.oMatRibbon.scrollLeft + this.nScrollDefault;
						if (parseInt(this.getWidth(this.oMatRibbon)) + nJumpTo > this.nRealWidth) {
							nJumpTo -= ((parseInt(this.getWidth(this.oMatRibbon)) + nJumpTo) - this.nRealWidth);
							//Oculto la flecha
							this.oAfter.style.display = "none";
						} else {
							this.oAfter.style.display = "";
							//Por las dudas muestro la flecha del inicio
						}
						this.oBefore.style.display = "";
					}
					this.jumpToPixel(nJumpTo);

						if (top.arboldebug) {
						alert(
							"Funcion Cambiar \n" +
							"ScrollLeft: " + this.oMatRibbon.scrollLeft + "\n" +
							"ScrollDefault: " + this.nScrollDefault + "\n" +
							"JumpTo: " + nJumpTo + "\n" +
							"Width: " + parseInt(this.getWidth(this.oMatRibbon)) + "\n" +
							"RealWidth: " + this.nRealWidth
						);
						}

				}
			};
		this.turnto = function(item)
			{
				if(this.oMatRow.children.length>0)
				{
					this.checkVisibilidad();
				}
			};
		this.select = function(id)
			{
				var bReturn = false;
				if(this.oMatRow.children.length>0)
				{
					var nItem = this.idToIndex(id);
					//alert("id: " + id + "\nnItem: " + nItem);
					if (nItem >= 0) {
						//Quito el "selected" al actual
						var sClassName = this.oMatRow.children[this.nCurItem].className;
						this.oMatRow.children[this.nCurItem].className = sClassName.replace(/_selected/g,"");
						//Cambio el current al recibido por parametro
						this.nCurItem=nItem;
						this.oMatRow.children[this.nCurItem].className += "_selected";
						this.turnto(nItem);
						bReturn = true;
						//Actualizo el path del arbol
						if (this.nNivel == 1 && !oArbolPath.isSetFijo()) {
							oArbolPath.setFijo(this.getObjectLink(nItem));
						} else {
							oArbolPath.setPath(this.nNivel,this.getObjectLink(nItem));
						}
						//Guardo el ultimo seleccionado
						this.oMatRibbon.setAttribute("lastSelected",nItem);
						//this.oMatRibbon.lastSelected = nItem;
					}
				}
				return bReturn;
			};
		this.click = function(id)
			{
				if(this.select(id))
				{
					//OJO AL PIOJO
					//Si ya existe una APP cargada en el content entonces NO SE DISPARA EL CLICK
					var oContent = document.getElementById("content");
					//alert("Click del id: " + id);
					if(oContent)
					{
					//alert("oContent: " + oContent.innerHTML);
						if (oContent.innerHTML.indexOf("<") == -1)
						{
							var oLink = this.oMatRow.children[this.nCurItem].getElementsByTagName("A"); //GDR
							if (oLink[0])
							{
								//alert("muestro submenu y ejecuto");
								oLink[0].click();
								//window.status="lo clickeo"+id;
							}
						}
						else
						{
							//alert("no ejecuto pero muestro submenu [["+id);
							arbolDo.llamarSubMenu(this.oMatRow.children[this.idToIndex(id)]);
							//window.status="lo cargo"+id;
						}
					}else{
						//alert("sin content"+id);
						arbolDo.llamarSubMenu(this.oMatRow.children[this.idToIndex(id)]);
						//window.status="lo cargo"+id;
					}
				}else{
					//alert("sin select"+id);
				}
			};
		this.focus = function()
			{
				if(this.oMatRow.children.length>0) this.oMatRow.children[this.nCurItem].className="actual";
			};
		this.blur = function()
			{
				if(this.oMatRow.children.length>0) this.oMatRow.children[this.nCurItem].className=(this.nCurItem==this.nSelItem)?"selected":"item";
			};
		this.reinicializar = function()
			{
				this.oMatRibbon.scrollLeft = 0;
				this.initRibbon();
			};
		this.derecha = function()
			{
				if(this.oMatRow.children.length>0)
				{
					this.cambiar(this.HRDerecha);
					//MCNAV.focus(this.nombre);
				}
			};
		this.izquierda = function()
			{
				if(this.oMatRow.children.length>0)
				{
					this.cambiar(this.HRIzquierda);
					//MCNAV.focus(this.nombre);
				}
			};
		this.getObjectLink = function(nIndex)  {
			var oLink = this.oMatRow.children[nIndex].getElementsByTagName("A"); //GDR
			return oLink[0];
		}

		this.getSelected = function () {
			var nReturn = null;
			for(var i=0; i < this.oMatRow.children.length; i++) {
				var sClassName = this.oMatRow.children[i].className;
				if (sClassName.indexOf("_selected")) {
					nReturn = i;
					break;
				}
			}
			return nReturn;
		}
		this.getIdFromSelected = function (bLast) {
			var nReturn = null;
			for(var i=0; i < this.oMatRow.children.length; i++) {
				var sClassName = this.oMatRow.children[i].className;
				if (sClassName.indexOf("_selected")) {
					nReturn = this.oMatRow.children[i].id.replace(/item/g,"");
					if (typeof bLast != "undefined" && !bLast) {
						break;
					}
				}
			}
			return nReturn;
		}
	// ==============================================
	// = MANEJO INTERNO
	// ==============================================
		this.checkVisibilidad = function()
			{
				if (this.oMatRow.children[this.nCurItem]) {
					inicioItem = this.oMatRow.children[this.nCurItem].offsetLeft;
					finItem = inicioItem + this.getWidth(this.oMatRow.children[this.nCurItem]);

					cintaAncho= this.getWidth(this.oMatRibbon);

					inicioWin = parseInt(this.oMatRibbon.scrollLeft);
					finWin = inicioWin + cintaAncho;

					//alert("inicioItem: " + inicioItem + "\nfinItem: " + finItem + "\ncintaAncho: " + cintaAncho + "\ninicioWin: " + inicioWin + "\nfinWin: " + finWin);

					if (inicioItem < inicioWin) {
						this.oMatRibbon.scrollLeft = inicioItem;
					} else {
						if (finItem >= finWin) {
							this.oMatRibbon.scrollLeft = finItem - cintaAncho;
						}
					}

					//porque quizas se cambiaron arriba
					inicioWin = parseInt(this.oMatRibbon.scrollLeft);
					finWin = inicioWin + cintaAncho;

					//alert("inicioItem: " + inicioItem + "\nfinItem: " + finItem + "\ncintaAncho: " + cintaAncho + "\ninicioWin: " + inicioWin + "\nfinWin: " + finWin + "\nRealWidth: " + this.getRealWidth());

					if (inicioWin > 0 ) {
						this.oBefore.style.display = "";
					} else {
						this.oBefore.style.display = "none";
					}
					if (finWin < this.getRealWidth()) {
					//if (finWin < this.nRealWidth) {
						this.oAfter.style.display = "";
					} else {
						this.oAfter.style.display = "none";
					}
				}
				if (top.arboldebug) {
					alert ("funcion checkvisibilidad\n"+
					       "inicioWin:" + inicioWin + "\n" +
						"finwin:" + finWin +"\n" +
						"realWidth:" + this.getRealWidth());
				}
			};
			this.jumpToPixel = function(pixel)
				{
					var p1 = parseInt(this.oMatRibbon.scrollLeft);
					var p2 = parseInt(pixel);
					var paso = 0.5/(100); //delta PI
					for(t=1.5; t<=2; t+=paso) //subo por cuarto cuadrante
						this.oMatRibbon.scrollLeft=p1+(p2-p1)*Math.cos(t*Math.PI);
				};
			this.idToIndex = function(id)
				{
					var nReturn = -1;
					for(var i=0; i < this.oMatRow.children.length; i++) {
						if (id == parseInt(this.oMatRow.children[i].id.replace(/item/g,""))) {
							nReturn = i;
							break;
						}
					}
					return nReturn;
				}
			this.indexToId = function(index) {
				var nReturn = -1;
				if (this.oMatRow.children.length > 0) {
					nReturn = parseInt(this.oMatRow.children[index].id.replace(/item/g,""));
				}
				return nReturn;
			}
	// ==============================================
	// = CALCULO DE TAMANIO
	// ==============================================
		this.getWidth = function(obj)
			{
				//if(obj.nWidth=undefined || refresh) this.setProperties(obj);
				this.setProperties(obj);
				return(obj.nWidth);
			};
		this.getHeight = function(obj)
			{
				if(obj.nHeight==undefined) this.setProperties(obj);
				return(obj.nHeight);
			};
		this.setProperties = function(obj)
			{
				objPosition=obj.getBoundingClientRect()
				obj.nLeftPos = objPosition.left -2
				obj.nRightPos = objPosition.right -2
				obj.nTopPos = objPosition.top - 2
				obj.nBottomPos = objPosition.bottom -2
				obj.nWidth = (obj.nRightPos - obj.nLeftPos)
				obj.nHeight = (obj.nBottomPos - obj.nTopPos)
			};
		this.getScrollDefault = function()
			{
				var nReturn = 0;
				for(var i = 0; i < this.oMatRow.children.length; i++) {
					if (this.getWidth(this.oMatRow.children[i]) > nReturn) {
						nReturn = this.getWidth(this.oMatRow.children[i]);
					}
				}
				return nReturn;
			}
		this.getRealWidth = function()
			{
				/*
				var nReturn = 0;
				for(var i = 0; i < this.oMatRow.children.length; i++) {
					nReturn += parseInt(this.getWidth(this.oMatRow.children[i]));
				}
				//alert("RealWidth: " + nReturn + "MatRow.Width: " + this.getWidth(this.oMatRow));
				*/
				return this.getWidth(this.oMatRow);
			}

	// ==============================================
	// = INICIALIZACION
	// ==============================================
		this.initRibbon();
}

//alert("TextHRibbon 1.0.001");