/*******
(C) www.dhtmlgoodies.com, September 2005
This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.
Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.
Thank you!
www.dhtmlgoodies.com
Alf Magne Kalleland
*******
Remake and add-ons : Jokaweb 2008
*******/

var contentHeight=new Array(); // The total height of the content
var visibleContentHeight=new Array();
var scrollActive=new Array();

var scrollHandleObj=new Array(); // reference to the scroll handle
var scrollHandleHeight=new Array();
var scrollbarTop=new Array();
var eventYPos=new Array();

var scrollbuttonActive=new Array();
var scrollbuttonDirection=new Array();
var scrollbuttonSpeedDef=2;
var scrollbuttonSpeed=new Array(); // How fast the content scrolls when you click the scroll buttons(Up and down arrows)
var scrollTimerDef = 10;// Also how fast the content scrolls. By decreasing this value, the content will move faster
var scrollTimer=new Array();

var scrollMoveToActive=new Array();
var scrollMoveToYPosition=new Array();

var currentScrolledLayerNum=-1;

var allInitedScrollers=new Array();
function scrolldiv_refreshScroll(){
  for(var numdiv in allInitedScrollers){
  if( /\D/.test(numdiv) )continue;
  if(!allInitedScrollers[numdiv])continue;
  //scrolldiv_setWidth($countScrollers,".$largeur.");
  //scrolldiv_setHeight($countScrollers,".$hauteur.");
  scrolldiv_initScroll(numdiv);
 }
}

function scrollDiv_startScroll(e){
 var numdiv=this.id.substr(this.id.lastIndexOf('_')+1,this.id.length);
 if(document.all)e=event;
 scrollbarTop[numdiv]=document.getElementById('scrolldiv_theScroll_'+numdiv).offsetTop;
 eventYPos[numdiv]=e.clientY;
 scrollActive[numdiv]=true;
}

function scrollDiv_stopScroll(){
 var numdiv=currentScrolledLayerNum;
 currentScrolledLayerNum=-1;
 scrollActive[numdiv]=false;
 scrollbuttonActive[numdiv]=false;
 scrollMoveToActive[numdiv]=false;
}

function scrollDiv_scroll(e){
 var numdiv=currentScrolledLayerNum;
 if(!scrollActive[numdiv])return;
 if(document.all)e=event;
 if(e.button!=1 && document.all)return;
 var scrollerTopPos=scrollbarTop[numdiv]+e.clientY-eventYPos[numdiv];
 if(scrollerTopPos<0)scrollerTopPos=-1;
 if( (scrollerTopPos/1) > (visibleContentHeight[numdiv]-(scrollHandleHeight[numdiv]+4)/1) )
  scrollerTopPos = visibleContentHeight[numdiv]-(scrollHandleHeight[numdiv]+4);
 if(scrollerTopPos<0)scrollerTopPos=-1;
 document.getElementById('scrolldiv_theScroll_'+numdiv).style.top = scrollerTopPos + 'px';
 var scrollDivTop=-( Math.floor((contentHeight[numdiv]) * ((scrollerTopPos)/(visibleContentHeight[numdiv]-scrollHandleHeight[numdiv]))) );
 if(scrollDivTop>0)scrollDivTop=0;
 document.getElementById('scrolldiv_content_'+numdiv).style.top = scrollDivTop+'px';
}

function scrolldiv_scrollWheel(evt){
 var numdiv=this.id.substr(this.id.lastIndexOf('_')+1,this.id.length);
  try{if(!evt)evt=event}catch(e){return;}
 var delta=0;
 if(evt.wheelDelta){  delta = evt.wheelDelta/120;
  if(window.opera)delta = -delta; }else if(evt.detail){  delta = -evt.detail/3;
 }
 currentScrolledLayerNum=numdiv;
  scrollbarTop[numdiv]=document.getElementById('scrolldiv_theScroll_'+numdiv).offsetTop;
 if(delta<0)var wheelDir=1;
 else var wheelDir=2
 scrolldiv_scrollButton(evt,wheelDir,numdiv);
 scrolldiv_scrollButtonStop(evt,numdiv);
 if (evt.preventDefault)evt.preventDefault();
 evt.returnValue = false;
}

