var dw_event = {
  
  add: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
    else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
  }, 

  remove: function(obj, etype, fp, cap) {
    cap = cap || false;
    if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
    else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
  }, 

  DOMit: function(e) { 
    e = e? e: window.event;
    e.tgt = e.srcElement? e.srcElement: e.target;
    
    if (!e.preventDefault) e.preventDefault = function () { return false; }
    if (!e.stopPropagation) e.stopPropagation = function () { if (window.event) window.event.cancelBubble = true; }
        
    return e;
  }

}






function Scroll(sIdScroll, sIdContenido, iAlturaContenido){
	// Atributos Privados
	this.sIdScroll		= sIdScroll;
	this.sIdContenido	= sIdContenido;
	
	this.oScroll 	= document.getElementById(this.sIdScroll);
	if (this.oScroll.style.top == "") this.oScroll.style.top 		= "0px";
		
	this.oContenido = document.getElementById(this.sIdContenido);
	if (this.oContenido.style.top == "") this.oContenido.style.top 	= "0px";
	
	this.iHeight	= iAlturaContenido;
	this.iSpeed		= 5;
	this.iIncremento= this.iHeight * this.iSpeed /(this.oContenido.offsetHeight - this.iHeight);
	this.iMinTop	= parseInt(this.oScroll.style.top);
	this.iMaxTop	= parseInt(this.oScroll.style.top) + this.iHeight;
	this.iScrollAux = 0;
	this.iMoveDown	= 0;
	this.iMoveUp	= 0;
	this.iActualPos	= 0;
	this.iX			= 0;
	this.iY			= 0;
	this.bIsDown	= false;
	this.bIsOver	= false;
	
	// Metodos
	this.setSpeed	= setSpeed;
	this.getSpeed	= getSpeed;
	this.getIdScroll 	= getIdScroll;
	this.getIdContainer	= getIdContainer;
	this.scrollUp	= scrollUp;
	this.scrollDown	= scrollDown;
	this.stopScroll	= stopScroll;
	this.startDrag	= startDrag;
	this.stopDrag	= stopDrag;
	this.drag		= drag;
	this.setOver	= setOver;
}

function setSpeed(iSpeed){
	this.iSpeed = iSpeed;
}

function getSpeed(){
	return this.iSpeed;
}

function scrollDown(){

	this.iScrollAux += this.iIncremento;
		
	if (parseInt(this.oContenido.style.top) >= ((-1) * this.oContenido.offsetHeight + this.iMaxTop)){
		// Desplazar el contenido
		this.oContenido.style.top = parseInt(this.oContenido.style.top) - this.iSpeed + "px";
		
		if (this.iScrollAux >= 1){
			if ((parseInt(this.oScroll.style.top) + parseInt(this.iScrollAux)) <= this.iMaxTop){
				// Desplazar la barra de scroll
				this.oScroll.style.top = parseInt(this.oScroll.style.top) + parseInt(this.iScrollAux) + "px";
				//this.iActualPos	+= this.iScrollAux;
				this.iScrollAux = this.iScrollAux - parseInt(this.iScrollAux);
			}
		}
	}
	
	oInstanciaActual = this;
	this.iMoveDown = setTimeout(
		function() {
			oInstanciaActual.scrollDown(oInstanciaActual.getIdContainer(), oInstanciaActual.getIdScroll());
		}, 20);
}

function scrollUp(){
	this.iScrollAux += this.iIncremento;

	if (parseInt(this.oContenido.style.top) <= 0){
		// Desplazar el contenido
		this.oContenido.style.top = parseInt(this.oContenido.style.top) + this.iSpeed + "px";

		if (this.iScrollAux >= 1){
			if ((parseInt(this.oScroll.style.top) - parseInt(this.iScrollAux)) >= this.iMinTop){
				// Desplazar la barra de scroll
				this.oScroll.style.top = parseInt(this.oScroll.style.top) - parseInt(this.iScrollAux) + "px"; 
				//this.iActualPos	-= this.iScrollAux;
				this.iScrollAux = this.iScrollAux - parseInt(this.iScrollAux);
			}
		}
	}
	
	oInstanciaActual = this;
	this.iMoveUp = setTimeout(
		function() {
			oInstanciaActual.scrollUp(oInstanciaActual.getIdContainer(), oInstanciaActual.getIdScroll());
		}, 20);
}

function stopScroll(){
	this.iScrollAux = 0;
	
	clearTimeout(this.iMoveDown);
	clearTimeout(this.iMoveUp);
}

function getIdScroll(){
	return this.sIdScroll;
}

function getIdContainer(){
	return this.sIdContenido;
}

function startDrag(aEvent){
	aEvent = dw_event.DOMit(aEvent);
	var bIsMozilla = (document.all) ? 0 : 1;
	var oThisScroll = this;
	
	if (bIsMozilla) {
		this.iX = aEvent.layerX;
		this.iY = aEvent.layerY;
		this.bIsDown = true;
	} else {
		this.iX = aEvent.offsetX;
		this.iY = aEvent.offsetY;
		this.bIsDown = true;
	}
	
	dw_event.add( document, "mousemove", oThisScroll.drag, true );
    dw_event.add( document, "mouseup",   oThisScroll.stopDrag,  true );
    aEvent.stopPropagation();
	return false;
	/*
	var myEvent = aEvent ? aEvent : window.event;
	var bIsMozilla = (document.all) ? 0 : 1;


	//if (this.bIsOver) {
		if (bIsMozilla) {
			this.iX = myEvent.layerX;
			this.iY = myEvent.layerY;
			this.bIsDown = true;
			return false;
		} else {
			this.iX = myEvent.offsetX;
			this.iY = myEvent.offsetY;
			this.bIsDown = true;
		}
	//}
	*/
}

function stopDrag(){
	var oThisScroll = this;

	dw_event.remove( document, "mousemove", oThisScroll.drag, true );
    dw_event.remove( document, "mouseup",   oThisScroll.stopDrag,  true );
	//this.bIsDown = false;
}

function drag(aEvent){
	var myEvent = aEvent ? aEvent : window.event;
	var bIsMozilla = (document.all) ? 0 : 1;
	
	//if (this.bIsDown) {
		if (bIsMozilla) {
			this.oScroll.style.top 	= myEvent.pageY - this.iY;
			this.oScroll.style.left = myEvent.pageX - this.iX;
		} else {
			this.oScroll.pixelLeft 	= myEvent.clientX - this.iX + document.body.scrollLeft;
			this.oScroll.pixelTop 	= myEvent.clientY - this.iY + document.body.scrollTop;
	  	}
	//}
	
	return false;
}

function setOver(bMode){
	this.bIsOver = bMode;
}