﻿// JScript File

// A gallery image represents a single img (image) tag. Images
// are created and stored in an array.
function galleryImage()
{
    this.imageIndex = -1; // No assigned image.
    this.image = new Image();
    this.image.style.position = "absolute";
    this.image.onmouseover = onMouseOverGalleryImage;
    this.image.onmouseout = onMouseOutGalleryImage;
    this.image.onclick = onClickGalleryImage;
}

// A list of thumbnails is passed. We then fill the gallery with images
// until there is no more room.
function initGallery(gallery)
{
    try
    {
        var divGallery = document.getElementById(gallery.gallery);

        // Add the gallery as a property of the gallery <div>
        divGallery.gallery = gallery;
        
        // Set up an array for the images.
        gallery.galleryImages = new Array();
        
        // If there are attached control images then add the handlers.
        if(gallery.imgUp!="")
        {
            var imgUp = document.getElementById(gallery.imgUp);
            imgUp.onmouseover = function(e) {gallery.scrollDistance = gallery.initialScrollDistance;};
            imgUp.onclick = function(e) {advanceGalleryByDistance(gallery,gallery.horizontal?gallery.width:gallery.height);};
        }

        if(gallery.imgDown!="")
        {
            var imgDown = document.getElementById(gallery.imgDown);
            imgDown.onmouseover = function(e) {gallery.scrollDistance = -gallery.initialScrollDistance;};
            imgDown.onclick = function(e) {advanceGalleryByDistance(gallery,gallery.horizontal?-gallery.width:-gallery.height);};
        }

        // Work out the number of images that can be displayed.
        // Simply we start at the first image with it's offset and work
        // until the next image would begin off the end of the gallery.
        var location = gallery.offset;
        var izImage = gallery.firstImage;
        var izGalleryImage = 0;

        var limit = gallery.horizontal?gallery.width:gallery.height;
        
        while(location <= limit)
        {
            setGalleryImage(gallery,izImage,location,izGalleryImage);
            location += ((gallery.horizontal?gallery.images[izImage].tnWidth:gallery.images[izImage].tnHeight) + gallery.spacing);
            izGalleryImage++;
            izImage++;
            
            if(izImage >= gallery.images.length) izImage = 0;
        } 
        
        // Start scrolling
        scrollStart(gallery);
        
        // Fill the cache with images
        cacheImages(gallery);
    }
    catch(err)
    {
       alert(err.message);
    }
}

function onMouseOverGalleryImage(e)
{
    try
    {
        var img;

        if(!e)
        {
            var e = window.event;
            img = e.toElement;
        }
        else
        {
            img = e.currentTarget || e.relatedTarget || e.toElement;
        }

        if(img)
        {
            var gallery = getGalleryFromImage(img);
            scrollStop(gallery);
            
            if(gallery.onHover != null)
            {
                gallery.onHover(gallery,img.imageIndex);
            }
        }
    }
    catch(err)
    {
        alert(err.message);
    }
}

function onMouseOutGalleryImage(e)
{
    try
    {
        var img;
        
        if(!e)
        {
            var e = window.event;
            img = e.fromElement;
        }
        else
        {
            img = e.currentTarget || e.relatedTarget || e.toElement;
        }

        if(img)
        {
            var gallery = getGalleryFromImage(img);
            scrollStart(gallery);
            
            window.status = "";
            
            if(gallery.onHoverStop != null)
            {
                gallery.onHoverStop(gallery);
            }
        }
    }
    catch(err)
    {
        alert(err.message);
    }
}   

function onClickGalleryImage(e)
{
    try
    {
        var img;

        if(!e)
        {
            var e = window.event;
            
            img = e.srcElement;
        }
        else
        {
            img = e.currentTarget || e.relatedTarget || e.toElement;
        }

        if(img)
        {
            var gallery = getGalleryFromImage(img);
            
            if(gallery.onClick != null)
            {
                gallery.onClick(gallery,img.imageIndex);
            }
        }
    }
    catch(err)
    {
        alert(err.message);
    }
}      

function getGalleryFromImage(image)
{
    return image.parentNode.gallery;
}

function scrollStop(gallery)
{
    gallery.stopRequests++;
    window.clearTimeout(gallery.timerHandle);
    gallery.timerHandle = -1;
}

