﻿var CurrentPosition = 0;
var Length = document.getElementById("PhotoContainer").offsetWidth;
var Timer;

function AdjustScroller(finalPos){
    var increment;
    if(CurrentPosition < finalPos){
        increment = Math.ceil((finalPos - CurrentPosition) / 10);  
        if(increment < 2){
            increment = 2;
        }
        CurrentPosition = CurrentPosition + increment;
        if(CurrentPosition >= finalPos){
            CurrentPosition = finalPos;
            document.getElementById("PhotoContainer").style.marginLeft = -finalPos + "px";
            checkScrollBack();
            checkScrollForward();
        }else{
            document.getElementById("PhotoContainer").style.marginLeft = -CurrentPosition + "px";
            Timer = setTimeout("AdjustScroller(" + finalPos + ")",10);
        }
    }else{
        increment = Math.ceil((finalPos - CurrentPosition) / 10);
        if(increment > -2){
            increment = -2;
        }
        CurrentPosition = CurrentPosition + increment;
        if(CurrentPosition <= finalPos){
            CurrentPosition = finalPos;
            document.getElementById("PhotoContainer").style.marginLeft = -finalPos + "px";
            checkScrollBack();
            checkScrollForward();
        }else{
            document.getElementById("PhotoContainer").style.marginLeft = -CurrentPosition + "px";
            Timer = setTimeout("AdjustScroller(" + finalPos + ")",10);
        }
    }
}

function ScrollBack(){
    if(CurrentPosition > 0){
        DeactivateScrollers();
        AdjustScroller(CurrentPosition - 900);
    }
}

function checkScrollBack(){
    if(CurrentPosition <= 0){
        document.getElementById("LeftScroller").className = "NoScroll";
        document.getElementById("LeftScroller").onclick=null;
    }else{
        document.getElementById("LeftScroller").className = "Scroller";
        document.getElementById("LeftScroller").onclick=function(){ScrollBack();};
    }
}

function ScrollForward(){
    if(CurrentPosition + 900 < Length){
        DeactivateScrollers();
        AdjustScroller(CurrentPosition + 900);
    } 
}

function checkScrollForward(){
    if(CurrentPosition + 900 >= Length){
        document.getElementById("RightScroller").className = "NoScroll";
        document.getElementById("RightScroller").onclick=null;
    }else{
        document.getElementById("RightScroller").className = "Scroller";
        document.getElementById("RightScroller").onclick=function(){ScrollForward();};
    }
}

function DeactivateScrollers(){
    document.getElementById("LeftScroller").onclick=null;
    document.getElementById("RightScroller").onclick=null;
    document.getElementById("LeftScroller").className = "NoScroll";
    document.getElementById("RightScroller").className = "NoScroll";
}

checkScrollBack();
checkScrollForward();