function scrolldiv_scrollMoveToInit(evt){
 var numdiv=this.id.substr(this.id.lastIndexOf('_')+1,this.id.length);
 currentScrolledLayerNum=numdiv;
 if(document.all)evt=event;
 scrollMoveToActive[numdiv]=true;
 arrd=relativ2abspos( $('scrolldiv_scrollbar_'+numdiv),true );
 scrollMoveToYPosition[numdiv]=evt.clientY-arrd['T'];
 if(document.getElementById('scrolldiv_theScroll_'+numdiv).offsetTop > scrollMoveToYPosition[numdiv])
  scrollbuttonDirection[numdiv]=scrollbuttonSpeed[numdiv]*-2;
 else scrollbuttonDirection[numdiv]=scrollbuttonSpeed[numdiv]*2;
 scrolldiv_scrollMoveTo(evt,numdiv);
}

function scrolldiv_scrollMoveTo(evt,numdiv){
 if(!numdiv)numdiv=this.id.substr(this.id.lastIndexOf('_')+1,this.id.length);
 if(!scrollMoveToActive[numdiv] || scrollActive[numdiv])return;
 var scrollerTopPos = document.getElementById('scrolldiv_theScroll_'+numdiv).style.top.replace('px','');
 scrollerTopPos = scrollerTopPos/1 + scrollbuttonDirection[numdiv];
 if(scrollerTopPos<-1){
  scrollerTopPos=-1;
  scrollMoveToActive[numdiv]=false;
 }
 if(scrollerTopPos/1>visibleContentHeight[numdiv]-(scrollHandleHeight[numdiv]+4)/1){
  scrollerTopPos=visibleContentHeight[numdiv]-(scrollHandleHeight[numdiv]+4);
  scrollMoveToActive[numdiv]=false;
 }
 if(scrollerTopPos<0)scrollerTopPos=-1;
 if(scrollbuttonDirection[numdiv]<0 && scrollerTopPos<scrollMoveToYPosition[numdiv]-scrollHandleHeight[numdiv]/2)return;
 if(scrollbuttonDirection[numdiv]>0 && scrollerTopPos>scrollMoveToYPosition[numdiv]-scrollHandleHeight[numdiv]/2)return;
 document.getElementById('scrolldiv_theScroll_'+numdiv).style.top = scrollerTopPos + 'px';
 var scrollDivTop=-( Math.floor((contentHeight[numdiv]) * ((scrollerTopPos)/(visibleContentHeight[numdiv]-scrollHandleHeight[numdiv]))));
 if(scrollDivTop>0)scrollDivTop=0;
 document.getElementById('scrolldiv_content_'+numdiv).style.top = scrollDivTop+'px';
 setTimeout('scrolldiv_scrollMoveTo(false,'+numdiv+')',scrollTimer[numdiv]);
}

function cancelEvent(){
 return false;
}

function scrolldiv_scrollButton(evt,wheelDir,numdiv){
  if(!numdiv)numdiv=this.id.substr(this.id.lastIndexOf('_')+1,this.id.length);
 currentScrolledLayerNum=numdiv;
 if(!wheelDir)wheelDir=false;
 if(!wheelDir && this.id=='scrolldiv_scrollDown_'+numdiv)
  scrollbuttonDirection[numdiv]=scrollbuttonSpeed[numdiv];
 else if(!wheelDir)scrollbuttonDirection[numdiv]=scrollbuttonSpeed[numdiv]*-1;
 else if(wheelDir==1)scrollbuttonDirection[numdiv]=5;
 else if(wheelDir==2)scrollbuttonDirection[numdiv]=-5;
 else scrollbuttonDirection[numdiv]=scrollbuttonSpeed[numdiv]*-1;
 scrollbuttonActive[numdiv]=true;
 scrolldiv_scrollButtonScroll(evt,numdiv);
}

