﻿var acceleration = 2;
var startAcceleration = 1;
var velocity = 0;
var nextPosition = 0;
var maxVelocity = 150;
var contentWidth = 900;
var moving = false;
var contentDivWidth = 0;
var animationSpeed = 30;
var currentIndex = 0;
var totalItems = 6;
var contentDiv;

window.onload = GetWidth;

function GetWidth() {
    contentDiv = document.getElementById("contentDiv");
    contentDivWidth = parseInt(contentDiv.style.width);
    GetQueryIndex();
}

function MoveTo(index) {
    if (!moving) {
        if (index >= 0 && index < totalItems) {
            acceleration = startAcceleration;
            var left = parseInt(contentDiv.style.left);

            nextPosition = left + (contentWidth * (currentIndex - index));
            currentIndex = index;
            if (nextPosition < left) {
                moving = true;
                AnimateLeft();
            }
            else if (nextPosition > left) {
                moving = true;
                AnimateRight();
            }
        }
    }
}

function MoveLeft() {
    if (!moving) {
        acceleration = startAcceleration;
        var left = parseInt(contentDiv.style.left);
        nextPosition = left - contentWidth;
        if (nextPosition > (-1 * contentDivWidth)) {
            moving = true;
            AnimateLeft();
        }
        else {
            nextPosition = 0;
            moving = true;
            AnimateRight();
        }
    }
    else {
        // Can't move because already moving
    }
}

function AnimateLeft() {
    var left = parseInt(contentDiv.style.left);

    velocity += acceleration;
    acceleration += 2;
    if (velocity > maxVelocity) {
        velocity = maxVelocity;
    }

    var newPosition = left - velocity;

    if (newPosition < nextPosition) {
        velocity = 0;
        contentDiv.style.left = nextPosition + "px";
        moving = false;
    }
    else {
        contentDiv.style.left = newPosition + "px";
        setTimeout(AnimateLeft, animationSpeed);
    }
}

function MoveRight() {
    if (!moving) {
        acceleration = startAcceleration;
        var left = parseInt(contentDiv.style.left);
        nextPosition = left + contentWidth;
        if (nextPosition <= 0) {
            moving = true;
            AnimateRight();
        }
        else {
            nextPosition = (-1 * (contentDivWidth - contentWidth));
            moving = true;
            AnimateLeft();
        }
    }
    else {
        // Can't move because already moving
    }
}

function AnimateRight() {
    var left = parseInt(contentDiv.style.left);

    velocity += acceleration;
    acceleration += 2;
    if (velocity > maxVelocity) {
        velocity = maxVelocity;
    }

    var newPosition = left + velocity;

    if (newPosition > nextPosition) {
        velocity = 0;
        contentDiv.style.left = nextPosition + "px";
        moving = false;
    }
    else {
        contentDiv.style.left = newPosition + "px";
        setTimeout(AnimateRight, animationSpeed);
    }
}

function GetQueryIndex() {
    var query = location.search.substring(1);
    var keyValues = query.split("&");
    for (var i = 0; i < keyValues.length; i++) {
        var currentKeyValue = keyValues[i].split("=");
        if (currentKeyValue.length == 2) {
            if (currentKeyValue[0].toLowerCase() == "index") {
                try
                {
                    var index = parseInt(currentKeyValue[1]);
                    if(index < totalItems && index >= 0)
                    {
                        MoveTo(index);
                        break;
                    }
                }
                catch(e){}
            }
        }
    }
} 
