/* ----------------------------------------------------------------
	----------------------------------------------------------------
	--  Manejador de capas
	--  Funciones de ayuda/soporte de capas.
	----------------------------------------------------------------
	---------------------------------------------------------------- */
function WBEDivManager(sId)
{
	/* Constantes	 */
	this.TAG_NAME = "DIV";

	/* Propiedades	 */
	this.id = sId;
	this.div = new Object();
	this.browserCheck = new WBECheckBrowser();

	/* Tamaño y posicion */
	this.x0 = 0;
	this.y0 = 0;
	this.x1 = 0;
	this.y1 = 0;
	this.w = 0;
	this.h = 0;

	/* Devuelve el valor de un entero de forma segura */
	this.Int = function (d_x, d_y)
	{	return isNaN(d_y = parseInt(d_x)) ? 0 : d_y; };

	/*  --- Posicionamiento y dimensiones --- */

	/* Inicializa el objeto con dimensiones tal como esta. */
	this.reset  = function() {
		this.resetPageXY();
		this.resetWH();
	};

	/* Oculta la capa */
	this.hide = function() {
		this.div.style.display = "none";
	};

	/* Muestra la capa */
	this.show = function() {
		this.div.style.display = "block";
	};
	
	/* Inicializa los valors XY de esta capa */
	this.resetPageXY = function()
	{
		var object = this.div;
		this.x0 = this.y0 = 0;
		if (object.offsetLeft != undefined) {
			while(object) {
				this.x0 += object.offsetLeft;
				this.y0 += object.offsetTop;
				object = object.offsetParent || null;
			}
		} else {
			this.x0 = object.pageX || 0;
			this.y0 = object.pageY || 0;
		}
	};

	/* Reset del ancho y alto de la capa */
	this.resetWH  = function() {
		if (this.div.clip) this.w = this.Int(this.div.clip.width);
		else if (this.div.offsetWidth!=undefined) this.w = this.Int(this.div.offsetWidth);
		else if (this.div.css.pixelWidth!=undefined) this.w = this.Int(this.div.css.pixelWidth);
		else if (this.div.css.width!=undefined) this.w = this.Int(this.div.css.width);
		else this.w=0;
 		this.x1 = this.x0 + this.w;

		if (this.div.clip) this.h = this.Int(this.div.clip.height);
		else if (this.div.offsetHeight!=undefined) this.h = this.Int(this.div.offsetHeight);
		else if (this.div.css.pixelHeight!=undefined) this.h = this.Int(this.div.css.pixelHeight);
		else if (this.div.css.height!=undefined) this.h = this.Int(this.div.css.height);
		else this.h=0;
		this.y1 = this.y0 + this.h;
	};

	/* Mueve una capa a la nueva posicion indicada. */
	this.moveToXY = function (new_x, new_y) {
		this.isXYPosition = true;
		this.div.style.position = "absolute";
		this.div.style.top = new_y + 'px';
		this.div.style.left = new_x + 'px';
		this.resetPageXY();			// Calcula posición nueva
	};

	/* Redimensiona una capa (si alguno de los valores es 0 lo pone al 100x100 */
	this.resize = function (new_w, new_h) {
		if (this.div.resizeTo)	{
			this.div.resizeTo(new_w, new_h);
		} else if (this.div.pixelWidth) {
			this.div.pixelWidth = new_w;
			this.div.pixelHeight = new_h;
		} else {
		if (new_w!=undefined)
			this.div.style.width = (new_w) ? new_w + 'px' : null;
		if (new_h!=undefined)
			this.div.style.height = (new_h) ? new_h + 'px' : null;
		}
		this.resetWH();			// Calcula nuevos tamaños
	};

	/* Nos devuelve si una coordenada pertenece a una capa. */
	this.isOver = function (cX, cY) {
		//alert(cX + "-" + cY + ", " + this.x0 + "-" + this.y0 + "-" + this.x1 + "-" + this.y1)
		return ( (this.x0 <= cX && cX <= this.x1) &&
				(this.y0 <= cY && cY <= this.y1) );
	};

	/* /////////// Gestión de hijos /////////////// */

	/* Devuelve el indice que ocupa un objeto entre los hijos. */
	this.getChildIndex  = function (oObject) {
 		var childs;
		var idx = 0;
		var i = 0;
		if (this.div.hasChildNodes())
		{
			childs = (this.div.children || this.div.childNodes);
			while (i < childs.length) {
				if (childs[i].tagName==this.TAG_NAME && childs[i].id!=undefined) {
					if (childs[i]==oObject || childs[i]==oObject.div)	return idx;
					idx++;
				}
				i++;
			}
		}
		return idx;
	};

	/* Devuelve el hijo que ocupa la posición Idx y que tiene
		de nombre de etiqueta sTagName como un HtmlDIV */
	this.getChildAt = function (idx) {
		var childs;
		var iDivCount;
		if (this.div.hasChildNodes()) {
			childs = (this.div.children || this.div.childNodes);
			iDivCount = 0;
			for (var i=0; i<childs.length; i++) {
				if (childs[i].tagName!=this.TAG_NAME || childs[i].id==undefined) continue;
				if (iDivCount==idx) return childs[i];
				iDivCount++;
			}
		}
		return null;
	};

	/* Inserta el objeto oObject de tipo WBEDivManager o Div como hijo en la posición idx
		Si no existe elemento en idx lo inserta el último. */
	this.insertChildAt = function(oObject, idx) {
		var oTargetDiv;
		/* 		if (oTargetLayer!=wbe_zone_list.AbsZone.obj) {
  					resizeDiv(oObject, "100%", "");
	   				oObject.style.position = "relative";
					oObject.style.top = 0;
					oObject.style.left = 0;
  				}
		*/
  		// Obtiene el elemento que reemplazará en la capa destino
		oTargetDiv = this.getChildAt(idx)
  		if (oObject.div) {	// De Tipo WBEDivManager
			if (oTargetDiv) {
				this.div.insertBefore(oObject.div, oTargetDiv);
			 } else
				this.div.appendChild(oObject.div);
			oObject.resetPageXY();	// Calcula posición nueva
		} else if (oObject) {	// De tipo Div
			if (oTargetDiv)
				this.div.insertBefore(oObject, oTargetDiv);
			else
				this.div.appendChild(oObject);
		}
	};


	/* Devuelve la capa TAG_NAME hija sobre la que está situada el cursor.
	// La devuelve como elemento HtmlDiv **** */
	this.getChildBelowMouse = function(mX, mY) {
		var childs;		// colección de hijos
		var o_child;	// hijo que procesamos
		var ichild_index;
		if (this.div.hasChildNodes()) {
			childs = (this.div.children || this.div.childNodes);
			ichild_index = 0;
			for (var i=0; i<childs.length; i++) {
				o_child = childs[i];
				if ( o_child.tagName != this.TAG_NAME || o_child.id==undefined) continue;
				o_child = new WBEDivManager(o_child.id);
				//alert(mX + "-" + mY + "-" + o_child.id + "---" + o_child.x0 + "-" + o_child.y0 + "-" + o_child.x1 + "-" + o_child.y1);
				if (o_child.isOver(mX, mY)) {
					o_child.parentIndex = ichild_index;
					return o_child;
					}
				ichild_index++;
			} // for
		} // if
		return null;
	}

	/* Devuelve el último hijo de tipo TAG_NAME de una capa. */
	this.getLastChild = function() {
		var childs;		// colección de hijos
		var o_child;	// hijo que procesamos
		var oLastChild = null;
		if (this.div.hasChildNodes()) {
			childs = (this.div.children || this.div.childNodes);
			for (var i=0; i<childs.length; i++) {
				o_child = childs[i];
				if ( o_child.tagName != this.TAG_NAME || o_child.id==undefined) continue;
				oLastChild = new WBEDivManager(o_child.id);
			} // for
		} // if
		return oLastChild;
	}


	/* Scroll de la ventana para permitir dragdrop */
	this.scrollPage = function(pos_xy) {
		var oSize = getWindowSize();
		var oScroll = getWindowScrollXY();
		var oPageHeight = oScroll[1] + oSize[1];
		var oPageWidth = oScroll[0] + oSize[0];
		
		if (oScroll[1]>0 && oScroll[1] <= pos_xy.y && pos_xy.y< oScroll[1]+10)
			window.scrollTo(oScroll[0], oScroll[1]-5);
		else if (oPageHeight-10 < pos_xy.y && pos_xy.y<= oPageHeight)
			window.scrollTo(oScroll[0], oScroll[1]+5);
			
		if (oScroll[0]>0 && oScroll[0] <= pos_xy.x && pos_xy.x< oScroll[0]+10)
			window.scrollTo(oScroll[0]-5, oScroll[1]);
		else if (oPageWidth-10 < pos_xy.x && pos_xy.x<=oPageWidth)
			window.scrollTo(oScroll[0]+5, oScroll[1]);
	}
	
  /* /////////// Inicializa los valores /////////////// */
  this.div = document.getElementById(this.id);
  try {
	this.reset();
  } catch (e) {}
}
