var FlechaUp = 'FlechaUp'
var FlechaDown = 'FlechaDown'
var OutLayer = 'OutLayer'
var InLayer = 'InLayer'

var tup=null, tdown=null, stillup=0, stilldown=0;
var despl=2;
var velocidad=1;
var min=1;
var max=20;
var top,bottom,OffsetX,OffsetY,cogido='',tamOut,tamIn;

function inicioScroll(){
	document.onmousedown = mouseDown
	document.onmouseup = mouseUp
	tamOut=parseInt(document.getElementById('OutLayer').offsetHeight, 10);
	scactualizar();
}

function scactualizar(){
	var actpos;
	
	tamIn = parseInt(document.getElementById('InLayer').offsetHeight ,10);
	max = tamOut - tamIn

	if (max<0){				
		document.getElementById('FlechaUp').style.display='block';
		document.getElementById('FlechaDown').style.display='block';
	}
	else{
		document.getElementById('FlechaUp').style.display='none';
		document.getElementById('FlechaDown').style.display='none';
	}
}

function mouseDown(e){	
	try{
		var x = event.clientX + document.body.scrollLeft - 2;
		var y = event.clientY + document.body.scrollTop - 2;
		
		switch(QuePulsa(x,y)){
			case 'FlechaUp':
				subir()
				cogido=FlechaUp;			
				return false;
			case 'FlechaDown':
				bajar()
				cogido=FlechaDown;
				return false;
		}
		return true;
	}
	catch(error){
		var x = e.pageX
		var y = e.pageY
		
		switch(QuePulsa(x,y)){			
			case 'FlechaUp':
				subir()
				cogido=FlechaUp;			
				return false;
			case 'FlechaDown':
				bajar()
				cogido=FlechaDown;
				return false;
		}
		return true;
	}
}

function mouseMove(e){}

function mouseUp(e){
	if (cogido=='') return true;
	stillup=0;
	stilldown=0
	cogido='';
	return false;
}

function QuePulsa(x,y){
	if (isInLayer(x,y,FlechaUp)) return (FlechaUp);
	if (isInLayer(x,y,FlechaDown)) return (FlechaDown);
	return ('');
}

function isInLayer(x,y,ncapa){
	var l,r,t,b;

	l=parseInt(document.getElementById(ncapa).offsetLeft,10);
	r=l+parseInt(document.getElementById(ncapa).clientWidth,10);
	t=parseInt(document.getElementById(ncapa).offsetTop,10);
	b=t+parseInt(document.getElementById(ncapa).clientHeight,10);

	if ((x>=l && x<=r) && (y>=t && y<=b)) return true;
	else return false;	
}

function rsubir(){
	var actpos;
	var documento=document.getElementById('InLayer').style;
	actpos=parseInt(documento.top,10);

	if ((actpos + despl)<min){
		documento.top=actpos + despl;
	}
	else{
		documento.top=min;
	}

	if (stillup==1) tup=setTimeout('rsubir()',velocidad);
	else clearTimeout(tup);
}

function rbajar(){
	var actpos;
	var documento=document.getElementById('InLayer').style;
	actpos=parseInt(documento.top,10);

	if ((actpos - despl)>max){
		documento.top=actpos - despl;
	}
	else{
		documento.top=max;
	}
	if (stilldown==1) tdown=setTimeout('rbajar()',velocidad);
	else clearTimeout(tdown);
}

function subir(){
	if (stillup==0){
		stillup=1;
		tup=setTimeout('rsubir()',velocidad);
	}
}

function bajar(){
	if (stilldown==0){
		stilldown=1;
		tdown=setTimeout('rbajar()',velocidad);
	}
}
