onload=eventLoader

var upH = 9; // Height of up-arrow
var upW = 5; // Width of up-arrow
var downH = 9; // Height of down-arrow
var downW = 5; // Width of down-arrow
var dragH = 26; // Height of scrollbar
var dragW = 17; // Width of scrollbar
var scrollH = 230; // Height of scrollbar
var speed = 4; // Scroll speed
var tocando = 0;
var tocando1 = 0;
// 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; // Down-arrow Y
var dragL; // Scrollbar X
var dragT; // Scrollbar Y
var rulerL; // Ruler X
var rulerT; // Ruler Y
var contentT; // 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 content;

// 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);
	startY1 = (mouseY - dragT1);
	// If click on up-arrow
	if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
		clickUp = true;
		return scrollUp();
	}	
	// Else if click on down-arrow
	else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
		clickDown = true;
		return scrollDown();
	}
	// Else if click on scrollbar
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
		clickDrag = true;
		return false;
	}
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && 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();
		}
	}
//para el scroll 2
	// If click on up-arrow
	else if(mouseX >= upL1 && (mouseX <= (upL1 + upW1)) && mouseY >= upT1 && (mouseY <= (upT1 + upH1))){
		clickUp1 = true;
		return scrollUp1();
	}	
	// Else if click on down-arrow
	else if(mouseX >= downL1 && (mouseX <= (downL1 + downW1)) && mouseY >= downT1 && (mouseY <= (downT1 + downH1))){
		clickDown1 = true;
		return scrollDown1();
	}
	// Else if click on scrollbar
	else if(mouseX >= dragL1 && (mouseX <= (dragL1 + dragW1)) && mouseY >= dragT1 && (mouseY <= (dragT1 + dragH1))){
		clickDrag1 = true;
		return false;
	}
	else if(mouseX >= dragL1 && (mouseX <= (dragL1 + dragW1)) && mouseY >= rulerT1 && (mouseY <= (rulerT1 + scrollH1))){
		// If click above drag
		if(mouseY < dragT1){
			clickAbove1 = true;
			clickUp1 = true;
			return scrollUp1();
		}
		// Else click below drag
		else{
			clickBelow1 = true;
			clickDown1 = true;
			return scrollDown1();
		}
	}
	// If no scrolling is to take place
	else{
		return true;
	}
}

// Drag function
function move(e){
//Parte del div que se mueve
	if (window.ss) {
            var x=(nav)?  e.pageX  :  event.x+document.body.scrollLeft; 
            var y=(nav)?  e.pageY  :  event.y+document.body.scrollTop; 
            skn.left=x+Xoffset; 
            skn.top=y+Yoffset; 
	}
//FINParte del div que se mueve
	getContent();
	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=contentT*(-1);
		moveTo();
		
		// So ie-pc doesn't select gifs
		if(ie4)
			return false;
	}
	else {
		if(clickDrag1 && contentH1 > contentClipH1){
			getMouse(e);
			dragT1 = (mouseY - startY1);
		
			if(dragT1 < (rulerT1))
				dragT1 = rulerT1;		
			if(dragT1 > (rulerT1 + scrollH1 - dragH1))
				dragT1 = (rulerT1 + scrollH1 - dragH1);
		
			contentT1 = ((dragT1- rulerT1)*(1/scrollLength1));
			contentT1=contentT1*(-1);	
	
			moveTo1();
		
			// So ie-pc doesn't select gifs
			if(ie4)
				return false;
		}
	}
}

