/**
 * lightweight gallery viewer.  There are some other great pure Javascript galleries but they 
 * are all too heavy for use over dial-up.  This was designed to be a minimal implementation.
 *
 * Copyright (c) 2008 - Renee Burkett
 */
function Gallery(name) 
{
    this.name = name;
    this.displayTime = 5000;
    this.started = false;

    // reference to gallery frame
    this.frame = document.getElementById(name);      
   
    // setup picture indexes
    this.currPicIndex = -1;
    this.nextPicIndex = -1;
    
    this.pictures = new Array();
	this.transition = new Transition(5);
}

Gallery.prototype.start = function ()
{
    if (this.started) return;

    for (var i=0;i<this.frame.childNodes.length;i++)
    {
        var obj = this.frame.childNodes[i];
        if (obj.className == "galleryImage")
        {
            var pic = new Picture(obj);			
			var frameWidth = this.frame.clientWidth;
			var frameHeight = this.frame.clientHeight;
			
            obj.style.top = Math.round((frameHeight - obj.height) / 2) + "px";
            obj.style.left = Math.round((frameWidth - obj.width) / 2) + "px";
			pic.fullFrame = (obj.width >= frameWidth) && (obj.height >= frameHeight);
			
			this.pictures[this.pictures.length] = pic;
        }
    }

    if (this.pictures.length > 0)
    {
        this.nextPicIndex = 0;
        this.started = true;
        this.next();

        var thisObj = this;
        setInterval(function() { thisObj.next(); }, this.displayTime);      
    }
}

Gallery.prototype.next = function ()
{   
    var currPic = (this.currPicIndex > -1) ? this.pictures[this.currPicIndex] : null;
    var nextPic = this.pictures[this.nextPicIndex];
    
    this.transition.fade(currPic, nextPic);    
    this.currPicIndex = this.nextPicIndex;
    this.nextPicIndex = (this.nextPicIndex + 1) % this.pictures.length;  
}

function Picture(img)
{
    this.img = img;
	this.fullFrame = null;
}

function Transition(step)
{
    this.step = step;
}

Transition.prototype.fade = function (currPic, nextPic, opacityIndex)
{ 
    if (!currPic)
    {        
        this.setOpacity(nextPic.img, 100);
		nextPic.img.style.zIndex = 100;
    }
    else
    {
		if (!opacityIndex)
		{
			opacityIndex = 1;
			currPic.img.style.zIndex = 1;
			nextPic.img.style.zIndex = 100;
		}
	
        this.setOpacity(nextPic.img, opacityIndex);
        if (!nextPic.fullFrame) this.setOpacity(currPic.img, (100 - opacityIndex));
        
        if (opacityIndex < 100)
        {
			opacityIndex += this.step;
			
        	var thisObj = this;
            setTimeout(function() { thisObj.fade(currPic, nextPic, opacityIndex); }, 40);
        }
		else
		{
			this.setOpacity(currPic.img, 0);
		}
    }
}

Transition.prototype.setOpacity = function (img, opIdx)
{
    img.style.opacity = opIdx * 0.01;
    img.style.MozOpacity = opIdx * 0.01;
    img.style.filter = "alpha(opacity=" + opIdx + ")";
}
