﻿//Written by Mark Savage for MidwayUSA.com

var imageArray = [];
var rightEdge; //make this the left position of the last image
var startingLeft; //make this the left position of the first image
var distanceMoved = 0;
var autoScrollInterval;

//user control attributes
var _imageCount;
var _imagePadding; //distance you want between images
var _msBetweenFrames; //time between animation frames - increase to slow down the animation
var _animationIntervalInPixels; //distance to move each animation - should be a multiple of the image padding to avoid goofiness
var _imageNamePrefix;
var _msBetweenAutoScroll;
var _autoScroll;

function initializeImageScrollerProperties(imageCount, imagePadding, msBetweenFrames, 
    animationIntervalInPixels, imageNamePrefix, autoScroll, msBetweenAutoScroll) {
    _imageCount = imageCount;
    _imagePadding = imagePadding;
    _msBetweenFrames = msBetweenFrames;
    _animationIntervalInPixels = animationIntervalInPixels;
    _imageNamePrefix = imageNamePrefix;
    _autoScroll = autoScroll;
    _msBetweenAutoScroll = msBetweenAutoScroll;
   
    startingLeft = -_imagePadding;
    rightEdge = startingLeft + ((_imageCount - 1) * _imagePadding);
}

function initializeImages() {
	for (i = 0; i < _imageCount; i++) {
		imageArray[i] = document.getElementById(_imageNamePrefix + (i));		
	}
}

function autoScrollAllImages() {
    animateAllImages(false, _imagePadding);    
    autoScrollInterval = setInterval("animateAllImages(false," + _imagePadding + ")", _msBetweenAutoScroll);
    //setInterval("animateAllImages(false," + _imagePadding + ")", (_imagePadding / _animationIntervalInPixels) * _msBetweenFrames);
}

function animateAllImages(moveRight, distanceToMove) {
//    if (_autoScroll) {
//        clearInterval(autoScrollInterval);
//    }
    
    moveAllImages(moveRight, _animationIntervalInPixels);
    
    distanceMoved = distanceMoved + _animationIntervalInPixels;
    
    if (distanceMoved < distanceToMove) {
        setTimeout("animateAllImages(" + moveRight + "," + distanceToMove + ")", _msBetweenFrames);    
    }    
    else {       
        distanceMoved = 0;
    }    
}

function moveAllImages(moveRight, distanceToMove) {
    if (imageArray == "") {
        initializeImages();
    }
    
    for (i = 0; i < _imageCount; i++) {        
		moveImage(imageArray[i], moveRight, distanceToMove);						
	}
}

function moveImage(imageObj, moveRight, distanceToMove) {    
    var currentLeft = parseInt(imageObj.style.left);
    var endingLeft;
    
    if (moveRight) {
        endingLeft = currentLeft + distanceToMove;
        
        if (endingLeft < rightEdge + _imagePadding) {
            imageObj.style.left = endingLeft + "px";
        }
        else {
            imageObj.style.left = startingLeft + "px";
        }  
    }
    else {
        endingLeft = currentLeft - distanceToMove;
                
        if (endingLeft > startingLeft - _imagePadding) {
            imageObj.style.left = endingLeft + "px";
        }
        else {
            imageObj.style.left = rightEdge + "px";
        }        
    }         
}