function scrollStart(gallery)
{
    if(gallery.stopRequests > 1)
    {
        gallery.stopRequests--;
    }
    else
    {
        if(gallery.timerHandle == -1)
        {
            gallery.timerHandle = window.setTimeout(function() { advanceGallery(gallery); },gallery.scrollInterval);
        }
        gallery.stopRequests = 0;
    }
}

function advanceGallery(gallery)
{
    advanceGalleryByDistance(gallery,gallery.scrollDistance);
    gallery.timerHandle = window.setTimeout(function() { advanceGallery(gallery); },gallery.scrollInterval);
}

function advanceGalleryByDistance(gallery,distance)
{
    // Distance can be positive or negative, if negative then the scroller will
    // move backwards.
    gallery.offset -= distance;
    
    // Firstly check if the location of the first image means it is
    // no longer visible. If this is the case we need to shuffle
    // everything down one place.
    while(gallery.offset > 0)
    {
        gallery.firstImage--;
        
        if(gallery.firstImage < 0) gallery.firstImage = gallery.images.length - 1;
        
        gallery.offset -= ((gallery.horizontal?gallery.images[gallery.firstImage].tnWidth:gallery.images[gallery.firstImage].tnHeight) + gallery.spacing);
    }
    
    while(gallery.offset < (gallery.horizontal?-gallery.images[gallery.firstImage].tnWidth:-gallery.images[gallery.firstImage].tnHeight))
    {
        gallery.offset += ((gallery.horizontal?gallery.images[gallery.firstImage].tnWidth:gallery.images[gallery.firstImage].tnHeight) + gallery.spacing);
        gallery.firstImage++;
        
        if(gallery.firstImage >= gallery.images.length) gallery.firstImage = 0;
    }

    var location = gallery.offset;
    var izImage = gallery.firstImage;
    var izGalleryImage = 0;
    
    var limit = gallery.horizontal?gallery.width:gallery.height;

    while(location <= limit)
    {
        setGalleryImage(gallery,izImage,location,izGalleryImage);
        location += ((gallery.horizontal?gallery.images[izImage].tnWidth:gallery.images[izImage].tnHeight) + gallery.spacing);
        izGalleryImage++;
        izImage++;
        
        if(izImage >= gallery.images.length) izImage = 0;
    } 
    
    while(izGalleryImage < gallery.galleryImages.length)
    {
        hideGalleryImage(gallery,izGalleryImage);
        izGalleryImage++;
    }
}

function hideGalleryImage(gallery,izGalleryImage)
{
    gallery.galleryImages[izGalleryImage].image.style.display = 'none';
}

function setGalleryImage(gallery,izImage,offset,izGalleryImage)
{
    var gi;

    if(gallery.galleryImages.length < (izGalleryImage + 1))
    {
        gi = new galleryImage;
        gi.imageIndex = izImage;
        document.getElementById(gallery.gallery).appendChild(gi.image);
        gallery.galleryImages[izGalleryImage] = gi;
    }
    else
    {
        gi = gallery.galleryImages[izGalleryImage];
    }

    var imgSrc = gi.image.src;
    var tnSrc = gallery.images[izImage].tnPath;
    if(imgSrc == '' || imgSrc.substring(imgSrc.length - tnSrc.length) != tnSrc)
    {
        gi.image.src = tnSrc;
        gi.image.width = gallery.images[izImage].tnWidth;
        gi.image.height = gallery.images[izImage].tnHeight;
        gi.image.alt = gallery.images[izImage].caption;
        if(gallery.horizontal)
        {
            gi.image.style.top = Math.round((gallery.height - gallery.images[izImage].tnHeight)/2) + "px";
        }
        else
        {
            gi.image.style.left = Math.round((gallery.width - gallery.images[izImage].tnWidth)/2) + "px";
        }
        gi.image.imageIndex = izImage;
        gi.image.style.display = 'block';
    }
    if(gallery.horizontal)
    {
        gi.image.style.left = offset + "px";
    }
    else
    {
        gi.image.style.top = offset + "px";
    }
}

function cacheImages(gallery)
{
    var cacheIndex = 0;
    var image;
    gallery.cachedImages = new Array();
    
    if(gallery.cacheThumbnails)
    {
        for(var x in gallery.images)
        {
            image = new Image();
            image.src = gallery.images[x].tnPath;
            gallery.cachedImages[cacheIndex++] = image;
        }
    }
    
    if(gallery.cacheImages)
    {
        for(var x in gallery.images)
        {
            image = new Image();
            image.src = gallery.images[x].path;
            gallery.cachedImages[cacheIndex++] = image;
        }
    }
}