﻿var horstopScrollItemWidth;
var horstopScrollBarWidth;
var horstopSpos;
var horstopS; //timer3id
var horstopScrollNode;
var horstopScrollNode_old;
var horstopScrollItemHolder;
var horstopSposStart;
var horstopMainScroll;
var horstopMainScrollInterval = 7000;
function setHorStopScroller(aScrollItemWidth, aScrollBarWidth, aScrollSpeed, aScrollInterval, aFastScrollSpeed, autoscrollholder, aScroller, aScrollLeft, aScrollRight) {
    horstopScrollItemWidth = aScrollItemWidth;
    horstopScrollBarWidth = aScrollBarWidth;
    horstopScrollItemHolder = document.getElementById(aScroller);
    document.getElementById(aScrollLeft).onclick = function() {fHorStopScrollSide(false, aFastScrollSpeed, aScrollInterval);};
    document.getElementById(aScrollRight).onclick = function() {fHorStopScrollSide(true, aFastScrollSpeed, aScrollInterval);};  
    horstopScrollItemHolder.style.left = horstopScrollBarWidth + "px";
    horstopSpos = horstopScrollItemHolder.style.left.substring(0,horstopScrollItemHolder.style.left.length-2);
}
function fHorStopScrollAutoScroll(LeftSide, aFastScrollSpeed,aScrollInterval)
{	clearTimeout(horstopMainScroll);
	clearTimeout(horstopS);   
	horstopSposStart = horstopSpos;
    if (LeftSide) {       
		fHorStopScrollLeft(true, aFastScrollSpeed, aScrollInterval);	
    }else{
        fHorStopScrolRight(true, aFastScrollSpeed, aScrollInterval);
    }
}
function fHorStopScrollSide(LeftSide, speed, scrollInterval) {
    clearTimeout(horstopMainScroll);	// stop de mainscroller - nuttig als de scroll richting moet veranderen!	
	clearTimeout(horstopS);   
	horstopSposStart = horstopSpos;		
    if (LeftSide) {       
		fHorStopScrollLeft(true, speed, scrollInterval);	
    }else{
        fHorStopScrolRight(true, speed, scrollInterval);
    }	
}
function fHorStopScrollLeft(goOn, speed, scrollInterval) {
	horstopScrollItemHolder.style.left = horstopSpos + "px";
    //set horstopSpos later
    if(mod(horstopSpos-horstopScrollBarWidth, horstopScrollItemWidth)==0) {
        horstopScrollNode = horstopScrollItemHolder.lastChild;
        horstopScrollNode_old = horstopScrollItemHolder.firstChild;
        while (horstopScrollNode.nodeType != 1) {
            horstopScrollNode = horstopScrollNode.previousSibling;
        }
        while (horstopScrollNode_old.nodeType != 1) {
            horstopScrollNode_old = horstopScrollNode_old.nextSibling;
        }
            horstopScrollNode = horstopScrollItemHolder.removeChild(horstopScrollNode);
            horstopScrollItemHolder.insertBefore(horstopScrollNode, horstopScrollItemHolder.firstChild);
            horstopScrollNode.style.left = eval(eval(horstopScrollNode_old.style.left.substring(0,horstopScrollNode_old.style.left.length-2))-horstopScrollItemWidth) + "px";
    }    
    horstopSpos = eval(eval(horstopSpos) + scrollInterval);    
	maxpos = horstopSposStart + (horstopScrollItemWidth * 2);
	if((maxpos % horstopScrollItemWidth) != 0)
	{	maxpos = (Math.floor((horstopSposStart / horstopScrollItemWidth)) * horstopScrollItemWidth) + (horstopScrollItemWidth * 2);
	}	
	if (horstopSpos < maxpos)
		horstopS = setTimeout("fHorStopScrollLeft(" + goOn + ", " + speed + "," + scrollInterval + ");", speed);
}
function fHorStopScrolRight(goOn, speed, scrollInterval) {
	horstopSpos = horstopSpos - scrollInterval;
    if(mod(horstopSpos-horstopScrollBarWidth, horstopScrollItemWidth)==0) {
        horstopScrollNode = horstopScrollItemHolder.firstChild;
        horstopScrollNode_old = horstopScrollItemHolder.lastChild;
        while (horstopScrollNode.nodeType != 1) {
            horstopScrollNode = horstopScrollNode.nextSibling;
        }
        while (horstopScrollNode_old.nodeType != 1) {
            horstopScrollNode_old = horstopScrollNode_old.previousSibling;
        }
		horstopScrollNode = horstopScrollItemHolder.removeChild(horstopScrollNode);
		horstopScrollItemHolder.appendChild(horstopScrollNode);
		horstopScrollNode.style.left = eval(eval(horstopScrollNode_old.style.left.substring(0,horstopScrollNode_old.style.left.length-2))+horstopScrollItemWidth) + "px";
    }
    horstopScrollItemHolder.style.left = horstopSpos + "px";
    //set horstopSpos first
	maxpos = horstopSposStart - (horstopScrollItemWidth * 2);	
	if((maxpos % horstopScrollItemWidth) != 0)
	{	maxpos = (Math.ceil((horstopSposStart / horstopScrollItemWidth)) * horstopScrollItemWidth) - (horstopScrollItemWidth * 2);
	}	
    if (horstopSpos > maxpos)
		horstopS = setTimeout("fHorStopScrolRight(" + goOn + ", " + speed + "," + scrollInterval + ");", speed);
}       
function mod(div,base) {
    return Math.round(div - (Math.floor(div/base)*base));
}