
var ImagesToCache = [];	// an array of all the A elements in the gallery_thumbs element.  
									// This is used to get the paths, description, and thumbnails of images
var ImageLoading = 0;				// the image currently being downloaded
var ImageObject = null;				// the image object used to do the asynchronous downloading
var CurrentImage = null;
var oldThumbSize = 'minimised';
var popup   = 0;

var NEXT = 1;
var PREVIOUS = -1;

// finds the next/previous sibling ELEMENT, skips #text nodes.
function NextNode(Node, Direction, WithWrap)
{
    var parent = Node.parentNode;
    do
    {
            Node = Direction == NEXT ? Node.nextSibling : Node.previousSibling;
            if (!Node && WithWrap)
            {
                    Node = Direction == NEXT ? parent.firstChild : parent.lastChild;
            }
    }
    while (Node && Node.nodeType != 1)
    return Node;
}

function SetCurrentImage(set)
{
    if (CurrentImage)
    {
        var previous = NextNode(CurrentImage.parentNode, PREVIOUS);
        if (previous)
        {
            previous.className = set ? 'previous' : '';
        }
        CurrentImage.parentNode.className = set ? 'current' : '';
        var next = NextNode(CurrentImage.parentNode, NEXT);
        if (next)
        {
            next.className = set ? 'next' : '';
        }
    }
}

function SelectImage(img)
{
    SetCurrentImage(false);
    CurrentImage = img;
    
    if (popup)
    {
        popup.parentNode.removeChild(popup);
        popup = 0;
    }
    else
    {
        document.getElementById('gallery_sidebar').className = 'openned';
    }

    SetCurrentImage(true);

    var title = CurrentImage.childNodes[0].alt;
    var src   = CurrentImage.href;
    popup = document.createElement('div');
    popup.className = 'gallery_viewer';
    popup.innerHTML = "<div class='gallery_viewer_header'><h2>" + title + "</h2>" +
                           "  <a href='?thumbs=" + oldThumbSize + "' title='Click to close' class='button close'> </a>" + 
                           "</div>" + 
                           "<img src='" + src + "' alt='" + title + "' />";

    var buttons = popup.getElementsByTagName('a');
    buttons[0].onclick = UnselectImage;    
    document.getElementById('gallery_sidebar').appendChild(popup);
    return false;
}

function UnselectImage()
{
    SetCurrentImage(false);
    document.getElementById('gallery_sidebar').className             =       oldThumbSize;
    if (popup)
    {
        popup.parentNode.removeChild(popup);
        popup = 0;
    }
    return false;
}

var gallery_buttons  = 
{
    tagName : 'a',
    parentElement : 'gallery_sidebar_header',
    
    init : function (element)
    {
        element.onclick = function (e)
        {
            e = e || window.event;
            var target = e.target || e.srcElement;
            document.getElementById('gallery_sidebar').className = oldThumbSize = target.className + 'd';
            return false;    
        }
    }
};

var gallery_thumbs = 
{
    tagName : 'a',
    parentElement : 'gallery_thumbs',

    init : function(element)
    {
        element.onclick = function() { return SelectImage(element); };
        element.title = element.childNodes[0].alt;
        ImagesToCache[ImagesToCache.length] = element;
    }

};

install(gallery_buttons);
install(gallery_thumbs);
/*domOnLoad(CacheNextImage);
	
function CheckDownloadStatus()
{
	if (ImageObject.complete) 
	{
		//set_opacity(ImagesToCache[ImageLoading].firstChild, 1.0);
		++ImageLoading;
		CacheNextImage();
	}
	else 
	{
		setTimeout("CheckDownloadStatus()", 100);
	}
	return true;
}

function CacheNextImage() 
{
	if (ImageLoading < ImagesToCache.length)
	{
		ImageObject = new Image();
		ImageObject.src = ImagesToCache[ImageLoading].href;
		CheckDownloadStatus();
	}
}*/
