
var gStartet = false;
var gSet = new Object();
gSet = 
    {
    // GALLERY SETTINGS 
    id      :"AGalleryList",   // put the gallery list's ID here
    next    :" >",      // text for "next" link
    back    :"< ",  // text for "previous" link
    linkTitleText:"Show image:",// text for title tag of links (is followed by image title)
    titles  :false,              // whether or not to use titles (true or false)
    titleTag:"h2",              // the HTML tag to be used for the displayed title
    // END SETTINGS
    imgArray: new Array()
    }

function init()
{
	gSetup();
}

function gSetup(){

 	if(!document.createElement || !document.getElementsByTagName || gStartet) return false; 
	gStartet = true;
	var gList = document.getElementById(gSet.id); 
	if(!gList ) return false; 
	var gListItems = gList.getElementsByTagName('img'); //get the images into an array
	var nextSibling = gList.nextSibling;
	
	for(var i=0; i<gListItems.length; i++)
	{
		gSet.imgArray.push(gListItems[i]);
	}
		
	gList.parentNode.removeChild(gList); //remove the list of images;
	var startImg  = testHash();
	buildg(null,nextSibling,startImg);
}
	
function testHash(){ //test if a specific image is being linked to
	for(var i=0; i<gSet.imgArray.length; i++)
		if("#" + gSet.imgArray[i].title == window.location.hash)
			return i;
}
	
	
function buildg(go,nextSibling,startImg){

	//Decide id we're going forwards or backwards, or restarting.
	if(go == "next") imgNum = imgNum+1; 	
	if(go == "back") imgNum = imgNum-1;	
	if(!go || imgNum == gSet.imgArray.length) imgNum = 0; //if we're starting for the first time or gone off the top.	
	if(startImg) imgNum = startImg;
	if(imgNum<0) imgNum = gSet.imgArray.length-1 // if we go backwards off the bottom.
	
	function plus(num)
	{
		if(num == gSet.imgArray.length-1) return 0;
		else num = num+1;
		return num;
	}
	
	function minus(num)
	{
		if(num == 0) return gSet.imgArray.length-1;
		else return num-1;
	}

	var gDisplay = document.getElementById("gDisplay"); 	
	if(gDisplay) //find next sibling and destroy current g
	{
		var nextSibling = gDisplay.nextSibling;
		gDisplay.parentNode.removeChild(gDisplay);  
	}
	
	//make image objects (for getting the width of the image in Internet Explorer)
	var image = new Image();
	var nextImage = new Image();
	image.src = gSet.imgArray[imgNum].src;
	nextImage.src = gSet.imgArray[ (go == "back")?minus(imgNum):plus(imgNum) ].src;

	var gDisplay = document.createElement("div"); //create a containing div for the g display
	gDisplay.id = "gDisplay";

	//create the control links
	var controlDiv = document.createElement("div");
	controlDiv.id = "controlDiv";
	if(image.width != 0)
		controlDiv.style.width = image.width + "px";
	
	var backLink = document.createElement("a"); 
	backLink.id = "backLink";
	backLink.innerHTML = gSet.back;
	backLink.href = "#" + gSet.imgArray[minus(imgNum)].title;
	backLink.onclick = function(){buildg("back",null); return false;};
			
	var nextLink = document.createElement("a"); 
	nextLink.id = "nextLink";
	nextLink.href = "#" + gSet.imgArray[plus(imgNum)].title;
	nextLink.innerHTML = gSet.next;
	nextLink.onclick = function(){buildg("next",null); return false;};
	
	var imgLink = document.createElement("a"); 
	imgLink.id = "imgLink";
	imgLink.href = "#" + gSet.imgArray[plus(imgNum)].title;
	imgLink.onclick = function(){buildg("next",null); return false;};

	if(gSet.titles) //create and insert title stuff.
	{
		backLink.title = gSet.linkTitleText + " " + gSet.imgArray[minus(imgNum)].title;
		nextLink.title = gSet.linkTitleText + " " + gSet.imgArray[plus(imgNum)].title;
		
		var imgTitle = document.createElement(gSet.titleTag);
		imgTitle.innerHTML = gSet.imgArray[imgNum].title; 
		gDisplay.appendChild(imgTitle);  //first element put into display div appears first.
	}
	if(image.width != 0)
		gDisplay.style.width = image.width + "px";
		
	gDisplay.appendChild(imgLink);
	imgLink.appendChild(gSet.imgArray[imgNum]); //add in an image then the control links
	imgLink.firstChild.onload=function(){imgLoaded(this);};
	gDisplay.appendChild(controlDiv);
	controlDiv.appendChild(backLink);
	controlDiv.appendChild(nextLink);
	nextSibling.parentNode.insertBefore(gDisplay,nextSibling); //put the gallery display back in the DOM
	
	if(go == "next")
	{
		nextLink.focus(); /*set focus for easy keyboard usability*/
	}
	if(go == "back")
	{
		backLink.focus();
	}
	
	if(go!=null) window.location.hash =	"#" + gSet.imgArray[imgNum].alt; //set the anchor.
	
}// End buildGallery

function imgLoaded(that)
{
	if(that.width!=0)
	{
		that.parentNode.parentNode.style.width = that.width + "px";
		that.parentNode.nextSibling.style.width = that.width + "px";
	}
}

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", gSetup, false);
}

window.onload = function(){init();}  //this needs replacing with something more usable.

