// File containing common image processing (selection/navigation) functions.

try
{
    var path_images = PathProductImages;    // common base path to product images
}
catch(ex)
{
    // not used in Gallery code
}

var img_cnt;                                // count of product images
var images_path = new Array();              // path/filenames of product images
var images = new Array();                   // container for loaded product images
var images_link = new Array();              // links to info for product images
var images_desc = new Array();              // description of product images
var the_image = 0;                          // index of currently selected image
var more_info = new Array(
    " - Click ",
    "to go to gallery!",
    "for more information!"
);


// Retrieve/return specified image.
//  - img = name/path of image to retrieve/return
function newImage(img)
{
    if (document.images)
    {
        rslt = new Image();
        rslt.src = img;
        return rslt;
    }
}

// Configure the image/link arrays.
function setImageList()
{
    img_cnt = ProductImages.length - 1;

    for (var i = 0; i < img_cnt; ++i)
    {
        images_path[i] = path_images + ProductImages[i];
        if (ProductLinks[i])
        {
            images_link[i] = ProductLinks[i];
        }
        else
        {
            images_link[i] = "";    // no clickable link
        }
        if (ProductDescription[i])
        {
            images_desc[i] = ProductDescription[i] + more_info[0];
            if (ProductLinks[i].indexOf("Gallery.htm") >= 0)
            {
                images_desc[i] += more_info[1];
            }
            else
            {
                images_desc[i] += more_info[2];
            }
        }
        else
        {
            images_desc[i] = "";
        }
    }
}

// Preload the images for ready accessibility.
function preloadImages()
{
    if (document.images)
    {
        for (var i = 0; i < img_cnt; ++i)
        {
            images[i] = newImage(images_path[i]);
        }
    }
}

// Configure and show the Special Items.
function setSpecialItemsList()
{
    var cnt = SpecialItemsLinks.length - 1;

    if (SpecialItemsLinks[0])
    {
        document.write('<div class="specialdiv">');
    }
    for (var i = 0; i < cnt; ++i)
    {
        if (SpecialItemsLinks[i])
        {
            document.write('<a href="' + SpecialItemsLinks[i] + '" class="specialhref">');
            if (SpecialItemsImages[i])
            {
                document.write('<img src="' + PathSpecialItemsImages + SpecialItemsImages[i] + '" ');
                document.write('class="specialimg" ');
                if (SpecialItemsDescription[i])
                {
                    document.write('title="' + SpecialItemsDescription[i] + '">');
                    document.write('<br>' + SpecialItemsDescription[i]);
                }
                else
                {
                    document.write('>');
                }
            }
            document.write('</a>');
        }
    }
    if (SpecialItemsLinks[0])
    {
        document.write('</div>');
    }
}

// Show specified image.
//  - img = index of image to show (0-based)
//  - caption = optional caption string
//     - if empty string, use image name
//     - if no parameter, ignore caption processing
// stops and resets next image timer
function ChangeImage(img, caption)
{
    var image = document.getElementById("Img1");
    var bkgimage = document.getElementById("Img1Bgnd");

    image.src = images_path[img];       // change the main image
    bkgimage.src = images_path[img];    // put image in background also
    the_image = img;                    // save index to selected image
    clearTimeout(playing);              // stop next image timer
    PlaySlide();                        // reset next image timer

/*  show image caption - primarily for debugging
    if (arguments.length > 1)
    {
        var captionSpan = document.getElementById("captionspan");
        if (captionSpan == null)
        {
            alert("Need to uncomment ID 'captionspan' in HTML.");
        }
        else
        {
            if (caption != '')
            {
                captionSpan.innerHTML = caption;
            }
            else       // use file name
            {
                caption = images_path[img].substring(images_path[img].indexOf(path_images) + path_images.length);
                caption = caption.substring(0, caption.length - ".jpg".length);
                captionSpan.innerHTML = caption;
            }
        }
    }
*/
}

// Select next image.
function NextImage()
{
    the_image = GetNextImage();
    ChangeImage(the_image);             // select next image
}

// Retrieve next image index.
function GetNextImage()
{
    var new_image;

    // next image in sequence
    if (the_image == (img_cnt - 1))     // last image
    {
        return 0;                       // select first image
    }
    else
    {
        return the_image + 1;           // select next image
    }
}

// Select previous image.
function PreviousImage()
{
    the_image = GetPreviousImage();
    ChangeImage(the_image);             // select previous image
}

// Retrieve previous image index.
function GetPreviousImage()
{
    var new_image;

    // previous image in sequence
    if (the_image == 0)                 // first image
    {
        return img_cnt - 1;             // select last image
    }
    else
    {
        return the_image - 1;           // select previous image
    }
}

