var productScroller_container;
var productScroller_pane;
var productScroller_inner;

var productScroller_totalWidth;
var productScroller_paneWidth;
var productScroller_scrollWidth;
var productScroller_wraparoundOffset;
var productScroller_currentOffset = 0;
var productScroller_balking = false;

var productScroller_scrollStepPx_minimum = 2;
var productScroller_scrollStepPx_maximum = 35;
var productScroller_timeoutDelay = 5;

function productScroller_initialize() {
    productScroller_container = document.getElementById('pageHomeInsideProduct');
    productScroller_pane = document.getElementById('pageHomeInsideProductPane');
    productScroller_inner = document.getElementById('pageHomeInsideProductPaneInner');
    var measurableContainer = document.getElementById('pageHomeInsideProductPaneInner_measure');
    
    if(productScroller_container && productScroller_pane && productScroller_inner && measurableContainer) {
        productScroller_totalWidth = measurableContainer.offsetWidth;
        productScroller_paneWidth = productScroller_pane.offsetWidth;
        productScroller_scrollWidth = productScroller_paneWidth - 1;
        productScroller_wraparoundOffset = productScroller_totalWidth / 2;
        
        var aElement = document.createElement('a');
        var imgElement = document.createElement('img');
        
        aElement.id = 'pageHomeInsideProductLeft';
        aElement.title = 'Pan Left';
        aElement.href = 'javascript:productScroller_panLeft()';
        imgElement.src = 'images/scroller/arrow-left.jpg';
        imgElement.alt = '<';
        imgElement.title = 'Pan Left';
        imgElement.width = 56;
        imgElement.height = 62;
        
        aElement.appendChild(imgElement);
        productScroller_container.appendChild(aElement);
        
        aElement = document.createElement('a');
        imgElement = document.createElement('img');
        
        aElement.id = 'pageHomeInsideProductRight';
        aElement.title = 'Pan Right';
        aElement.href = 'javascript:productScroller_panRight()';
        imgElement.src = 'images/scroller/arrow-right.jpg';
        imgElement.alt = '>';
        imgElement.title = 'Pan Right';
        imgElement.width = 57;
        imgElement.height = 62;
        
        aElement.appendChild(imgElement);
        productScroller_container.appendChild(aElement);
    }
}

if(window.addEventListener)  window.addEventListener('load', productScroller_initialize, false);
else if(window.attachEvent)  window.attachEvent('onload', productScroller_initialize);

function productScroller_panLeft() {
    if(productScroller_balking) return;
    productScroller_balking = true;
    productScroller_panLeft_execute(productScroller_scrollWidth);
}

function productScroller_panLeft_execute(pixelsToGo) {
    var scaledPixelStep = productScroll_getScaledPixelStep(pixelsToGo);
    if(pixelsToGo > 0) {
        var pixelsToSlide = Math.min(scaledPixelStep, pixelsToGo);
        productScroller_currentOffset -= pixelsToSlide;  pixelsToGo -= pixelsToSlide;
        if(productScroller_currentOffset <= 0) productScroller_currentOffset = productScroller_wraparoundOffset + productScroller_currentOffset;
        
        productScroller_inner.style.left = '-' + productScroller_currentOffset + 'px';
        setTimeout(function() { productScroller_panLeft_execute(pixelsToGo); }, productScroller_timeoutDelay);
    }
    else
        productScroller_balking = false;
}


function productScroller_panRight() {
    if(productScroller_balking) return;
    productScroller_balking = true;
    productScroller_panRight_execute(productScroller_scrollWidth);
}

function productScroller_panRight_execute(pixelsToGo) {
    var scaledPixelStep = productScroll_getScaledPixelStep(pixelsToGo);
    if(pixelsToGo > 0) {
        var pixelsToSlide = Math.min(scaledPixelStep, pixelsToGo);
        productScroller_currentOffset += pixelsToSlide;  pixelsToGo -= pixelsToSlide;
        if(productScroller_currentOffset >= productScroller_wraparoundOffset) productScroller_currentOffset = productScroller_currentOffset - productScroller_wraparoundOffset;
        
        productScroller_inner.style.left = '-' + productScroller_currentOffset + 'px';
        setTimeout(function() { productScroller_panRight_execute(pixelsToGo); }, productScroller_timeoutDelay);
    }
    else
        productScroller_balking = false;
}

function productScroll_getScaledPixelStep(pixelsToGo) {
    return Math.round(
        (
            (
                productScroller_scrollStepPx_maximum -
                productScroller_scrollStepPx_minimum
            ) *
            Math.sin(
                Math.PI * (
                    pixelsToGo /
                    productScroller_scrollWidth
                )
            )
        ) +
        productScroller_scrollStepPx_minimum
    );
}
