//The starting image.
var current = randomNumber();

// image arrays are built in separate files so that i can SHARE generic code


var opacDelay = new Array();

//RE-USABLE FUNCTIONS/////////////////////////////////////
/////////////////////////////////////////////////////////////////

//Generic onload event handler.
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//A means of inserting an element AFTER an existing element
function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

//Returns a random number. 
function randomNumber() {
	upper_limit= 7; //The number of images (remember arrays begin with 0!)
	var ID;
	ID=Math.round(upper_limit * Math.random()); 
	return ID;
}

//STANDARD IMAGE GALLERY/////////////////////////////////
/////////////////////////////////////////////////////////////////

//Prepares the functionality of the gallery.
function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("hds_gallery")) return false;
  var gallery = document.getElementById("hds_gallery");
  var links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
	links[i].imageValue=i;
    links[i].onclick = function() {
		if(undefined != window.delay){
			clearTimeout(delay);
		}
		addImage(this.imageValue);
		return false;
	}
    links[i].onkeypress = links[i].onclick;
  }
}
addLoadEvent(prepareGallery);

//IMAGE GALLERY SLIDESHOW////////////////////////////////
/////////////////////////////////////////////////////////////////

function addImage(userSelection){
	if(undefined != userSelection){current = userSelection;}
	var imgContainer = document.getElementById("image_container"); //Get the container.
	
	if(imgContainer.childNodes.length > 1){ //If there is more than one element, remove the first.
		imgContainer.removeChild(imgContainer.firstChild);
	}
	var lastElement = imgContainer.lastChild;
	
	var newElement = document.createElement("img"); //Create a new image (Image B) and place it in front of the first element.
	newElement.id = "image_" + current;
	newElement.src = imageArray[current];
	newElement.alt = altArray[current];
	newElement.className = "placeholder invisible";
	insertAfter(newElement, lastElement);
	opacity(newElement.id, 0, 100, 1000);
	
	if(current==imageArray.length-1){current=0} //Start from the beginning when the last image is reached.
	else{current++};
	
	if(userSelection){delay = setTimeout("addImage()",8000);} //Create a longer delay if the user selects an image to view.
	else{delay = setTimeout("addImage()",4000);}
}
addLoadEvent(preloader);

function opacity(id, opacStart, opacEnd, millisec) {
	var speed = Math.round(millisec / 100);
    var timer = 0;

	for(i = opacStart; i <= opacEnd; i++) {
		opacDelay[i] = setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
		timer++;
	}
}

function changeOpac(opacity, id) {
   	if(!document.getElementById(id)){return false;} //A means of error-handling if the element has not been created.
	var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 