// Copyright Abrahamsson Solutions
// Tillstånd givet till Mediaverket för fritt användande av denna scroller på sina egna och kunders webbplatser.
// Senast uppdaterad: 2002-11-05 // David

// Change as you see fit
// VVVVVVVVVVVVVVVVVVVVV
var scrFirstDelay=500;
var scrSecondDelay=50;
var scrUDmousedownspeed=25;
var scrUDmouseoverspeed=3;
var scrUDarrowspeed=25; // Up & Down on the keyboard...
var scrSlide_MinHeight=1;



// DONT change...
// VVVVVVVVVVVVVV
var scrUDspeed;

var scrSlide_Top,scrSlide_Height;
var scrContent_Top,scrContent_VisibleHeight,scrContent_CurrTop,scrContent_CurrBottom,scrContent_Height,scrContent_Width;
var scrBar_Top,scrBar_Height,scrBar_Left,scrBar_Width;

var oldMouseMove=-1;
var oldMouseUp=-1;
var bCaptured=false;

var bBarMouseDown=false;
var bSlideMouseDown=false;
var bUDMouseDown=false;
var nMouseX=0;
var nMouseY=0;
var timer=null;
var nScrDir=0;
var isNS6=(!document.all && document.getElementById)?true:false;
var isNS4=((navigator.appName=="Netscape") && !isNS6) ?true:false;
var isNSany=isNS6 || isNS4;


function scrSetup(height, barheight)
{

  if (isNS6)
  {
	scrContentCss=scrContent=document.getElementById("scrContent").style;
    scrBarCss=scrBar=document.getElementById("scrBar").style;
    scrSlideCss=scrSlide=document.getElementById("scrSlide").style;
    scrUpCss=scrUp=document.getElementById("scrUp").style;
    scrDownCss=scrDown=document.getElementById("scrDown").style;  
	scrContent_Height=document.getElementById("scrContent").offsetHeight;
  }
  else if (isNS4) 
  {
    scrContentCss=scrContent=document.scrContent;
    scrBarCss=scrBar=document.scrBar;
    scrSlideCss=scrSlide=document.scrSlide;
    scrUpCss=scrUp=document.scrUp;
    scrDownCss=scrDown=document.scrDown;  
	scrContent_Height=scrContent.document.height;
  }else
  { 
    scrContentCss=scrContent.style;
    scrBarCss=scrBar.style;
    scrSlideCss=scrSlide.style;
    scrUpCss=scrUp.style;
    scrDownCss=scrDown.style; 
    scrContent_Height=scrContent.clientHeight;
  }  

  // Do we need a scrollbar at all? Otherwise don't show it...
  if (scrContent_Height>height)
  {
	scrContent_Top=parseInt(scrContentCss.top);
    scrContent_CurrTop=0;
	scrContent_VisibleHeight=height;
    scrContent_CurrBottom=scrContent_CurrTop+height;
    scrContent_Width=parseInt(scrContentCss.width);

    scrBarCss.height=barheight; 
	if(isNS4) scrBarCss.clip.height=barheight;

	scrBar_Top=parseInt(scrBarCss.top);
    scrBar_Height=parseInt(isNS4?scrBarCss.clip.height:scrBarCss.height);
	scrBar_Left=parseInt(scrBarCss.left);
	scrBar_Width=parseInt(isNS4?scrBarCss.clip.width:scrBarCss.width);
	scrSlide_Top=scrBar_Top;	

	//----------------
	// Event handlers
	//----------------
    if(isNS6)
	{
		scrBar=document.getElementById("scrBar");
	    scrSlide=document.getElementById("scrSlide");
    }

	scrBar.onmousedown=new Function('e','return scrBar_mousedown(e)');
	scrBar.onmouseup=scrBar_mouseup;
    scrBar.onmouseout=scrBar_mouseout;

//    scrBar.onmousedrag=scrBar_mouseout;

	scrBar.onmousemove=scrBar_mousemove;
    scrBar.ondragstart=new Function("return false;");
    scrBar.onkeydown=scrKeydown;

    scrSlide.onmousedown=scrSlide_mousedown;
    scrSlide.onmouseup=scrSlide_mouseup;
	scrSlide.onmouseout=scrSlide_mouseout;
    scrSlide.onmousemove=scrSlide_mousemove;
	scrSlide.ondragstart=new Function("return false;");
    scrSlide.onkeydown=scrKeydown;
	window.onscroll=new Function("scrollTo(0,0);return false;");

    if(isNS4)
	{
	  scrContentCss.clip.bottom=scrContent_CurrBottom;
	  scrBarCss.clip.height=scrBar_Height;
	  scrBarCss.clip.width=scrBar_Width;
	  scrBar.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP|Event.MOUSEMOVE|Event.MOUSEOUT);
	  scrSlide.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP|Event.MOUSEMOVE|Event.MOUSEOUT|Event.KEYDOWN);
	}
	else
	{
      // IE and NS6+
	  scrContentCss.clip="rect("+scrContent_CurrTop+"px,"+scrContent_Width+"px,"+scrContent_CurrBottom+"px,0px)";
	  scrBarCss.clip="rect(0px,"+scrBar_Width+"px,"+scrBar_Height+"px,0px)";
	}

    scrSlide_setsize();
    scrSlide_move();

	// Make the scrollthingies visible
    scrBarCss.visibility="visible";
	scrSlideCss.visibility="visible";
	scrUpCss.visibility="visible";
	scrDownCss.visibility="visible";
  }
  scrContentCss.visibility="visible";
}

