var upH = 16; // Height of up-arrow
var upW = 18; // Width of up-arrow
var downH = 16; // Height of down-arrow
var downW = 18; // Width of down-arrow
var dragH = 16; // Height of scrollbar
var dragW = 18; // Width of scrollbar
var scrollH = 152; // Height of scrollbar
var listaW = 300;
var listaH = 182;
var speed = 4; // Scroll speed

// And now... go to the bottom of the page...

// Browser detection
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;

var mouseY; // Mouse Y position onclick
var mouseX; // Mouse X position onclick

var clickUp = false; // If click on up-arrow
var clickDown = false; // If click on down-arrow
var clickDrag = false; // If click on scrollbar
var clickAbove = false; // If click above scrollbar
var clickBelow = false; // If click below scrollbar

var timer = setTimeout("",500); // Repeat variable
var upL; // Up-arrow X
var upT; // Up-arrow Y
var downL; // Down-arrow X
var downT = 0; // Down-arrow Y
var dragL; // Scrollbar X
var dragT; // Scrollbar Y
var rulerL; // Ruler X
var rulerT; // Ruler Y
var contentT = 0; // Content layer Y;
var contentH; // Content height
var contentClipH; // Content clip height
var scrollLength; // Number of pixels scrollbar should move
var startY; // Keeps track of offset between mouse and span

var ckPlusName = "";

//Es necesario cambiar manuamente la anchura del 'clip' en contentClip
function nueDim(x,y)
{
    listaW=x;
    listaH=y;
}
// Mousedown
function down(e){
	if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
	getMouse(e);
	startY = (mouseY - dragT);
	// If click on up-arrow
	if(mouseX >= upL && (mouseX <= (upL + upW + 7)) && mouseY >= upT && (mouseY <= (upT + upH))){
		clickUp = true;
		return scrollUp();
	}
	// Else if click on down-arrow
	else if(mouseX >= downL && (mouseX <= (downL + downW + 7)) && mouseY >= downT && (mouseY <= (downT + downH))){
		clickDown = true;
		return scrollDown();
	}
	// Else if click on scrollbar
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW + 7)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
		clickDrag = true;
		return false;
	}
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW + 7)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
		// If click above drag
		if(mouseY < dragT){
			clickAbove = true;
			clickUp = true;
			return scrollUp();
		}
		// Else click below drag
		else{
			clickBelow = true;
			clickDown = true;
			return scrollDown();
		}
	}
	// If no scrolling is to take place
	else{
		return true;
	}
}

// Drag function
function move(e){
	if(clickDrag && contentH > contentClipH){
		getMouse(e);
		dragT = (mouseY - startY);

		if(dragT < (rulerT))
			dragT = rulerT;
		if(dragT > (rulerT + scrollH - dragH))
			dragT = (rulerT + scrollH - dragH);

		contentT = ((dragT - rulerT)*(1/scrollLength));
		contentT = eval('-' + contentT);

		moveTo();

		// So ie-pc doesn't select gifs
		if(ie4)
			return false;
	}
}

function up(){
	clearTimeout(timer);
	// Resetting variables
	clickUp = false;
	clickDown = false;
	clickDrag = false;
	clickAbove = false;
	clickBelow = false;
	return true;
}

// Reads content layer top
function getT(){
	if(ie4)
		contentT = document.all.content.style.pixelTop;
	else if(nn4)
		contentT = document.contentClip.document.content.top;
	else if(dom)
		contentT = parseInt(document.getElementById("content").style.top);
}

