﻿var rottimeout=0;
var sourcey=rotindex * frameheight;
var maxtotaldiff=2*frameheight;
var arbiscrolltimeout=0;
var arbiscrollruntimeout=0;
  
function scrollup(){
	if(rotindex <= 0) { return; }
    rotindex=rotindex - 1; //Math.max(0,rotindex-1);
    if(rottimeout!=0) window.clearTimeout(rottimeout);
    sourcey=document.getElementById('divRot').scrollLeft;
    rotgo();
    //resetarbiscroll();
}
function scrolldown(){
	if(rotindex >= actmaxindex) { return; }
    rotindex=rotindex + 1; //Math.min(maxindex,rotindex+1);
    if(rottimeout!=0) window.clearTimeout(rottimeout);
    sourcey=document.getElementById('divRot').scrollLeft;
    rotgo();
    //resetarbiscroll();
}

function arbiscrollstop(){
    if(arbiscrolltimeout!=0) window.clearTimeout(arbiscrolltimeout);
    if(arbiscrollruntimeout!=0) window.clearTimeout(arbiscrollruntimeout);
}

function resetarbiscroll(){
    arbiscrollstop();
    arbiscrolltimeout=window.setTimeout("arbiscroll();",2000000);//Delay has been inflated so gallery remains stationery
}

function arbiscrollresume(){
    resetarbiscroll();
}

function arbiscrollrun(){
    arbiscrollruntimeout=0;
    
    var divRot;
    divRot=document.getElementById('divRot');
    
    var st;
    st=divRot.scrollLeft;
    st+=Math.max(Math.round(arbiscrollspeed*arbiscrollstepinterval/1000),1);
    
    rotindex=Math.floor(st/frameheight);
    var zmaxindex=maxindex;
    st %= zmaxindex * frameheight;
    rotindex %= zmaxindex;
    
    divRot.scrollLeft=st;
    
    arbiscrollruntimeout=window.setTimeout("arbiscrollrun();",arbiscrollstepinterval);
}

function arbiscroll(){
    arbiscrolltimeout=0;
    if(rottimeout!=0) window.clearTimeout(rottimeout);
    rottimeout=0;
    arbiscrollrun();
}

resetarbiscroll();

function sgn(vin){ //why is this not part of Math. ??
    if(vin==0) return 0;
    if(vin<0) return -1;
    return 1;
}
function rotgo(){
    rottimeout=0;
    var desty;
    desty=rotindex * frameheight;

    var divRot;
    divRot=document.getElementById('divRot');
    
    var totaldiff;
    totaldiff=desty-sourcey;
    if(Math.abs(totaldiff) > Math.abs(maxtotaldiff)){
        totaldiff=sgn(totaldiff) * maxtotaldiff;
    }
    var diff;
    var delta;
    diff=desty-divRot.scrollLeft;
    
    if(Math.abs(diff) > Math.abs(maxtotaldiff)){
        diff=sgn(diff) * maxtotaldiff;
    }
    
    if(Math.abs(diff) <= 0.5){
        return;
        diff=0.0;
    }
    
    var peakstep;
    peakstep=peakspeed*stepinterval/1000;
    delta=peakstep*Math.cos((1-(Math.abs(diff)/Math.abs(totaldiff)))*Math.PI/2)*sgn(diff);

    if(Math.abs(delta) <= 1.0){
        delta=sgn(delta);
    }
    if(Math.abs(delta) > Math.abs(diff)){
        delta=diff;
    }
    
    
    var sstop;
    sstop=divRot.scrollLeft;
    sstop+=delta;
    
    var zmaxindex=maxindex;
    
    if(sstop > zmaxindex * frameheight){
        sstop %= zmaxindex * frameheight;
        rotindex -= zmaxindex;
    }
    if(sstop < 0){
        sstop += zmaxindex * frameheight;
        rotindex += zmaxindex;
    }
    
    divRot.scrollLeft=sstop;
    
    //document.getElementById('spnDebug').innerHTML='' + rotindex + '<br />' + desty + '<br />' + divRot.scrollTop + '<br />' + delta + '<br/>' + maxindex + '<br/>' + actmaxindex + '<br />' + sstop;
    
    rottimeout=window.setTimeout("rotgo();",stepinterval);
}
function down(){
    scrolldown();
}
function up(){
    scrollup();
}
