var gSet = new Object();
gSet = 
    {    
    id      :"jsGalleryList",      
    imgArray: new Array()
    }

function gSetup(){

 	if(!document.createElement || !document.getElementsByTagName) return false; 
	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);
	
	new PeriodicalExecuter(function(){buildg("next",null)}, 5);
	
}
	
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,flag){

	//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.
	$('NewsCurrentPictureNum').innerHTML = imgNum+1;
	
	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);  
	}

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

	var backLink = document.getElementById('backLink'); 
	var nextLink = document.getElementById('nextLink');
	//create the control links
	//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,null,'1'); 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,null,'1'); return false;};

	gDisplay.appendChild(gSet.imgArray[imgNum]); //add in an image then the control links
	
	//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 = '<div class="gray small" style="padding: 3px 0 3px 0;">'+gSet.imgArray[imgNum].title+'</div>'; 
	gDisplay.appendChild(imgTitle);  //first element put into display div appears first.
	//}
	
	
	//gDisplay.appendChild(backLink);
	//gDisplay.appendChild(nextLink);
	nextSibling.parentNode.insertBefore(gDisplay,nextSibling); //put the gallery display back in the DOM
	
	if(go == "next" && flag=='1') nextLink.focus(); /*set focus for easy keyboard usability*/
	if(go == "back" && flag=='1') backLink.focus();
	
	if (flag=='1') window.location.hash =	"#" + gSet.imgArray[imgNum].title; //set the anchor.
	
}

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