Array.prototype.indexOf= function(value) {
    var i = 0;
    var l = this.length;
    var res = -1;
    for (i; i<l; i++) {
        if (this[i] == value) {
            res = i;
            break;
        }
    }
    return res;
}

function sectionSelectChange(){
	var sectionSel = document.sectionForm.sectionSelect;
	setHash(sectionSel.options[sectionSel.selectedIndex].value);
	showPic();
}

function setSectionSelect(){
	var currentImageIndex;
	var currentSectionIndex;
	var nextSectionIndex;
	var i = 0;
	var sectionFlag = false;
	
	while(!sectionFlag){
		if (i+1 >= sectionPointerArray.length){
			currentSectionIndex = i;
			sectionFlag=true;
		}
		else {
			nextSectionIndex = picArray.indexOf(sectionPointerArray[i+1]);
			currentImageIndex = picArray.indexOf(getFileFromHash());
			if (currentImageIndex < nextSectionIndex){
				sectionFlag = true;
				currentSectionIndex = i;
			}
			else {
				i++;
			}
		}	
	}
	document.sectionForm.sectionSelect.selectedIndex = i;
}

function setHash(file){
	location.hash=file;
}

function getFileFromHash(){
	input = location.hash;
	if ((input.match(".jpg") != null) && (input != "")){
		var filename = input.substring(1);
	}
	else {
		var filename = picArray[0];
	}
	return filename;
}

function changePic(filename){
	tmpimage = new Image();
	tmpimage.src = "http://www.nebhead.com/images/site/photo-wait.gif";
	document.s_picture.src = tmpimage.src;
	document.s_picture.className = "slideshow_image_loading";
	newimage = new Image();
	newimage.src = filename;
	newimage.onload = function () {
		document.s_picture.className = "slideshow_image";
		document.s_picture.src = newimage.src;
	}
}

function showPic(){
	var filename = getFileFromHash();
	changePic(filename)
	var index = picArray.indexOf(filename);
	document.getElementById("s_header").innerHTML="Picture "+(index+1)+" of "+picArray.length;
	if (commentArray != null){
		document.getElementById("s_comment").innerHTML=commentArray[index];
	}
	document.getElementById("s_link").href=filename;
	setSectionSelect();
}

function doNext(){
	var currentI = getFileFromHash();
	var nextI = picArray.indexOf(currentI)+1;
	if (nextI >= picArray.length) {
		nextI = 0;
	}
	setHash(picArray[nextI]);
	showPic();
}

function doPrevious(){
	var currentI = getFileFromHash();
	var prevI = picArray.indexOf(currentI)-1;
	if (prevI == -1) {
		prevI = picArray.length -1;
	}
	setHash(picArray[prevI]);
	showPic();
}

showPic();