function scrolldiv_scrollButtonScroll(e,numdiv){
 if(!numdiv)numdiv=this.id.substr(this.id.lastIndexOf('_')+1,this.id.length);
 if(!scrollbuttonActive[numdiv])return;
 var scrollerTopPos = document.getElementById('scrolldiv_theScroll_'+numdiv).style.top.replace('px','');
 scrollerTopPos = scrollerTopPos/1 + scrollbuttonDirection[numdiv];
 if(scrollerTopPos<-1){
  scrollerTopPos=-1;
  scrollbuttonActive[numdiv]=false;
 }
  if(scrollerTopPos/1>visibleContentHeight[numdiv]-(scrollHandleHeight[numdiv]+4)/1){
  scrollerTopPos = visibleContentHeight[numdiv]-(scrollHandleHeight[numdiv]+4);
  scrollbuttonActive[numdiv]=false;
 }
  document.getElementById('scrolldiv_theScroll_'+numdiv).style.top = scrollerTopPos + 'px';
 var scrollDivTop=-(Math.floor((contentHeight[numdiv])*((scrollerTopPos)/(visibleContentHeight[numdiv]-scrollHandleHeight[numdiv]))));
 if(scrollDivTop>0)scrollDivTop=0;
 document.getElementById('scrolldiv_content_'+numdiv).style.top =scrollDivTop+'px';
 setTimeout('scrolldiv_scrollButtonScroll(null,'+numdiv+')',scrollTimer[numdiv]);
}

function scrolldiv_scrollButtonStop(e,numdiv){
  numdiv=currentScrolledLayerNum;
 currentScrolledLayerNum=-1;
  scrollbuttonActive[numdiv]=false;
}

function scrolldiv_initScroll(numdiv){
  allInitedScrollers[numdiv]=true;
 scrollbuttonSpeed[numdiv]=scrollbuttonSpeedDef;
 visibleContentHeight[numdiv]=document.getElementById('scrolldiv_scrollbar_'+numdiv).offsetHeight+4 ;
 contentHeight[numdiv]=document.getElementById('scrolldiv_content_'+numdiv).offsetHeight-visibleContentHeight[numdiv];
 if(contentHeight[numdiv]<0)contentHeight[numdiv]=0;
 scrollHandleObj[numdiv]=document.getElementById('scrolldiv_theScroll_'+numdiv);
 scrollHandleHeight[numdiv]=scrollHandleObj[numdiv].offsetHeight;
 scrollbarTop[numdiv]=document.getElementById('scrolldiv_scrollbar_'+numdiv).offsetTop;
 document.getElementById('scrolldiv_theScroll_'+numdiv).onmousedown=scrollDiv_startScroll;
 document.body.onmousemove=scrollDiv_scroll;
 document.getElementById('scrolldiv_scrollbar_'+numdiv).onselectstart=cancelEvent;
 document.getElementById('scrolldiv_theScroll_'+numdiv).onmouseup=scrollDiv_stopScroll;
 if(document.all)document.body.onmouseup = scrollDiv_stopScroll;
 else document.documentElement.onmouseup = scrollDiv_stopScroll;
 document.getElementById('scrolldiv_scrollDown_'+numdiv).onmousedown=scrolldiv_scrollButton;
 document.getElementById('scrolldiv_scrollUp_'+numdiv).onmousedown=scrolldiv_scrollButton;
 document.getElementById('scrolldiv_scrollDown_'+numdiv).onmouseup=scrolldiv_scrollButtonStop;
 document.getElementById('scrolldiv_scrollUp_'+numdiv).onmouseup=scrolldiv_scrollButtonStop;
 document.getElementById('scrolldiv_scrollUp_'+numdiv).onselectstart = cancelEvent;
 document.getElementById('scrolldiv_scrollDown_'+numdiv).onselectstart = cancelEvent;
 document.getElementById('scrolldiv_scrollbar_'+numdiv).onmousedown = scrolldiv_scrollMoveToInit;
  if(document.all || !(!iex)){
  document.getElementById('scrolldiv_scrollbar_'+numdiv).onmousewheel=scrolldiv_scrollWheel;
  document.getElementById('dscrolldiv_'+numdiv).onmousewheel=scrolldiv_scrollWheel;
 }else if(document.getElementById('scrolldiv_scrollbar_'+numdiv).addEventListener){//mozz
  document.getElementById('scrolldiv_scrollbar_'+numdiv).addEventListener('DOMMouseScroll', scrolldiv_scrollWheel, false);
  document.getElementById('dscrolldiv_'+numdiv).addEventListener('DOMMouseScroll', scrolldiv_scrollWheel, false);
 }else if(document.getElementById('scrolldiv_scrollbar_'+numdiv).attachEvent){//iex
  document.getElementById('scrolldiv_scrollbar_'+numdiv).attachEvent('onmousewheel', scrolldiv_scrollWheel);
  document.getElementById('dscrolldiv_'+numdiv).attachEvent('onmousewheel', scrolldiv_scrollWheel);
 }else{
  document.getElementById('scrolldiv_scrollbar_'+numdiv).onmousewheel=scrolldiv_scrollWheel;
  document.getElementById('dscrolldiv_'+numdiv).onmousewheel=scrolldiv_scrollWheel;
 }
}