function scrSetTimeout(what, time)
{
  if (timer!=null) clearTimeout(timer);
  timer=setTimeout(what, time);
}

function scrClearTimeout()
{
  if (timer!=null) clearTimeout(timer);
  timer=null;
}

//-----------------------------------------------------------------------------
// BAR
//-----------------------------------------------------------------------------
function scrBar_mousedown(e)
{ 
  bBarMouseDown=true;
  nMouseX=isNSany?e.pageX:event.clientX;
  nMouseY=isNSany?e.pageY:event.clientY;

  // Get direction for move
  if (nMouseY<scrSlide_Top) nScrDir=-1; else nScrDir=1;
  scrBar_clicked(e);
  //  setTimeout("scrBar_checkmouse()", scrFirstDelay);
  scrSetTimeout("scrBar_checkmouse()",scrFirstDelay); 
}
function scrBar_mouseup()
{
  bBarMouseDown=false;
  // release mouse, shouldn't be neccessary, but just in case...
  if(!isNSany)mouseRelease();
  clearTimeout(timer);
}
function scrBar_mouseout(e)
{
  bBarMouseDown=false;
}
function scrBar_mousemove(e)
{
  nMouseX=isNSany?e.pageX:event.clientX;
  nMouseY=isNSany?e.pageY:event.clientY;
}
function scrBar_checkmouse()
{
  if (bBarMouseDown)
  {
	if ((nScrDir==-1 && nMouseY<scrSlide_Top+scrSlide_Height
	  ||nScrDir==1 && nMouseY>scrSlide_Top)
      && nMouseX>scrBar_Left
	  && nMouseX<scrBar_Left+scrBar_Width
	  && nMouseY>scrBar_Top
	  && nMouseY<scrBar_Top+scrBar_Height)
	{
      scrBar_clicked(0,nMouseX,nMouseY);
	}
    scrSetTimeout("scrBar_checkmouse()",scrSecondDelay);
  }
}
function scrBar_clicked(e,inmousex,inmousey)
{
  // get click position
  if (typeof(inmousex)=="undefined")
  {
	mousex=isNSany?e.pageX:event.x;
    mousey=isNSany?e.pageY:event.y;
  }else
  {
    mousex=inmousex;
	mousey=inmousey;
  }
  // Where did the click occur?
  if (mousey<scrSlide_Top)
  {
    // Move up, one page
 	scrUpPage();
  }else if (mousey>scrSlide_Top+scrSlide_Height)
  {
    // Move down, one page
	scrDownPage();
  }
}

