

//Points to the div to be moved
var scrollDiv;

//Cookie holding the last position
var scrollCook=null;

var currentLeft=0;

//These must be initialized by the aspx-page.
var totalNoImages;
var pixelsPrImage;
var imagesPrScreen;

function scroll(totalLength, sign, stepSize, maxScroll) {
    scrollDiv=document.getElementById("scrolldiv");
    var leftAfterScroll=currentLeft + totalLength*sign;
    if (leftAfterScroll < (-1)*maxScroll && 1*sign==-1) {
        //alert("No more images");
    } else {  
        if (leftAfterScroll>0) {
            leftAfterScroll=0;
            totalLength= Math.floor((-1)*currentLeft);
        }
        if (totalLength>0) {
            var factor=(totalLength-stepSize)/(totalLength-1);//This can be calculated under the condition that the last step must equal 1.
            scrollUtil(scrollDiv,totalLength,sign,stepSize,factor,0);
        }
     }
     
    
}


function scrollUtil(scrollDiv,totalLength, sign,stepSize,factor,scrolled) {
    
    synchScrollNo();
    var DELAY=10;
    var actualStep = Math.ceil(stepSize)
    if (actualStep+scrolled>totalLength) {
        //The last step will bring us pass the intended length, cut-off
        actualStep=totalLength-scrolled;
    }
    //Even if we haven't completed the totalLength a number of concurrent scrolls
    //might add up to more than max no of images to the left or right
    var leftAfterThisScroll= currentLeft + sign*Math.round(actualStep);
    
    //If we pass one of the borders then cut-off
    if (leftAfterThisScroll>0) {
        //Just make the actual step equal to the currentLeft.
        actualStep = (-1)*currentLeft;
    }
    //Did we pass the right border?
    if (leftAfterThisScroll < -getMaxPosition()) {
        //currentLeft will be negative here, hence just add them
        actualStep = getMaxPosition()+currentLeft;
    }
    
    if (actualStep>0) {
        setCurrentPos(currentLeft + sign*Math.round(actualStep));
        var newscrolled = scrolled + actualStep;
        window.setTimeout("scrollUtil(scrollDiv,"+totalLength+","+sign+","+stepSize*factor+","+factor+","+newscrolled+")", DELAY);
         
    } else {
        //Make a last check that we didn't pass the borders at this point.
        if (currentLeft>0) {
            setCurrentPos(0);
        }
        if (currentLeft < -getMaxPosition()) {
            setCurrentPos(-getMaxPosition());
        }
        //Done
        setCookiePosition(currentLeft);        
    }   
}
var maxPosition;

function getMaxPosition() {
    if (!maxPosition) {
        var _noPages=getTotalNoPages();
        //The maximal position is the left-most pixel of the last page
        maxPosition=(_noPages-1)*imagesPrScreen*pixelsPrImage + 1;
    }
    return maxPosition;
}

function setCurrentPos(pos) {
        currentLeft = pos;
        scrollDiv.style.left = pos+"px";
}

function setCookiePosition(pos) {
        if (scrollCook!=null) {
            scrollCook.position=Math.floor(pos);
            scrollCook.store();
            synchScrollNo();
        }
}

function getScrollCookie() {
    var cook=new Cookie(document, "scrollPosition", 2);
    cook.load()
    return cook;
}

function getTotalNoPages() {
    return Math.ceil(totalNoImages/imagesPrScreen);
}

function initScroll() {
    scrollCook = getScrollCookie();

    
    if (!scrollCook.position) {
        //Set default
        scrollCook.position=0;
    } 
    
    
    //Read position from the cookie.
    currentLeft=parseInt(scrollCook.position);
    
    //If we have parsed the right-most border, reset now.
    var currentLeftImageNo=Math.round((-1)*(currentLeft)/pixelsPrImage);
    var currentLeftImageNo2=Math.max(1,currentLeftImageNo+1);
    var totalNoPages=getTotalNoPages();
    var curPage=Math.ceil(currentLeftImageNo2/imagesPrScreen);
    if (curPage>totalNoPages) {
        currentLeft=0;
        scrollCook.position=0;
    }
        
    //Do initial scroll
    scrollDiv=document.getElementById("scrolldiv");
    scrollDiv.style.left=currentLeft+"px";
    synchScrollNo();
}


function setRightArrowNo(noText) {
    document.getElementById('scrollno').innerHTML=noText;
}

function synchScrollNo() {
    //Only do it when cookies are enabled 
    if (scrollCook!=null) {
        var currentLeftImageNo=Math.round((-1)*(currentLeft)/pixelsPrImage);
        var currentLeftImageNo2=Math.max(1,currentLeftImageNo+1);
        var totalNoPages=Math.ceil(totalNoImages/imagesPrScreen);
        var curPage=Math.ceil(currentLeftImageNo2/imagesPrScreen);
        var prefix;
        if (curPage<10) prefix="&nbsp;";
        else prefix="";
        setRightArrowNo(prefix+curPage+"/"+totalNoPages);
    }
}

//Use this to initialize the scrolling position cookie to 0.
function initScrollPosition() {
    scrollCook = getScrollCookie();
    if (scrollCook.position) {
        //Set to zero
        scrollCook.position=0;
        scrollCook.store();
    } 
}
    