function scrolldiv_setColor(numdiv,rgbColor){
 document.getElementById('scrolldiv_scrollbar_'+numdiv).style.borderColor = rgbColor;
 document.getElementById('scrolldiv_theScroll_'+numdiv).style.backgroundColor = rgbColor;
 document.getElementById('scrolldiv_scrollUp_'+numdiv).style.borderColor = rgbColor;
 document.getElementById('scrolldiv_scrollDown_'+numdiv).style.borderColor = rgbColor;
 document.getElementById('scrolldiv_scrollUp_'+numdiv).style.color = rgbColor;
 document.getElementById('scrolldiv_scrollDown_'+numdiv).style.color = rgbColor;
 document.getElementById('scrolldiv_parentContainer_'+numdiv).style.borderColor = rgbColor;
}
function scrolldiv_setWidth(numdiv,newWidth){
 document.getElementById('dscrolldiv_'+numdiv).style.width=newWidth+'px';
 document.getElementById('scrolldiv_parentContainer_'+numdiv).style.width=newWidth-30+'px';
 document.getElementById('scrolldiv_scrollbar_'+numdiv).style.width=12+'px';
}

function scrolldiv_setHeight(numdiv,newHeight){
 document.getElementById('dscrolldiv_'+numdiv).style.height=newHeight+'px';
 document.getElementById('scrolldiv_parentContainer_'+numdiv).style.height=newHeight-5+'px';
 document.getElementById('scrolldiv_slider_'+numdiv).style.height=(newHeight)+'px';
 document.getElementById('scrolldiv_scrollbar_'+numdiv).style.height=((newHeight-(document.getElementById('scrolldiv_scrollUp_'+numdiv).offsetHeight+document.getElementById('scrolldiv_scrollDown_'+numdiv).offsetHeight) )+1)+ 'px';
}
function setSliderBgColor(numdiv,rgbColor){
 document.getElementById('scrolldiv_scrollbar_'+numdiv).style.backgroundColor=rgbColor;
 document.getElementById('scrolldiv_scrollUp_'+numdiv).style.backgroundColor=rgbColor;
 document.getElementById('scrolldiv_scrollDown_'+numdiv).style.backgroundColor=rgbColor;
}
function setContentBgColor(numdiv,rgbColor){
 document.getElementById('scrolldiv_parentContainer_'+numdiv).style.backgroundColor=rgbColor;
}
function setContentBackground(numdiv,cssBg){
  cssBg=cssBg.replace(";","");
 document.getElementById('dscrolldiv_'+numdiv).style.background=cssBg;
}

function setScrollButtonSpeed(numdiv,newScrollButtonSpeed){
 scrollbuttonSpeed[numdiv]=newScrollButtonSpeed;
}
function setScrollTimer(numdiv,newInterval){
 scrollTimer[numdiv]=newInterval;
}
function setScrollPosition(numdiv,scrollTop){ currentScrolledLayerNum=numdiv;
 scrollMoveToActive[numdiv]=true;
 scrollMoveToYPosition[numdiv]=0;
 scrollbuttonDirection[numdiv]=0;

 document.getElementById('scrolldiv_theScroll_'+numdiv).style.top = '0px';
 document.getElementById('scrolldiv_content_'+numdiv).style.top = '0px';

}

function setScrollContentPosition(numdiv,scrollTop){ if(scrollTop>1)scrollTop=1;
 currentScrolledLayerNum=numdiv;
 scrollMoveToActive[numdiv]=true;
 scrollMoveToYPosition[numdiv]=0;
 scrollbuttonDirection[numdiv]=0;
 document.getElementById('scrolldiv_content_'+numdiv).style.top = scrollTop+'px';
 var nwtop= Math.floor( ( (-scrollTop)/ document.getElementById('scrolldiv_content_'+numdiv).offsetHeight ) * visibleContentHeight[numdiv] );
 if(nwtop<-1)nwtop=-1;
 document.getElementById('scrolldiv_theScroll_'+numdiv).style.top = nwtop+'px';
}