//-----------------------------------------------------------------------------
// SLIDE
//-----------------------------------------------------------------------------
function scrSlide_mousedown(e)
{
  if (!isNS4) mouseCapture(scrSlide_mousemove, scrSlide_mouseup)
  nSlideMouseDownYoffset=isNSany?e.pageY-scrSlide_Top:event.y-scrSlide_Top;
  bSlideMouseDown=true;  
}
 
function scrSlide_mouseup(e)
{
  bSlideMouseDown=false;
  if(!isNS4)mouseRelease();
}
function scrSlide_mouseout(e)
{
  if (isNS4)
  {
	bSlideMouseDown=false;
  }
}

function scrSlide_mousemove(e)
{

  if (bSlideMouseDown)//  && (isNSany || event.button==1) ) 
  {
    // Move 
	scrSlide_Top=isNSany?e.pageY-nSlideMouseDownYoffset:event.y-nSlideMouseDownYoffset;
    scrSlide_moveto(scrSlide_Top);
  }
/*  else
  {
    bSlideMouseDown=false;
  }*/
  return false;
}

function scrSlide_moveto(targetY)
{
  scrSlide_Top=targetY;
  if (scrSlide_Top<scrBar_Top) scrSlide_Top=scrBar_Top;
  else if(scrSlide_Top+scrSlide_Height>scrBar_Top+scrBar_Height) {scrSlide_Top=(scrBar_Top+scrBar_Height)-scrSlide_Height};

  // Get distance from top (0-(height of bar-height of slide))
  value=scrSlide_Top-scrBar_Top;
  // Normalize it, make it a number from 0-1
  value/=(scrBar_Height-scrSlide_Height);
  // Multiply it with height of scrollwindow-visible height
  value*=(scrContent_Height-scrContent_VisibleHeight);
  scrTo(value);
}
function scrSlide_move()
{
  // Calculate new position
  scrSlide_Top=scrBar_Top+(scrContent_CurrTop/(scrContent_Height-(scrContent_CurrBottom-scrContent_CurrTop)))*(scrBar_Height-scrSlide_Height); 
  scrSlideCss.top=scrSlide_Top;
}


function scrSlide_setsize()
{

  // Calculate new size (bar height / number of pages)
  scrSlide_Height=scrBar_Height/(scrContent_Height/(scrContent_CurrBottom-scrContent_CurrTop));
  if(scrSlide_Height<scrSlide_MinHeight) scrSlide_Height=scrSlide_MinHeight;

  scrSlideCss.height=scrSlide_Height;
  if (isNS4)
  {
    scrSlideCss.clip.height=scrSlide_Height;
  }else
  {
    scrSlideCss.clip="rect("+0+"px,"+scrSlideCss.width+","+scrSlide_Height+"px,0px)";
  }
}

//-----------------------------------------------------------------------------
// UP/DOWN
//-----------------------------------------------------------------------------
function scrUpDown_mouseover(nDir)
{
  scrUDspeed=scrUDmouseoverspeed;
  scrSetTimeout("scrUpDown_checkmouse("+nDir+")",scrSecondDelay);
}
function scrUpDown_mousedown(nDir)
{
  bUDMouseDown=true;  
  scrUDspeed=scrUDmousedownspeed;
}

function scrUpDown_checkmouse(nDir)
{
  scrBy(nDir*scrUDspeed);
  scrSetTimeout("scrUpDown_checkmouse("+nDir+")",scrSecondDelay);
}
function scrUpDown_mouseup()
{
  bUDMouseDown=false;
  scrUDspeed=scrUDmouseoverspeed;
  // Set focus to the slide
  scrSlide.focus();
}
function scrUpDown_mouseout()
{
  bUDMouseDown=false;
  scrClearTimeout();
  scrSlide.focus();
}

