/* ===========================================================================================
 * Programa   : ScrollBar.js
 * Descripción: Permite manejar una barra de scroll personalizable
 * Creado por : Jcruz
 * Creado en  : 16/05/2004
 *
 * -------------------------------------------------------------------------------------------
 *  		    Actualizaciones
 * -------------------------------------------------------------------------------------------
 * Versión     : 1.1
 * Fecha       : 22/05/2004
 * Analista    : jcruz
 * Descripción : Ocultar la capsula cuando no sea necesaria para scroll
 * ===========================================================================================*/

// Cambia la direccion del scroll moviendose mientras se pulsa el boton
function objswitchIt(obj, cmd)
{
	with (this) {
		if (cmd == 'in')
		{
			mouseover = true;
			if (obj.getAttribute('id') == prefijo + "_scrollup")
			{
				scrolldirection = -1;
			}
			else
			{
				scrolldirection = 1;
			}
		}
		else
		{
			mouseover = false;
		}
	}	
}

//*** Maneja el desplazamiento de la capsula
function objscroll()
{
	with (this) {
		if (((scrollbox.style.pixelTop + menuOffsetTop + scrolldirection) >= menuOffsetTop) && ((scrollbox.style.pixelTop + menuOffsetTop + scrolldirection) <= (menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight)))
		{
			scrollbox.style.pixelTop += scrolldirection;
			//window.status=scrollbox.style.pixelTop;
		} 
	}	
}

//*** Maneja el desplazamiento de la data en la zona de scroll
function objheartBeat()
{
	with (this) {
		data.style.pixelTop = -1 * scrollratio * (scrollbox.style.pixelTop + menuOffsetTop - menuOffsetTop);
		//window.status=data.style.pixelTop;
		//*** Maneja el repintar de las imagenes del catalogo por la combinacion
		//*** de la barra de scroll y el arrastrar y soltar
		try {
			ddGlobal.recalc();
		} catch(e){}
	}	
}

function objgrabIt(e) {
	with (this) {
		//if (event.button == 1)
		{
			if (mouseover)
				scrolling = window.setInterval(prefijo +".scroll();",1);
			else
			{
				whichIt = event.srcElement;
				while (whichIt.id.indexOf(prefijo + "_scrollbox") == -1) {
					whichIt = whichIt.parentElement;
					if (whichIt == null) { return true; }
			    }
	//			whichIt.style.pixelLeft = whichIt.offsetLeft;
			    whichIt.style.pixelTop = whichIt.offsetTop;
				currentX = (event.clientX + document.body.scrollLeft);
				currentY = (event.clientY + document.body.scrollTop);
			}
		
			theheartbeat = window.setInterval(prefijo +".heartBeat()",1);
		}
	}	
}

function objmoveIt(e) {
	with (this) {
		if (whichIt == null) { return false; }
		newX = (event.clientX + document.body.scrollLeft);
	    newY = (event.clientY + document.body.scrollTop);
	    distanceX = (newX - currentX);
		distanceY = (newY - currentY);
	    currentX = newX;
		currentY = newY;
		if (((scrollbox.style.pixelTop + menuOffsetTop + distanceY) >= menuOffsetTop) && ((scrollbox.style.pixelTop + menuOffsetTop + distanceY) <= (menuOffsetTop + menuMaxHeight - scrollbox.style.pixelHeight)))
		{
	    	scrollbox.style.pixelTop += distanceY;
		}
		
		event.returnValue = false;
		//try {
		//	document.getElementById("Aimg0");
		//window.status=ddGlobal.name;
		//DROP(e);
		
		//document.getElementById("Aimg0divNImG").moveTo(document.getElementById("Aimg0divNImG").defx, document.getElementById("Aimg0divNImG").defy);
		//} catch(e) {}	
		return false;
	}	
}

function objdropIt() {
	with (this) {
		clearInterval(scrolling);
		clearInterval(theheartbeat);
		whichIt = null;
	}	
}

