/*
    Images: class for maintaining Image objects
    Author: Theo M. Gerrits (tmg)
    
    Dependencies: Logger.js
    
    JavaScript1.5
    
    Copyright (C) 2005 Ontwerpfontein
*/


// constructor
function Images(doc)
{
    this.doc = doc;
    this.preloadedImages = new Array();
    this.swapImages = new Array();
}


// Description     : count the total number of preloaded images
// Parameters      : none
// Returns         : the number of preloaded images
Images.prototype.getNumberOfPreloadedImages = function ()
{
    var n = 0;
    for (var prop in imagesObject.preloadedImages)
        n++;
    return n;
}

// Description     : preload a list of images. All images will be preloaded and added to the preloadedImages Array.
// Parameters      : a list of image URLs
// Returns         : void
Images.prototype.preload = function ()
{
    with (this)
    {
        for (var i = 0; i < arguments.length; i++)
        {
            var img = new Image();
            img.src = arguments[i];
            // don't use preloadedImages.push(img) since this allows duplicates
            preloadedImages[arguments[i]] = img;
        }

        LOG.info("Total # of preloaded Images: " + getNumberOfPreloadedImages());
    }
}

// Description     : swap a series of images in the associated document to an alternative (e.g. rollover image).
// Parameters      : array of strings. The array consists of pairs of 2 strings:
//                   the first string contains the image name, the second the name (URL) of the image's replacement
// Returns         : void
// Remark          : Only 1 set of images may be swapped at 1 time, since there is only 1 swapImages Array
Images.prototype.swap = function ()
{
    with (this)
    {
        swapImages = new Array();
        for (var i = 0; i < arguments.length - 1; i += 2)
        {
            var img = doc.getElementById(arguments[i]);
            if (img)
            {
                // MAC### IE5.2 Array.push() not supported...
                // swapImages[swapImages.length] = img;
                swapImages.push(img);
                if (!img.originalUrl)
                {
                    img.originalUrl = img.src;
                    img.restore = _imagesRestore;
                }
                img.src = arguments[i + 1];
                LOG.debug("Swapped " + img.id + " to: " + img.src);
            }
        }
        
        LOG.debug("Swapped " + arguments.length/2 + " images");
    }
}

// Description     : restore all images in the swapImages Array to their original source URL.
// Parameters      : none
// Returns         : void
Images.prototype.restoreAll = function ()
{
    with (this)
    {
        for (var i = 0; i < swapImages.length; i++)
        {
            swapImages[i].src = swapImages[i].originalUrl;
            LOG.debug("Image " + swapImages[i].name + " restored to URL: " + swapImages[i].originalUrl);
        }

        LOG.debug("# of Images restored: " + swapImages.length);
    }
}


// Description     : restore a swapped image to its original source URL.
// Parameters      : none
// Returns         : void
// Remark          : do not use this as a global method
function _imagesRestore()
{
    this.src = this.originalUrl;
    LOG.debug("Image " + this.id + " restored");
}
