﻿// Global Variables ---------------------------------------------------------------------
var xPos = 0; 					// starting point
var distance = 0; 				// +/- delta based on left or right
var delta = 10; 				// how many pixels each interval moves
var maxScroll = -1000; 			// how far to the right we can move
var speed = 50; 				// how fast the thing scrolls in milliseconds
var intervalID = 0; 			// the only way to keep track of the interval
var currentID = 0;
// --------------------------------------------------------------------------------------	var xPos = 0;           // starting point

// Called when you mouse over an arrow
function scroll(direction) {
    distance = (direction == "left") ? delta : -1 * delta;
    intervalID = setInterval(autoScroll, speed);
}

// Called when you mouse out or interval expired
function endScroll() {
    if (intervalID != 0) {
        clearInterval(intervalID);
        intervalID = 0;
    }
}

//set scroll position
function setScroll(position) {
    setupScroll();
    var container = document.getElementById("ScrollContainer");
    position = Math.floor((position * -1) + (container.offsetWidth / 2) + 35);
    //alert(position);
    //alert(maxScroll);

    var obj = document.getElementById("ScrollItems");
    if (obj) {
        xPos = (position > 0) ? 0 : position;
        xPos = (xPos < maxScroll) ? maxScroll : xPos;
        obj.style.left = xPos + "px";
    }
}

// This is the interval function ... never call from HTML
function autoScroll() {
    obj = document.getElementById("ScrollItems");
    if (obj) {
        if (xPos > 0 || xPos < maxScroll) {
            xPos = (xPos > 0) ? 0 : maxScroll;
            endScroll();
        }
        else {
            xPos += distance;
            obj.style.left = xPos + "px";
        }
    }
}


function swap(obj, index, rollover) {
    obj.src = "scrollimages/" + index + "-" + rollover + ".png";
}

function setupScroll() {
    var obj = document.getElementById("ScrollItems");
    var container = document.getElementById("ScrollContainer");

    if (obj && container) {
        maxScroll = (obj.offsetWidth * -1) + (container.offsetWidth);

        //hide the arrows if there is nowhere to scroll.
        if (obj.offsetWidth <= container.offsetWidth) {
            var prevArrow = document.getElementById('PreviousPhotoArrow');
            var nextArrow = document.getElementById('NextPhotoArrow');

            prevArrow.style.visibility = "hidden";
            nextArrow.style.visibility = "hidden";

            maxScroll = 0;
        }
    }


}