function objinit()
{
	with (this) {
		//*** Apuntador a cada elemento que componen el scroll del objeto actual
		floater=document.getElementById(prefijo + "_floater");
		scrollbox=document.getElementById(prefijo + "_scrollbox");
		scrollbox2=document.getElementById(prefijo + "_scrollbox2");
		content=document.getElementById(prefijo + "_content");
		data=document.getElementById(prefijo + "_data");
		scrollup=document.getElementById(prefijo + "_scrollup");
		scrolldown=document.getElementById(prefijo + "_scrolldown");
		
		//*** Guardamos el tope inicial de la capsula y de la zona de contenido sin scrollear
		topeorig=data.style.top;
		
		//*** Ajustar el cursor a tipo mano para la capsula y las flechas arriba y abajo
		scrollup.style.cursor="hand";
		scrolldown.style.cursor="hand";
		scrollbox.style.cursor="hand";
		
		//*** Asociar a la caja de scroll los eventos de manejo del mouse para el objeto actual		
		scrollbox.onmousedown =new Function(prefijo + ".grabIt()");
		scrollbox.onmousemove =new Function(prefijo + ".moveIt()");
		scrollbox.onmouseup = new Function(prefijo + ".dropIt()");

		//*** Asociar a la flecha arriba los eventos de manejo del mouse para el objeto actual				
		scrollup.onmousedown =new Function(prefijo + ".grabIt()");
		scrollup.onmouseup = new Function(prefijo + ".dropIt()");
		scrollup.onmouseover=new Function(prefijo + ".switchIt(this, 'in')");
		scrollup.onmouseout=new Function(prefijo + ".switchIt(this, 'out')");
		
		//*** Asociar a la flecha abajo los eventos de manejo del mouse para el objeto actual						
		scrolldown.onmousedown =new Function(prefijo + ".grabIt()");
		scrolldown.onmouseup = new Function(prefijo + ".dropIt()");
		scrolldown.onmouseover=new Function(prefijo + ".switchIt(this, 'in')");
		scrolldown.onmouseout=new Function(prefijo + ".switchIt(this, 'out')");
		
		getscrollinc();
	}
}

function objgetscrollinc()
{
	with (this) {
	    menuOffsetTop = floater.style.pixelTop;
		menuMaxHeight = floater.style.pixelHeight;
		
		var contentinc;
		var scrollerinc;
		var contentHeight = data.offsetHeight;
		var scrollerHeight = content.style.pixelHeight;
		
		if (scrollerHeight < contentHeight)
		{
			//*** La capsula no ocupa el espacio completo y se necesita para scroll
			//*** se activa
			scrollbox.style.visibility="visible";
			contentinc = content.style.pixelHeight / contentHeight;
			scrollerinc = floater.style.pixelHeight*contentinc;
			scrollratio = contentHeight / floater.style.pixelHeight;
		}
		else
		{
			//*** La capsula ocupa el espacio completo y no se necesita para scroll
			//*** se apaga
			scrollbox.style.visibility="hidden";
			scrollerinc = floater.style.pixelHeight;
			contentinc = 1;
		}
		//*** Ajusta la posicion de la barra al tope del contenido
//		scrollbox.style.top=data.style.top;
		scrollbox.style.top=topeorig;
		//*** Ajusta el tamaño de la capsula su radio de accion
		scrollbox.style.height = scrollerinc;
		//*** Ajusta el tamaño de la capsula en si
		scrollbox2.style.height = Math.max(1,scrollerinc-13);
	}	
}

function crearScroll(nombre){
	this.prefijo=nombre;
	
	// variables
	this.mouseover = false;
	this.scrolldirection=0;
	this.scrollratio=0;
	this.theheartbeat=0;
	this.scrolling=0;
	this.menuOffsetTop = this.menuOffsetLeft = this.menuMaxHeight = 0;
	this.currentX = this.currentY = 0;
	this.whichIt = null;
	this.topeorig=0;
	
	this.floater=null;
	this.scrollbox=null;
	this.scrollbox2=null;
	this.content=null;
	this.data=null;
	this.scroollup=null;
	this.scroolldown=null;
	
	this.init=objinit;
	this.grabIt=objgrabIt;
	this.dropIt=objdropIt;
	this.moveIt=objmoveIt;
	this.switchIt=objswitchIt;
	this.heartBeat=	objheartBeat;
	this.scroll=objscroll;
	this.getscrollinc=objgetscrollinc;
}



