iens6=document.all||document.getElementById
ns4=document.layers

var upH   = 14;    // HEIGHT of up-arrow
var upW   = 15;    // Width of up-arrow

var downH = 14;    // HEIGHT of down-arrow
var downW = 15;    // Width of down-arrow

var dragH = 15;    // HEIGHT of scrollbar
var dragW = 20;    // Width of scrollbar

var scrollH = 82;  // HEIGHT of scrollbar
var scrollL = scrollH - dragH;

var conteinerH = 100; 
var topDrag    = 15;
var posLeft = 300;

//velocidade
var speed=5

var moveupvar;
var movedownvar;

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 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 objUp;
var objDown;
var objDrag;
var objRuler;
var objAux;

function getLayer(id){
	
	  if (document.getElementById){
		    return document.getElementById(id);
	  }
	  else if (document.all){
		    return document.all[id];
	  }
	  else if (document.layers){
		    return document.layers[id];
	  }
	  
	  return null;
	  
}

function atualizarValores(){

	if (iens6){	
		if(typeof(objUp.style.offsetLeft) == 'number'){
			upL = objUp.style.offsetLeft;
			upT = objUp.style.offsetTop;
	
			downL = objDown.style.offsetLeft;
			downT = objDown.style.offsetTop;
	
			dragL = objDrag.style.offsetLeft;
			dragT = objDrag.style.offsetTop;
	
			rulerT = objRuler.style.offsetTop; 
		}
		else if(typeof(objUp.offsetLeft) == 'number'){
			upL = objUp.offsetLeft;
			upT = objUp.offsetTop;

			downL = objDown.offsetLeft;
			downT = objDown.offsetTop;
	
			dragL = objDrag.offsetLeft;
			dragT = objDrag.offsetTop;
	
			rulerT = objRuler.offsetTop; 
		}
		scrollLength = ((scrollH-dragH)/(contentH-conteinerH));
	}
	return;
}


function moveElement(id, x, y){

	  var elm = null; 
      elm = getLayer(id);

     if (elm.style){
	    if (typeof(elm.style.left) == 'number'){
			if(y > -1){
				y += topDrag;
    		    elm.style.top  = y;
			}
			if(x > 0) elm.style.left  = x;
	    }
    	else{
			if(y > -1){
				y += topDrag;
    		    elm.style.top  = y + 'px';
			}
			if(x > 0) elm.style.left  = x + 'px';
	    }
	  }
	  else if (typeof(elm.left) == 'number'){
	  		if(y > -1){
				y += topDrag;
		        elm.top  = y;
			}
			if(x > 0) elm.left  = x;
	  }
	  
	  return;
	  
}


function movedown(){
	
	var mover = false;

	if (iens6 && (parseInt(crossobj.style.top) > (contentheight*(-1)+conteinerH)) ){
		crossobj.style.top=parseInt(crossobj.style.top)-speed+"px";
		mover= true;
	}
	else if (ns4 && (crossobj.top>(contentheight*(-1)+conteinerH)) ){
		crossobj.top-=speed;
		mover= true;
	}
	
	if(mover){
		var dy;
		var lyct = null;
		if(iens6){
			if( parseInt(crossobj.style.top) < (contentheight*(-1)+conteinerH) )
				crossobj.style.top=contentheight*(-1)+conteinerH+"px";
			var cttop = parseInt(crossobj.style.top);
		}
		else if (ns4){
			if( crossobj.top > (contentheight*(-1)+conteinerH) )
				crossobj.top=(contentheight*(-1)+conteinerH);
			var cttop = parseInt(crossobj.top);
		}
		
		dy = parseInt((-cttop*scrollL)/(contentheight-conteinerH));
		
		moveElement("drag", 0, dy);
		
		atualizarValores();
		movedownvar=setTimeout("movedown()",20)
	}
}

function moveup(){
	
	var mover= false;
	
	if (iens6&&parseInt(crossobj.style.top)<0){
		crossobj.style.top=parseInt(crossobj.style.top)+speed+"px";
		mover= true;
	}
	else if (ns4&&crossobj.top<0){
		crossobj.top+=speed;
		mover= true;
	}
	
	if(mover){
		var dy=0;
		var lyct = null;
		if(iens6)
			var cttop = parseInt(crossobj.style.top);
		else
			var cttop = parseInt(crossobj.top);
		dy = parseInt((-cttop*scrollL)/(contentheight-conteinerH));
		
		moveElement("drag", 0, dy);
		atualizarValores();
		movedownvar=setTimeout("moveup()",20)
	}
	else{
		moveElement("drag", 0, 0);
		if (iens6)
			crossobj.style.top="0px";
		else if (ns4)
			crossobj.top+=0;
	}
	
}

