
var gSet = new Object();
gSet = 
    {
    // GALLERY SETTINGS 
    id      :"BilderListe",   // put the gallery list's ID here
    next    :"<div class=\"bildergalerieNext\" style=\"margin-top:5px;\"></div>",      // text for "next" link
    back    :"<div class=\"bildergaleriePrevious\" style=\"margin-top:5px;\"></div>",  // text for "previous" link
    //next    :"next",
	//back    :"prev",
	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 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);
}
	
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("einzelarbeitBild"); 	
	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 = "einzelarbeitBild";
	//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); return false;};
	var Numbers = document.createElement("div");
	Numbers.style.cssFloat = "left"; 
	Numbers.style.styleFloat = "left"; 
	//Numbers.style.width = "59px";	
	if (imgNum < 9){  //bis Bild 10 muss vor die erste Ziffer ein Leerzeichen
	   Numbers.innerHTML = "<div class=\"bildergalerieText\">Bild&nbsp;&nbsp;" + (imgNum+1) + "/" +  gSet.imgArray.length +"</div>";
	}
	else{
	   Numbers.innerHTML = "<div class=\"bildergalerieText\">Bild&nbsp;" + (imgNum+1) + "/" +  gSet.imgArray.length +"</div>";
	}

	
	var Divi = document.createElement("div");

	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;};

	/**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.
	}**/

    var br1 = document.createElement("br");


	gDisplay.appendChild(gSet.imgArray[imgNum]); //add in an image then the control links
	if (gSet.imgArray.length > 1) { // no imgage control, if there is just 1 image	
    		Divi.appendChild(backLink);
		Divi.appendChild(Numbers);
		Divi.appendChild(nextLink);	

		gDisplay.appendChild(Divi);
    		gDisplay.appendChild(br1);
    	}

	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();
	
	//window.location.hash =	"#" + gSet.imgArray[imgNum].title; //set the anchor.
	
}// End buildGallery


window.onload = function(){gSetup();}  //this needs replacing with something more usable.