//-----------------------------------------------------------------------------
// OTHER FUNCTIONS
//-----------------------------------------------------------------------------
function scrUpPage()
{
  scrBy(-(scrContent_CurrBottom-scrContent_CurrTop));
}
function scrDownPage()
{
  scrBy((scrContent_CurrBottom-scrContent_CurrTop));
}
function scrTo(value)
{
  // Get current position, and compare it with value
  scrBy(value-scrContent_CurrTop);
}
function scrScrollIfNeccessary(value,height)
{ 
  if (value<scrContent_CurrTop)
  {
    scrTo(value);
  }
  else if(value+height>scrContent_CurrBottom)
  {
    scrTo(value+height-scrContent_VisibleHeight)
  }
  else
    return false;
  return true;
}

function scrBy(value)
{
  // Get the actual amount scrolled
  if (scrContent_CurrTop+value<0)
  {
    value=-(scrContent_CurrTop);
  }else if(scrContent_CurrBottom+value>scrContent_Height)
  {
    value=scrContent_Height-scrContent_CurrBottom;
  }
  
  // Scroll by that amount
  scrContent_CurrTop+=value;
  scrContent_CurrBottom+=value;

  // Adjust position
  scrContentCss.top=-scrContent_CurrTop+scrContent_Top;	

  if(isNS4)
  {
	scrContentCss.clip.bottom=scrContent_CurrBottom;
    scrContentCss.clip.top=scrContent_CurrTop;
  } 
  else
  {
    scrContentCss.clip="rect("+scrContent_CurrTop+"px,"+scrContent_Width+"px,"+scrContent_CurrBottom+"px,0px)";
  }

  // Move the slide
  scrSlide_move();
}


function mouseCapture(newMouseMove, newMouseUp)
{
  if (bCaptured) mouseRelease();
  bCaptured=true;
  if(isNS6)
  {
	document.addEventListener("mousemove",scrSlide_mousemove,true);
	document.addEventListener("mouseup",scrSlide_mouseup,true);
  }else
  {
	  oldMouseMove=document.onmousemove;
	  oldMouseUp=document.onmouseup;
	  document.onmousemove=newMouseMove;
	  document.onmouseup=newMouseUp;
  }
}
function mouseRelease()
{
  if (bCaptured)
  {
    if (isNS6)
	{
	  document.removeEventListener("mousemove",scrSlide_mousemove,true);
	  document.removeEventListener("mouseup",scrSlide_mouseup,true);
	}else
	{
	  document.onmousemove=oldMouseMove;
	  document.onmouseup=oldMouseUp;
	}
  }
}
function scrActivateKeys()
{
  scrContent.onkeydown=scrKeydown;  
}
function scrDeActivateKeys()
{
  scrContent.onkeydown=new Function("return true;");
}

function scrKeydown(e)
{
  if (isNS4) return true;
  switch(event.keyCode)
  {
    case 33: // Pageup
      scrUpPage();
	  break;
	case 34: // Pagedown
	  scrDownPage();
	  break;
	case 40: // Down
	  scrBy(scrUDarrowspeed);
	  break;
	case 38: // Up
	  scrBy(-scrUDarrowspeed);
	  break;
	default:
	//  alert(event.keyCode);
	  return true;
	  break;

  }
  
  return false;
  
 
}

activeFormItem=null;
function onFormItemFocus(item,nofocus)
{
  if(!isNS4)
  {
   	if(scrScrollIfNeccessary(getRelativeTopOffset(item,scrContent),item.offsetHeight) && !nofocus)
	{
	  item.focus();
    }
  }
  activeFormItem=this;
  scrDeActivateKeys(); // Not possible to scroll with keys
}
function onFormItemBlur(item)
{
  activeFormItem=null;
  scrActivateKeys(); // Possible to scroll with keys
}

function getRelativeTopOffset(item,baseobject)
{
  var curritem=item;
  var hiarchy=".";
  oOffset=0;
  while(curritem!=baseobject)
  {
	oOffset+=curritem.offsetTop;;
	curritem=curritem.offsetParent;
  }
  return oOffset;
}