function getcontent_height(){

	objUp   = document.getElementById? document.getElementById("up") : document.all.up;
	objDown = document.getElementById? document.getElementById("down") : document.all.down;
	objDrag = document.getElementById? document.getElementById("drag") : document.all.drag;
	objRuler= document.getElementById? document.getElementById("ruler") : document.all.ruler;
	objAux  = document.getElementById? document.getElementById("aux") : document.all.aux;

	if (iens6){
		contentheight=crossobj.offsetHeight;
		atualizarValores();
		contentH = contentheight;
	}
	
	// posicionar o scroll
	posicaoScroll();
	
	return;
		
}

function posicaoScroll(){
	
	var x = posLeft;
	var ww=1200;
	
	// posicionar o scroll
	if(typeof(document.body.clientWidth) == 'number'){
		x = (document.body.clientWidth - 972)/2 + 343;
		ww = document.body.clientWidth;
	}
	else if(typeof(window.innerWidth) == 'number'){
		x = (window.innerWidth - 972)/2 + 343;
		ww = window.innerWidth;
	}
	else{
		x = posLeft;
	}
	
	if(x < posLeft){
		x = posLeft;
	}
	
	if(ww < 1000){
		x -= 0;
	}
	
//	moveElement("up", x, -2);
//	moveElement("down", x, -2);
//	moveElement("drag", x, -2);
		
	if(contentheight > (conteinerH+10)){
		idvisible("up", "visible");
		idvisible("down", "visible");
		idvisible("drag", "visible");
		idvisible("linhazinha", "visible");
		atualizarValores();
	}
	
	return;
	
}


function up(){

	clearTimeout(moveupvar);
	clearTimeout(movedownvar);
	
	clickUp    = false; 
	clickDown  = false; 
	clickDrag  = false; 
	clickAbove = false; 
	clickBelow = false; 
	
	return true;
	
}

function reset_var(){

	clickUp    = false; 
	clickDown  = false; 
	clickDrag  = false; 
	clickAbove = false; 
	clickBelow = false; 
	atualizarValores();
	
	return;
	
}

// Mousedown
function down(e){

	if((document.layers && e.which!=1) || (document.all && event.button!=1)) 
		return true; 

	atualizarValores();
	getMouse(e);
	startY = (mouseY - dragT);

	// If click on up-arrow
	if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
		clickUp = true;
		return moveup();
	}	
	// Else if click on down-arrow
	else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
		clickDown = true;
		return movedown();
	}
	// 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(mouseY < dragT){ // If click above drag
			clickAbove = true;
			clickUp = true;
			return moveup();
		}
		else{ // Else click below drag 
			clickBelow = true;
			clickDown = true;
			return movedown();
		}
	}
	else{
		return true;
	}
	
}

// Drag function
function move(e){

	if(clickDrag && contentheight > conteinerH){
		getMouse(e);
		dragT = (mouseY - startY);
			
		if(dragT < topDrag)
			dragT = topDrag;		
		if(dragT > (topDrag + scrollH - dragH))
			dragT = (topDrag + scrollH - dragH);
		
		contentT = ((dragT - topDrag)*(1/scrollLength));
		contentT = eval('-' + contentT);

		moveTo();

		if(document.all)
			return false;
	}
	
	return;
	
}

function moveTo(){

	if (iens6 && parseInt(crossobj.style.top)>=(contentheight*(-1)+conteinerH)){
		crossobj.style.top = contentT + "px";
		objDrag.style.top  = dragT + "px";
	}
	else if (ns4 && crossobj.top>=(contentheight*(-1)+conteinerH)){
		crossobj.top = contentT;
		objDrag.top  = dragT;
	}
	
	return;
	
}

function getMouse(e){
	
	if(document.layers || navigator.product == 'Gecko'){
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
	else if(iens6){
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}
}

function idvisible(id, status){

	if(document.layers){
		if(status == "visible") status = "show";
		document.layers[id].visibility = status;
	}
	else if(document.all){
		document.all[id].style.visibility = status;
	}
	else if(document.getElementById){
		document.getElementById(id).style.visibility = status;
	}
	
	return;
	
}

if(navigator.product == 'Gecko')
	window.captureEvents(Event.MOUSEUP|Event.MOUSEDOWN);

document.onmousedown = down;
document.onmousemove = move;
document.onmouseup   = up;	
window.onresize      = posicaoScroll;