//<!--
document.write("<style type=\"text\/css\">div#content div#textContainerHolder{height:261px; overflow:hidden;}");
document.write("div#content div#navigation{display:block;}<\/style>");

moving = false;
scrollElement = "textContainer";
travelDistance = 8;

function initScroller(){

	baseHeight = getOff("scrollBase")[1]; // scrollipalkkialueen korkeus
	contentHeight = getOff(scrollElement)[1]+20; // Scrollattavan alueen korkeus
	contentAreaHeight = getOff("textContainerHolder")[1]; // Näkyvän alueen korkeus
		
	if(contentHeight < contentAreaHeight){
		document.getElementById("scrollBase").style.display = "none";
	}else{
		document.getElementById("scrollBase").style.display = "block";
		document.getElementById("middlePalkki").style.height = contentAreaHeight/contentHeight * contentAreaHeight + "px";
	}
	
	barHeight = getOff("scrollBar")[1]; // koko scrollipalkin korkeus
	
	moveMin = 0;
	moveMax = baseHeight - barHeight;
	
	ratio = moveMax / (contentHeight - contentAreaHeight);
	
	var scrollInterval = setInterval(scrollText,100);
}
	
function getOff(elementId)
{
	x = document.getElementById(elementId).offsetWidth;
	y = document.getElementById(elementId).offsetHeight;
	return [x,y];
}

/*SCROLL*/
function hookEvent(element, eventName, callback)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.addEventListener)
  {
    if(eventName == 'mousewheel')
      element.addEventListener('DOMMouseScroll', callback, false);  
      element.addEventListener(eventName, callback, false);
  }
  else if(element.attachEvent){
    element.attachEvent("on" + eventName, callback);
  }
}

function cancelEvent(e)
{
  e = e ? e : window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}

function printInfo(e)
{
  tx = document.getElementById(scrollElement); // teksti
  txB = document.getElementById("scrollBar"); // scrollBar
  txTop = tx.style.marginTop.substring(0,tx.style.marginTop.length-2)-0;
  txVisible = contentAreaHeight-0;
  txH = contentHeight-0;
  e = e ? e : window.event;
  var raw = e.detail ? e.detail : e.wheelDelta;
  var normal = e.detail ? e.detail * -1 : e.wheelDelta / 40;
  normal = (normal * 2)-0;
  
  if((txTop*(-1) < (txH-txVisible) && normal < 0) || (txTop < 0 && normal > 0)){
	tx.style.marginTop = txTop + normal + "px";
	
	txTop = tx.style.marginTop.substring(0,tx.style.marginTop.length-2)-0;
	txB.style.top = txTop*-ratio + "px";
  }
  
  if(txTop > 0 && normal > 0){
    tx.style.marginTop = 0 + "px";
	
	txTop = tx.style.marginTop.substring(0,tx.style.marginTop.length-2)-0;
	txB.style.top = 0 + "px";
  }
  
  if(txTop*(-1) > (txH-txVisible) && normal < 0){
	tx.style.marginTop = (txH-txVisible)*(-1) + "px";
	txTop = tx.style.marginTop.substring(0,tx.style.marginTop.length-2)-0;
	txB.style.top = baseHeight - barHeight  + "px";
  }
	txBTop = txB.style.top;
//  document.getElementById('scrollContent').innerHTML = "<br/>Raw Value: " + raw + "<br/>Normalized Value: " + normal + "<br/>marginTop: "+ txTop + "<br />barTop: " + txBTop + "<br />Vheight: " + txVisible + "<br />Theight: " + txH + "<br />Ratio: " + ratio + " ??" + (txH-txVisible);
  cancelEvent(e);
}

scrollDir = 0;

/*napit*/
function startScrollUp(e){
	scrollDir = 1;
	cancelEvent(e);
}
function startScrollDown(e){
	scrollDir = -1;
	cancelEvent(e);
}

function scrollText(){
	if(contentHeight != getOff(scrollElement)[1]+20){
		initScroller();
	}
	tx = document.getElementById(scrollElement);
	txB = document.getElementById("scrollBar");
  	txTop = tx.style.marginTop.substring(0,tx.style.marginTop.length-2)-0;
  	txVisible = contentAreaHeight-0;
	txH = contentHeight-0;
	scrollAmount = (scrollDir*travelDistance)-0;
	if((txTop*(-1) < (txH-txVisible) && scrollDir == -1) || ((txTop*-1) > 0) && scrollDir == 1){
		tx.style.marginTop = txTop+scrollAmount + "px";
		/*TARKISTETAAN ETTEI PALKKI MENE YLI ÄYRÄIDEN*/	
		if(txTop < ((txH-txVisible)*-1) && scrollDir == -1){
			tx.style.marginTop = ((txH-txVisible)-0)*(-1) + "px";
		}
		
		txTop = tx.style.marginTop.substring(0,tx.style.marginTop.length-2)-0;

		if(txTop > 0 && scrollDir == 1){
			tx.style.marginTop = "0px";
		}

		if(txTop*(-1) > (txH-txVisible) && scrollDir == -1){
			tx.style.marginTop = (txH-txVisible)*(-1)+"px";
		}
		txB.style.top = tx.style.marginTop.substring(0,tx.style.marginTop.length-2)*-ratio + "px";
	}
	
//	document.getElementById('scrollContent').innerHTML = "<br/>marginTop = "+ txTop + "<br />Vheight" + txVisible  + "<br />Theight" + txH;
	
	
	if((txTop*-1) <= 5){
		document.getElementById("moveUp").style.visibility = "hidden";
	}else{
		document.getElementById("moveUp").style.visibility = "visible";
	}
	if(txTop*(-1) > (txH-txVisible-5)){
		document.getElementById("moveDown").style.visibility = "hidden";
	}else{
		document.getElementById("moveDown").style.visibility = "visible";
	}
	
}

var mayDrag = false;

function dragBar(e){
	mayDrag = true;
	originalY = e.pageY;
	originalBarY = document.getElementById("scrollBar").style.top.substring(0,document.getElementById("scrollBar").style.top.length-2);
	originalY = originalY - originalBarY;
	cancelEvent(e);
}



function moveText(e){
	if(mayDrag){
		moveValue = e.pageY-originalY;
		if(moveValue > moveMin && moveValue < moveMax){
//			document.getElementById('scrollContent').innerHTML = moveValue
			document.getElementById('scrollBar').style.top = moveValue + "px";
			tx.style.marginTop = -moveValue/ratio + "px";
		}else if(moveValue <= moveMin){
//			document.getElementById('scrollContent').innerHTML = moveValue
			document.getElementById('scrollBar').style.top = moveMin+"px";
			tx.style.marginTop = -moveMin/ratio + "px";
		}else if(moveValue >= moveMax){
//			document.getElementById('scrollContent').innerHTML = moveValue
			document.getElementById('scrollBar').style.top = moveMax+"px";
			tx.style.marginTop = -moveMax/ratio + "px";
		}
	}
	cancelEvent(e);
}



function stopScroll(e){
	mayDrag = false;
	scrollDir = 0;
	cancelEvent(e);
}

function initScroll(){
	originalY = 310; 	
}

	// -->
