// Photo Gallery Load script.
// With image cross fade effect for those browsers that support it.
// Script copyright (C) 2004-2008 http://www.cryer.co.uk/.
// Script is free to use provided this copyright header is included.

// This switches the main photo in the galleries from click on thumbnails

var currentGalleryLink = new Array();
// these are our constants
var req, image, warning, imagepath;

function d(o)
{
    return document.getElementById(o);
}

function LoadPicture(pictureName,_imagepath,captionID,captionText)
{
	// hide the image object
    image = d("photo_main");
    image.src = 'grafix/spinner.gif';
	

    // set our imagepath constant
    imagepath = _imagepath;

    // display a loading message
    warning = d("warning");
    warning.innerHTML = "";

    // our XMLHttpRequest to grab the image
    req = getreq();
    req.onreadystatechange = imagexists;
    req.open("get", imagepath, true);
    req.send(null);


	document.getElementById(captionID).innerHTML=captionText;
	// Which link is currently selected?
	for (i=0; i < document.links.length;i++)
	{
	   	var l=document.links[i];
	   	var n=l.getAttributeNode('onclick');
		if (n)
		{
			var onclick = n.value;
	   		if ((onclick) &&
	   			(onclick.indexOf(pictureName) > 0) &&
	   			(onclick.indexOf(_imagepath) > 0))
	   		{
			    currentGalleryLink[pictureName] = i;
			    break;
			}
		}
	}
}
function LoadPrev(pictureName)
{
	var current = currentGalleryLink[pictureName];
	if (current == null) current = document.links.length+1;
	var alt = null;
	for (i=document.links.length-1;i>=0;i--)
	{
		var link=document.links[i];
		var node = link.getAttributeNode('onclick');
		if (node)
		{
			var onclick = node.nodeValue;
			if ((onclick) &&
				(onclick.indexOf('LoadPicture') >= 0) &&
				(onclick.indexOf(pictureName) > 0))
			{
				if (i < current)
				{
					eval(onclick);
					return;
				} else if (alt == null)
					alt = onclick;
			}
		}
	}
	if (alt) eval(alt);
}
function LoadNext(pictureName)
{
	var current = currentGalleryLink[pictureName];
	if (current == null) {
		currentGalleryLink[pictureName]=-1;
		LoadNext(pictureName);
		current = currentGalleryLink[pictureName];
	}
	var alt = null;
	for (i=0;i<document.links.length;i++) {
		var link = document.links[i];
		var node = link.getAttributeNode('onclick');
		if (node)
		{
			var onclick = node.value;
			if ((onclick) &&
				(onclick.indexOf('LoadPicture') >= 0) &&
				(onclick.indexOf(pictureName) > 0))
			{
				if (i > current)
				{
					eval(onclick);
					return;
				} else if (alt == null)
					alt = onclick;
			}
		}
	}
	if (alt) {
		eval(alt);
	}
}

function imagexists()
{
    // if the XMLHttpRequest is finished loading
    if(req.readyState == 4)
    {
        // and the file actually exists
        if(req.status == 200)
        {
            // say it exists and show the image
            warning.innerHTML = "";
			image.src = imagepath;
            image.style.display = "float";
            
        }
        else
        {
            // say it doesn't exist and hide the image
            warning.innerHTML = "Image does not exist";
            image.style.display = "none";
        }
    }
}

function getreq()
{
    if(window.XMLHttpRequest)
        return new XMLHttpRequest();
    else if(window.ActiveXObject)
        return new ActiveXObject("Microsoft.XMLHTTP");
}


