﻿function ScrollingImageSelector(parent, urls, selectedUrl, controlToUpdate)
{
    // Create the scrolling image list on the parent object
    var parentElement = parent,
                currentControl = this,
                imageIndex = 0,
                imageUrls = urls,
                childDivs = null,
                imgs = null,
                controlValueToSet = null;

    // Setup the control to update if it's there.
    if (typeof(controlToUpdate) !== 'undefined')
    {
        // Make a control out of it if it's a string
        if (typeof(controlToUpdate) === 'string')
        {
            var ctrl = document.getElementById(controlToUpdate);
            controlValueToSet = ctrl;
        }
        else
        {
            controlValueToSet = controlToUpdate;
        }
    }

    this.Unload = function Seansoft$ScrollingImageSelector$Unload()
    {
        parentElement.removeChild(childDivs[0].parentNode);
        childDivs = null;
        imageUrls = null;
        imgs = null;
        controlValueToSet = null;
        currentControl = null;
    };

    this.SelectedUrl = selectedUrl;

    this.SetupElements = function Seansoft$ScrollingImageSelector$SetupElements()
    {
        // Make the parent div and the three child divs, and the list of images here.
        var i = 0,
            img = null,
            btnUp = null,
            btnContainer = null,
            btnDown = null,
            pd = document.createElement("DIV");
        pd.className = "imgWrapper";

        btnUp = document.createElement("DIV");
        btnUp.className = "divButton divButtonUp";
        $(btnUp).click(function() { currentControl.MoveUp(); });
        btnUp.innerHTML = "&nbsp;";
        pd.appendChild(btnUp);

        btnContainer = document.createElement("DIV");
        btnContainer.className = "imgContainer";
        pd.appendChild(btnContainer);

        // Add the images
        for (i = 0; i < imageUrls.length; i++)
        {
            var imgUrl = imageUrls[i];
            img = document.createElement("img");
            img.className = "imgScroll";
            img.src = imgUrl;
            img.alt = "Selectable Image: " + i;
            btnContainer.appendChild(img);
            if (imgUrl === currentControl.SelectedUrl)
            {
                imageIndex = i;
            }
        }

        btnDown = document.createElement("DIV");
        btnDown.className = "divButton divButtonDown";
        // Bind the click event to our call
        $(btnDown).click(function() { currentControl.MoveDown(); });
        btnDown.innerHTML = "&nbsp;";
        pd.appendChild(btnDown);

        // Add us to the page
        parentElement.appendChild(pd);

        // And get the elements so we don't have to keep refetching them
        childDivs = pd.getElementsByTagName("div");
        imgs = childDivs[1].getElementsByTagName("img");
        currentControl.SetupScrollingPosition();
    };

    this.SetupScrollingPosition = function Seansoft$ScrollingImageSelector$SetupScrollingPosition()
    {
        // Setup Arrow Buttons
        if (imageIndex < 1)
        {
            childDivs[0].className += " divButtonUpDisabled";
        }

        if (imageIndex >= (imgs.length - 1))
        {
            childDivs[2].className += " divButtonDownDisabled";
        }

        // Setup Image Position and URL/Control Value
        childDivs[1].scrollTop = (imageIndex > 0 ? imageIndex * 83 : 0);
        currentControl.SelectedUrl = imgs[imageIndex].src;
        if (controlValueToSet !== null && controlValueToSet.value !== undefined)
        {
            controlValueToSet.value = currentControl.SelectedUrl;
        }
    };

    this.SetupCommonClassesAndReturnDivList = function Seansoft$ScrollingImageSelector$SetupCommonClassesAndReturnDivList()
    {
        childDivs[0].className = "divButton divButtonUp";
        childDivs[2].className = "divButton divButtonDown";
    };

    this.MoveDown = function Seansoft$ScrollingImageSelector$MoveDown()
    {
        currentControl.SetupCommonClassesAndReturnDivList();

        if (imageIndex < (imgs.length - 1))
        {
            imageIndex++;
        }

        currentControl.SetupScrollingPosition();
        return false;
    };

    this.MoveUp = function Seansoft$ScrollingImageSelector$MoveUp()
    {
        currentControl.SetupCommonClassesAndReturnDivList();

        if (imageIndex > 0)
        {
            imageIndex -= 1;
        }

        currentControl.SetupScrollingPosition();
        return false;
    };

    currentControl.SetupElements();
}

