var autoScrollID = null;
var animateInterval = null;
var mod = 1;
var lastPos = 0;
var tinyincrease = 0.001;

function scrollHorizontal (value, element, slider) 
{
    //clearInterval(autoScrollID);
    element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth));
    lastPos=value;
}

function animateScrollHorizontal (value, element, slider) 
{
    mod = (value-lastPos >= 0)?1:-1;
    
    if(value != lastPos)
    {
        //alert(value);
        clearInterval(autoScrollID);
        autoScrollID = null;
        if(lastPos >= 1 || productSlider.value >= 1) lastPos = 1;
        animateInterval = setInterval(
            function() 
            {
                lastPos = calculateDecel(lastPos, value);
                //alert(lastPos + "," + value + "," + mod);
                element.scrollLeft = Math.round(lastPos/slider.maximum*(element.scrollWidth-element.offsetWidth)); 
                if((mod>0 && lastPos >= value) || (mod<0 && lastPos <= value)) 
                { 
                    //alert("done" + lastPos + "," + value + "," + mod);
                    clearInterval(animateInterval); 
                    animateInterval = null;
                    lastPos=value; 
                    setTimeout("autoScroll()", 2000);
                }
            }
        , 30);
    }
    else
    {
        element.scrollLeft = Math.round(value/slider.maximum*(element.scrollWidth-element.offsetWidth)); 
    }
}

function autoScroll()
{
    if(autoScrollID==null)
    autoScrollID = setInterval(function(){
        if(productSlider.value+tinyincrease >=1)
        {
            clearInterval(autoScrollID);
            setTimeout("productSlider.setValue(0)", 2000);
        }
        else
        {
            lastPos = productSlider.value+tinyincrease;
            productSlider.setValue(productSlider.value+tinyincrease, 0);
        }
    },50);
}

function calculateDecel(from,to)
{
    var n=from-((from-to)*0.4);
    if(Math.abs(from-to)<0.01) return to;
    else return n;
}