function up(){
	clearTimeout(timer);
	clearTimeout(timer1);
	// Resetting variables
	if (tocando!=1){
	clickUp = false;
	clickDown = false;
	}
	clickDrag = false;
	clickAbove = false;
	clickBelow = false;
	
	if (tocando1!=1){
	clickUp1 = false;
	clickDown1 = false;
	}
	clickDrag1 = false;
	clickAbove1 = false;
	clickBelow1 = 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(){
	getContent();
	getT();
	
	if(clickBelow){
		if(dragT >= (mouseY-(dragH/2)))
			return up();
	}
	if(clickDown){
		if(contentT > -(contentH - contentClipH)){			
			dragT = dragT + (speed*scrollLength);
			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 getContent(){
	contentH = alturaTotal;
	if (ie4){
		// Height of content layer and clip layer
		contentClipH = parseInt(document.all.contentClip.style.height);
	}
	else if (nn4){
		// Height of content layer and clip layer
		contentClipH = document.contentClip.clip.bottom;	
	}
	else if(dom){
		contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
	}
	// Number of pixels scrollbar should move
	scrollLength = ((scrollH-dragH)/(contentH-contentClipH));

}

// 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;

	}
	else if(nn4){
		document.up.top=document.up.top-8;
		document.up1.top=document.up1.top-8;
		document.down.top=document.down.top-8;
		document.down1.top=document.down1.top-8;
		document.drag.top=document.drag.top-8;
		document.drag1.top=document.drag1.top-8;
		document.ruler.top=document.ruler.top-8;
		document.ruler1.top=document.ruler1.top-8;
		document.fondo.top=document.fondo.top-8;
		document.fondo1.top=document.fondo1.top-8;

		// 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;
		

	}
	else if(dom){
		document.getElementById("up").style.top=(parseInt(document.getElementById("up").style.top)-18)+ "px";
		document.getElementById("up1").style.top=(parseInt(document.getElementById("up1").style.top)-18)+ "px";
		document.getElementById("down").style.top=(parseInt(document.getElementById("down").style.top)-18)+ "px";
		document.getElementById("down1").style.top=(parseInt(document.getElementById("down1").style.top)-18)+ "px";
		document.getElementById("drag").style.top=(parseInt(document.getElementById("drag").style.top)-18)+ "px";
		document.getElementById("drag1").style.top=(parseInt(document.getElementById("drag1").style.top)-18)+ "px";
		document.getElementById("ruler").style.top=(parseInt(document.getElementById("ruler").style.top)-18)+ "px";
		document.getElementById("ruler1").style.top=(parseInt(document.getElementById("ruler1").style.top)-18)+ "px";
		document.getElementById("fondo").style.top=(parseInt(document.getElementById("fondo1").style.top)-18)+ "px";
		document.getElementById("fondo1").style.top=(parseInt(document.getElementById("fondo").style.top)-18)+ "px";
		
                document.getElementById("up").style.left=(parseInt(document.getElementById("up").style.left)-2)+ "px";
                document.getElementById("down").style.left=(parseInt(document.getElementById("down").style.left)-2)+ "px";
                document.getElementById("drag").style.left=(parseInt(document.getElementById("drag").style.left)-2)+ "px";
                document.getElementById("ruler").style.left=(parseInt(document.getElementById("ruler").style.left)-2)+ "px";
                document.getElementById("fondo").style.left=(parseInt(document.getElementById("fondo").style.left)-2)+ "px";
                document.getElementById("up1").style.left=(parseInt(document.getElementById("up1").style.left)-2)+ "px";
                document.getElementById("down1").style.left=(parseInt(document.getElementById("down1").style.left)-2)+ "px";
                document.getElementById("drag1").style.left=(parseInt(document.getElementById("drag1").style.left)-2)+ "px";
                document.getElementById("ruler1").style.left=(parseInt(document.getElementById("ruler1").style.left)-2)+ "px";
                document.getElementById("fondo1").style.left=(parseInt(document.getElementById("fondo1").style.left)-2)+ "px";

                document.getElementById("contentClip").style.top=(parseInt(document.getElementById("contentClip").style.top)-13)+ "px";

		// 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);
		document.getElementById("content").style.top = 0 + "px";
	}
	//getContent();

	// Initializes event capturing
	eventLoader1();
	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
	
	ensenarObjetos();
	getContent();
	movera();
	esconder1();
	esconder2();
}

function showObject(obj) {
	if (ns4) obj.visibility = "show"
	else if (ie4 || ns6) {obj.visibility = "visible";}
}

function layerWrite(id,nestref,text) {
	if (ie4) {
		document.all[id].innerHTML = text;

	}
	else if (nn4) {		
		var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document;
		lyr.open();
		lyr.write(text);
		lyr.close();}
	else if (dom) {document.getElementById(id).innerHTML = text;}
}

function encontrarObjeto(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=encontrarObjeto(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); 
  return x;
}

function ensenarObjetos(){
text="";
fecha="";
for (i=0;i<datos.array.length;i++){
  text=text+"<div id=\"not"+i+"\" style=\"position:absolute; width:220px; z-index:0; visibility: inherit\">\n";
  text=text+"<table border='0' width=220>\n";
  if (fecha!=datos.array[i].fecha){
     fecha=datos.array[i].fecha;
     text=text+"<tr><td align=right><p><font face='Verdana, Arial, Helvetica, sans-serif' size='1' color='#083863'><u><b>"+datos.array[i].fecha+"</b></u></font></p></td></tr>\n";
  }
  text=text+"<tr><td height='25' align=right><p><a href='javascript:void(null)' onClick='ensenarInformacion("+datos.array[i].name+");'><font face='Verdana, Arial, Helvetica, sans-serif' size='1' color='#000000'><b>"+datos.array[i].titular+"</b></font></a></p></td></tr></table>\n";
  text=text+"</div>\n";
}
layerWrite("content","contentClip",text);
text="";
alturas();
}

function movera(){
var altura=0;
if (nn4){alturaClip=document.contentClip.clip.bottom;}
else if (ie4){alturaClip=parseInt(document.all.contentClip.style.height);}
else if (dom){alturaClip=parseInt(document.getElementById("contentClip").offsetHeight);}

if (noticia!=""){
  	i=0;
	while ((datos.array[i].name!=noticia)&&(i<datos.array.length)){
	  altura += array[i];
	  i++;
	}
	if (alturaTotal>alturaClip){
	  contentT=-altura;
	  if (-contentT>alturaTotal-alturaClip){
		contentT = -(alturaTotal-alturaClip);
	  }
	}
	else contentT=0;
	tantoporciento=(-1*parseInt(contentT)*100)/(contentH-contentClipH);
	dragT=152+((tantoporciento*(scrollH-dragH))/100)
	moveTo();
	ensenarInformacion(eval(datos.array[i].name));
}
}

function alturas(){
array=new Array();
alturaTotal=0;
for (i=0;i<datos.array.length;i++){
	eval("objetos1=encontrarObjeto('not"+i+"')");
	if (i==0){
		if (nn4) {array[i]=objetos1.clip.bottom;objetos1.top=0;}
		else if (ie4) {array[i]=parseInt(objetos1.scrollHeight);objetos1.style.pixelTop=0;}
		else if (dom) {array[i]=parseInt(objetos1.offsetHeight);objetos1.style.top=0;}
	}
	else {
		eval("objetos2=encontrarObjeto('not"+(i-1)+"')");
		if (nn4) {array[i]=objetos1.clip.bottom;
                          objetos1.top=parseInt(array[i-1])+parseInt(objetos2.top);}
		else if (ie4) {array[i]=parseInt(objetos1.scrollHeight);
			       objetos1.style.pixelTop=parseInt(array[i-1])+parseInt(objetos2.style.pixelTop);}
		else if (dom) {array[i]=parseInt(objetos1.offsetHeight);
			       objetos1.style.top=parseInt(array[i-1])+parseInt(objetos2.style.top);}
	}
	alturaTotal += array[i];
}
if (nn4) encontrarObjeto('content').clip.height=alturaTotal;
}

function ensenarInformacion(objeto){
	if(ie4){
		document.all.drag1.style.pixelTop=152;		
		document.all.ruler1.style.pixelTop=152;
		document.all.content1.style.pixelTop=0;
		dragT1=152;
		rulerT1=152;
	}
	else if(nn4){
		document.drag1.top=146;		
		document.ruler1.top=146;
		document.contentClip.document.content1.top=0;
		dragT1=146;
		rulerT1=146;
	}
	else if(dom){
		document.getElementById("drag1").style.top=136;
		document.getElementById("ruler1").style.top=136;
		document.getElementById("content1").style.top = 0 + "px";
		dragT1=136;
		rulerT1=136;
	}
texto="";
texto=texto+"<table border='0' width=230>\n";
texto=texto+"<tr><td align=right><p><font face='Verdana, Arial, Helvetica, sans-serif' size='1' color='#000000'><b>"+objeto.fecha+"</b></font></p></td></tr>\n";
texto=texto+"<tr><td align=right><p><font face='Verdana, Arial, Helvetica, sans-serif' size='1' color='#000000'><u><b>"+objeto.titular+"</u></b></font></p></td></tr>\n";
texto=texto+"<tr><td align=right><p><font face='Verdana, Arial, Helvetica, sans-serif' size='1' color='#000000'><b>"+objeto.cuerpo+"</b></font></p></td></tr>\n";
texto=texto+"</table>\n";
layerWrite("content1","contentClip",texto);
esconder2();
}

function esconder1() {
	if (nn4) {
		var prof = alturaTotal;
		var profCont = alturaClip;
		if (profCont >= prof){ 
			document.up.visibility="hide";
			document.down.visibility="hide";
			document.drag.visibility="hide";
			document.ruler.visibility="hide";
			document.fondo.visibility="hide";
			}
		else   { 
			document.up.visibility="show";
			document.down.visibility="show";
			document.drag.visibility="show";
			document.ruler.visibility="show";
			document.fondo.visibility="show"; 
			}
	}
	else if (ie4) {
		var prof = alturaTotal;
		var profCont = alturaClip;
		if (profCont >= prof){
			document.all.up.style.visibility="hidden";
			document.all.down.style.visibility="hidden";
			document.all.drag.style.visibility="hidden";
			document.all.ruler.style.visibility="hidden";
			document.all.fondo.style.visibility="hidden";}
		else 	{
			document.all.up.style.visibility="visible";
			document.all.down.style.visibility="visible";
			document.all.drag.style.visibility="visible";
			document.all.ruler.style.visibility="visible";
			document.all.fondo.style.visibility="visible";
			}
	}
        else if (dom) {
                var prof = alturaTotal;
                var profCont = alturaClip;
                if (profCont >= prof){
                        document.getElementById("up").style.visibility="hidden";
                        document.getElementById("down").style.visibility="hidden";
                        document.getElementById("drag").style.visibility="hidden";
                        document.getElementById("ruler").style.visibility="hidden";
                        document.getElementById("fondo").style.visibility="hidden";
                        }
                else    {
                        document.getElementById("up").style.visibility="visible";
                        document.getElementById("down").style.visibility="visible";
                        document.getElementById("drag").style.visibility="visible";
                        document.getElementById("ruler").style.visibility="visible";
                        document.getElementById("fondo").style.visibility="visible"; 
                        }
        }
}

function esconder2() {
	if (nn4) {
		var prof = document.contentClip.document.content1.clip.bottom;
		var profCont = alturaClip;
		if (profCont >= prof){			
			document.up1.visibility="hide";
			document.down1.visibility="hide";
			document.drag1.visibility="hide";
			document.ruler1.visibility="hide";
			document.fondo1.visibility="hide";
			}
		else   { 
			document.up1.visibility="show";
			document.down1.visibility="show";
			document.drag1.visibility="show";
			document.ruler1.visibility="show";
			document.fondo1.visibility="show";
			}
	}
	else if (ie4) {		
		var prof = parseInt(document.all.content1.scrollHeight);
		var profCont = alturaClip;
		if (profCont >= prof){			
			document.all.up1.style.visibility="hidden";
			document.all.down1.style.visibility="hidden";
			document.all.drag1.style.visibility="hidden";
			document.all.ruler1.style.visibility="hidden";
			document.all.fondo1.style.visibility="hidden";}
		else 	{			
			document.all.up1.style.visibility="visible";
			document.all.down1.style.visibility="visible";
			document.all.drag1.style.visibility="visible";
			document.all.ruler1.style.visibility="visible";
			document.all.fondo1.style.visibility="visible";
			}
	}
        if (dom) {
                var prof = parseInt(document.getElementById("content1").offsetHeight);
                var profCont = alturaClip;
                if (profCont >= prof){
                        document.getElementById("up1").style.visibility="hidden";
                        document.getElementById("down1").style.visibility="hidden";
                        document.getElementById("drag1").style.visibility="hidden";
                        document.getElementById("ruler1").style.visibility="hidden";
                        document.getElementById("fondo1").style.visibility="hidden";
                        }
                else    {
                        document.getElementById("up1").style.visibility="visible";
                        document.getElementById("down1").style.visibility="visible";
                        document.getElementById("drag1").style.visibility="visible";
                        document.getElementById("ruler1").style.visibility="visible";
                        document.getElementById("fondo1").style.visibility="visible"; 
                        }
        }
}
