// Functions for page's image gallery:

var counter=0;
// Array containing the gallery's images is loaded later...

// This function shows the next gallery image:
function next(img_arr) {
if (img_arr[counter+1]) {
counter++;
} else {
counter = 0;
}
document.getElementById('gallery').src=img_arr[counter];
pictureCount();
preload_prev_next(img_arr,counter);
}

// This function shows the previous gallery image:
function prev(img_arr) {
if (img_arr[counter-1]) {
counter--;
} else {
counter = img_arr.length - 1;
}
document.getElementById('gallery').src=img_arr[counter];
pictureCount();
preload_prev_next(img_arr,counter);
}

// This function sets the image counter on the webpage:
function pictureCount(){
	var add = 1
	if (imgs.length==0) add = 0
	document.getElementById('pictureCount').innerHTML = counter+add+'/'+imgs.length;
}

// This function loads the url page:
function openpage(url){
	var w = document.myform.mylist.selectedIndex;
	var url_add = document.myform.mylist.options[w].value;
	window.location.href = url_add;
	//document.myform.mylist.selectedIndex=2;
}

// This function takes an array of images'urls and caches those images:
function preload(image_url_array){
    var img_arr = new Array();
	for(i=0; i<=image_url_array.length-1; i++) {
		img_arr[i] = new Image(1,1); 
		img_arr[i].src=image_url_array[i];
	}
}

// This function takes an array of images, an index and caches the previous and the next image:
function preload_prev_next(img_arr,index){
	if (img_arr.length > 0){
		img_p = new Image(1,1);
		img_n = new Image(1,1); 

		temp_next = index+1;
		temp_prev = index-1;
		
		if (temp_next > img_arr.length-1) {
			temp_next = 0;
		}
		if (temp_prev < 0) {
			temp_prev = img_arr.length-1;
		}
		
		img_p.src=img_arr[temp_prev];
		img_n.src=img_arr[temp_next];
	}
}

// This function shows the first gallery image, if present:
function show_first_image(img_arr){
	if (img_arr[0]) {
		document.getElementById('gallery').src=img_arr[0];
	} 
}