// Reads mouse X and Y coordinates
function getMouse(e){
	if(ie4){
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}
	else if(nn4 || dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
}

// Moves the layer
function moveTo(){
	if(ie4){
		document.all.content.style.top = contentT;
		document.all.ruler.style.top = dragT;
		document.all.drag.style.top = dragT;
	}
	else if(nn4){
		document.contentClip.document.content.top = contentT;
		document.ruler.top = dragT;
		document.drag.top = dragT;
	}
	else if(dom){
		document.getElementById("content").style.top = contentT + "px";
		document.getElementById("drag").style.top = dragT + "px";
		document.getElementById("ruler").style.top = dragT + "px";
	}
}

// Scrolls up
function scrollUp(){
	getT();

	if(clickAbove){
		if(dragT <= (mouseY-(dragH/2)))
			return up();
	}

	if(clickUp){
		if(contentT < 0){
			dragT = dragT - (speed*scrollLength);

			if(dragT < (rulerT))
				dragT = rulerT;

			contentT = contentT + speed;
			if(contentT > 0)
				contentT = 0;

			moveTo();
			timer = setTimeout("scrollUp()",25);
		}
	}
	return false;
}

// Scrolls down
function scrollDown(){
	var aux;

	getT();

	if(clickBelow){
		if(dragT >= (mouseY-(dragH/2)))
			return up();
	}

	if(clickDown){
		if(contentT > -(contentH - contentClipH)){

			aux = speed * scrollLength;
			dragT = dragT - (-1 * aux);

			if(dragT > (rulerT + scrollH - dragH))
				dragT = (rulerT + scrollH - dragH);

			contentT = contentT - speed;
			if(contentT < -(contentH - contentClipH))
				contentT = -(contentH - contentClipH);

			moveTo();
			timer = setTimeout("scrollDown()",25);
		}
	}
	return false;
}

// reloads page to position the layers again
function reloadPage(){
	location.reload();
}

function saveEstadoScroll()
{

  	var ckName;

	if (navigator.cookieEnabled)
	{
		ckName=document.location.pathname;

		while (ckName.indexOf("\/") != -1)
			ckName=ckName.replace("\/", "#");

		ckName=ckName.substr(ckName.lastIndexOf("#")+1);
		if (ckName.lastIndexOf(".")!=-1)
			ckName=ckName.substring(0, ckName.lastIndexOf("."));
		ckName = ckName + ckPlusName;

		document.cookie = ckName + "=" + dragT + ";";
	}
}

function restEstado()
{
	var ckName;
	var ckValue;

	if (navigator.cookieEnabled)
	{
		ckName=document.location.pathname;

		while (ckName.indexOf("\/") != -1)
			ckName=ckName.replace("\/", "#");

		ckName=ckName.substr(ckName.lastIndexOf("#")+1);
		if (ckName.lastIndexOf(".")!=-1)
			ckName=ckName.substring(0, ckName.lastIndexOf("."));
		ckName = ckName + ckPlusName;

		ckValue = document.cookie;
		if (ckValue.indexOf(ckName) != -1)
		{
			ckValue = ckValue.substr(ckValue.indexOf(ckName));
			dragT = ckValue.substr(ckValue.indexOf("=")+1);

			if (dragT.indexOf(";") != -1)
				dragT=dragT.substring(0, dragT.indexOf(";"));

		        contentT = ((dragT - rulerT)*(1/scrollLength));
		        contentT = eval('-' + contentT);

			moveTo();
		}
	}
}


// Preload
function eventLoader(){

	if(ie4){
		// Up-arrow X and Y variables
		upL = document.all.up.style.pixelLeft;
		upT = document.all.up.style.pixelTop;
		// Down-arrow X and Y variables
		downL = document.all.down.style.pixelLeft;
		downT = document.all.down.style.pixelTop;
		// Scrollbar X and Y variables
		dragL = document.all.drag.style.pixelLeft;
		dragT = document.all.drag.style.pixelTop;
		// Ruler Y variable
		rulerT = document.all.ruler.style.pixelTop;
		// Height of content layer and clip layer
		contentH = parseInt(document.all.content.scrollHeight);
		contentClipH = parseInt(document.all.contentClip.style.height);
	}
	else if(nn4){
		// Up-arrow X and Y variables
		upL = document.up.left;
		upT = document.up.top;
		// Down-arrow X and Y variables
		downL = document.down.left;
		downT = document.down.top;
		// Scrollbar X and Y variables
		dragL = document.drag.left;
		dragT = document.drag.top;
		// Ruler Y variable
		rulerT = document.ruler.top;
		// Height of content layer and clip layer
		contentH = document.contentClip.document.content.clip.bottom;
		contentClipH = document.contentClip.clip.bottom;
	}
	else if(dom){
		// Up-arrow X and Y variables
		upL = parseInt(document.getElementById("up").style.left);
		upT = parseInt(document.getElementById("up").style.top);
		// Down-arrow X and Y variables
		downL = parseInt(document.getElementById("down").style.left);
		downT = parseInt(document.getElementById("down").style.top);
		// Scrollbar X and Y variables
		dragL = parseInt(document.getElementById("drag").style.left);
		dragT = parseInt(document.getElementById("drag").style.top);
		// Ruler Y variable
		rulerT = parseInt(document.getElementById("ruler").style.top);
		// Height of content layer and clip layer
		contentH = parseInt(document.getElementById("content").offsetHeight);
		contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
		document.getElementById("content").style.top = 0 + "px";

	}
	// Number of pixels scrollbar should move
	scrollLength = ((scrollH-dragH)/(contentH-contentClipH));
	// Initializes event capturing
	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
	restEstado();
}


function reDim (t,l)
{
	if(ie4){
		// Up-arrow X and Y variables
		document.all.up.style.pixelLeft = l + listaW - upW;
		document.all.up.style.pixelTop = t;
		// Down-arrow X and Y variables
		document.all.down.style.pixelLeft = l + listaW - downW;
		document.all.down.style.pixelTop = t + listaH - downH;
		// Scrollbar X and Y variables
		document.all.drag.style.pixelLeft = l + listaW - dragW;
		document.all.drag.style.pixelTop = t + upH;
		// Ruler Y variable
		document.all.ruler.style.pixelTop = t + upH;
		document.all.ruler.style.pixelLeft = l + listaW - dragW;
		document.all.ruler.style.height = listaH - upH - downH;
		document.all.fondo.style.pixelTop = document.all.ruler.style.pixelTop;
		document.all.fondo.style.pixelLeft  = document.all.ruler.style.pixelLeft;
		document.all.fondo.style.height = listaH - upH - downH;
		//document.all.imgFondo.style.pixelTop = document.all.ruler.style.pixelTop;
		//document.all.imgFondo.style.pixelLeft  = document.all.ruler.style.pixelLeft;
		//document.all.imgFondo.style.height = listaH - upH - downH;
		// Height of content layer and clip layer
		document.all.contentClip.style.pixelLeft = l;
		document.all.contentClip.style.pixelTop = t;
		document.all.contentClip.style.height = listaH;
		document.all.contentClip.style.width = listaW - upW;

		document.all.contentClip.style.clip = 'rect(0 ' +  document.all.contentClip.style.width + ' ' + listaH  + ' 0)';

		//document.all.borrar1.pixelLeft = l - 5;
		//document.all.borrar1.pixelTop = t - 5;
		document.all.borde.style.pixelLeft = l - 3;
		document.all.borde.style.pixelTop = t - 3;
		document.all.borde.style.height = listaH + 6;
		document.all.borde.style.width = listaW + 6;
		document.all.borrar.style.pixelLeft = l - 15;
		document.all.borrar.style.pixelTop = t - 30;
		document.all.borrar.style.height = listaH + 60;
		document.all.borrar.style.width = listaW + 60;

	}
	else if(dom){
		// Up-arrow X and Y variables
		document.getElementById("up").style.left = l + listaW - upW;
		document.getElementById("up").style.top = t;
		// Down-arrow X and Y variables
		document.getElementById("down").style.left = l + listaW - downW;
		document.getElementById("down").style.top = t + listaH - downH;
		// Scrollbar X and Y variables
		document.getElementById("drag").style.left = l + listaW - dragW;
		document.getElementById("drag").style.top = t + upH;
		// Ruler Y variable
		document.getElementById("ruler").style.top = t + upH;
		document.getElementById("ruler").style.left = l + listaW - dragW;
		document.getElementById("ruler").style.height = listaH - upH - downH;
		document.getElementById("fondo").style.left =document.getElementById("ruler").style.left;
		document.getElementById("fondo").style.top = document.getElementById("ruler").style.top;
		document.getElementById("fondo").style.height = listaH - upH - downH;
		//document.getElementById("imgFondo").style.left =document.getElementById("ruler").style.left;
		//document.getElementById("imgFondo").style.top = document.getElementById("ruler").style.top;
		//document.getElementById("imgFondo").style.height = listaH - upH - downH;

		// Height of content layer and clip layer
		document.getElementById("contentClip").style.left =l;
		document.getElementById("contentClip").style.top = t;
		document.getElementById("contentClip").style.height = listaH;
		document.getElementById("contentClip").style.width = listaW - upW;
		document.getElementById("contentClip").style.clip= 'rect(0 ' +  document.getElementById("contentClip").style.width + ' ' + listaH  + ' 0)';
		//document.getElementById("borrar1").style.left =l - 5;
		//document.getElementById("borrar1").style.top = t - 5;
		document.getElementById("borde").style.left =l -3;
		document.getElementById("borde").style.top = t -3;
		document.getElementById("borde").style.height = listaH + 6;
		document.getElementById("borde").style.width = listaW + 6;
		document.getElementById("borrar").style.left = l - 15;
		document.getElementById("borrar").style.top = t - 30;
		document.getElementById("borrar").style.height = listaH + 60;
		document.getElementById("borrar").style.width = listaW + 60;
	}
	else if(nn4){
                alert("nn4");
		// Up-arrow X and Y variables
		document.up.left = l + listaW - upW;
		document.up.top = t;
		// Down-arrow X and Y variables
		document.down.left = l + listaW - downW;
		document.down.top = t + listaH - downH;
		// Scrollbar X and Y variables
		document.drag.left = l + listaW - dragW;
		document.drag.top = t + upH;
		// Ruler Y variable
		document.ruler.left =  l + listaW - dragW;
		document.ruler.top = t + upH;
		document.fondo.style.top = document.ruler.style.top;
		document.fondo.style.left  = document.ruler.style.left - 4;
		// Height of content layer and clip layer
		document.contentClip.left = l;
		document.contentClip.top = t;

		document.borrar.left = l - 5;
		document.borrar.top = t - 5;

		document.borde.left = l - 5;
		document.borde.top = t - 5;
	}
}

function showList ()
{
	if(ie4){
		// Up-arrow
		document.all.up.style.visibility = 'visible';
		// Down-arrow
		document.all.down.style.visibility = 'visible';
		// Scrollbar
		document.all.drag.style.visibility = 'visible';
		// Ruler
		document.all.ruler.style.visibility = 'visible';
		document.all.fondo.style.visibility = 'visible';
		// Height of content layer and clip layer
		document.all.contentClip.style.visibility = 'visible';

		document.all.borrar.style.visibility = 'visible';

		document.all["borde"].style.visibility = 'visible';
	}
	else if(dom){
		// Up-arrow
		document.getElementById("up").style.visibility = 'visible';
		// Down-arrow
		document.getElementById("down").style.visibility = 'visible';
		// Scrollbar
		document.getElementById("drag").style.visibility = 'visible';
		// Ruler Y variable
		document.getElementById("ruler").style.visibility = 'visible';
		document.getElementById("fondo").style.visibility ='visible';

		// Height of content layer and clip layer
		document.getElementById("contentClip").style.visibility ='visible';
		document.getElementById("borrar").style.visibility ='visible';
		document.getElementById("borde").style.visibility ='visible';
	}
}
function hideList ()
{
	if(ie4){
		// Up-arrow
		document.all.up.style.visibility = 'hidden';
		// Down-arrow
		document.all.down.style.visibility = 'hidden';
		// Scrollbar
		document.all.drag.style.visibility = 'hidden';
		// Ruler
		document.all.ruler.style.visibility = 'hidden';
		document.all.fondo.style.visibility = 'hidden';
		// Height of content layer and clip layer
		document.all.contentClip.style.visibility = 'hidden';

		document.all.borrar.style.visibility = 'hidden';

		document.all['borde'].style.visibility = 'hidden';
	}
	else if(dom){
		// Up-arrow
		document.getElementById("up").style.visibility = 'hidden';
		// Down-arrow
		document.getElementById("down").style.visibility = 'hidden';
		// Scrollbar
		document.getElementById("drag").style.visibility = 'hidden';
		// Ruler Y variable
		document.getElementById("ruler").style.visibility = 'hidden';
		document.getElementById("fondo").style.visibility ='hidden';

		document.getElementById("contentClip").style.visibility ='hidden';
		document.getElementById("borrar").style.visibility ='hidden';
		document.getElementById("borde").style.visibility ='hidden';
	}
}

function setAncho (a)
{
	listaW = a;
}
function setAlto (a)
{
	listaH = a;
	scrollH = listaH - 30;
}

