var idNum,outputLinks,numberOfPhotos,bSwitcher=1;
//Set the file and title name in the data array found in the gallery.js file
numberOfPhotos = data.length;
idNum=0;
function next(idNum) {
	idNum++;
	if (idNum > numberOfPhotos-1){idNum=0;}
	if (idNum < numberOfPhotos-1) {preLoad(idNum+1);} // Preload the next image is we're not at the end
	setTags(idNum);
}
function previous(idNum) {
	idNum--;
	if (idNum < 0){idNum = (numberOfPhotos-1);}
	if (idNum > 0){preLoad(idNum-1);} // Preload the next image is we're not at the beginning
	setTags(idNum);
}

function preLoad(PreLoadIdNum) {
	imgObject3 = document.getElementById('gallery_image_preload');
	imgObject3.src = data[PreLoadIdNum][2];
}

function setTags(idNum) {
	document.getElementById('titleTag').innerHTML=(data[idNum][1]);
	
	// Perform a fade, switch image tags first
	if (bSwitcher == 1) {
		imgObject1 = document.getElementById('gallery_image');
		imgObject2 = document.getElementById('gallery_image2');
		imgObject1.src = data[idNum][2];
		bSwitcher = 0;
	}
	else{
		imgObject1 = document.getElementById('gallery_image2');
		imgObject2 = document.getElementById('gallery_image');
		imgObject1.src = data[idNum][2];
		bSwitcher = 1;
	}
	for (var i=0;i<11;i++)
		setTimeout('setOpacity('+i+')',i*i*2);
	displayQuickLinks(idNum);
}

function displayQuickLinks(idNum) {
	//display quick links
	outputLinks = '';
	outputNext='';
	outputPrevious='';
	for ( i=0; i < numberOfPhotos ; i++){
		if (i == idNum){
			outputLinks += (" <a class='menu' href='#' onClick='showIdNoFade(" + i + ");' >" + "<b>" + i + "</b>" + "</a> ");						
		}
		else {
			outputLinks += (" <a class='menu' href='#' onClick='showIdNoFade(" + i + ");'  >" + i + "</a> ");
			}
	}
	outputNext +=("[<a href='#' class='menu' onclick='next("+idNum+")'>NEXT</a>]");
	outputPrevious += ("[<a href='#' class='menu' onclick='previous("+idNum+")'>PREV</a>]");
	document.getElementById('quickLinks').innerHTML=(outputLinks);
	document.getElementById('previous').innerHTML=(outputPrevious);
	document.getElementById('next').innerHTML=(outputNext);
}

function getParameters(){
	var getdata = [];
	var foundMatch = 0;
	if (location.search.length > 1){
		var ls = location.search.substring(1);
		var namevalue = ls.split("&");
		var actionParam = namevalue[0].split("=");
		idRef = actionParam[1];
		// Find the array row number which matches the passed reference ID
		for (var i = 0; i < numberOfPhotos; i++){
			if(data[i][0] == idRef){
				idNum = i;
				foundMatch=1;
				//alert(foundMatch);
			}		
		}	
	}
	if (foundMatch == 0){idNum=0;}
	showIdNoFade(idNum);
}

function showIdNoFade(idNum) {
	// Makes the new layer visible but transparent then fades in. Pops in the text. We don't both with a fade
	document.getElementById('titleTag').innerHTML=(data[idNum][1]);
	imgObject1 = document.getElementById('gallery_image');
	imgObject1.src = data[idNum][2];
	imgObject2 = document.getElementById('gallery_image2');
	setOpacity('10');
	displayQuickLinks(idNum);
}


function setOpacity(value){
	imgObject1.style.opacity = value/10;
	imgObject1.style.filter = 'alpha(opacity=' + value*10 + ')';
	imgObject2.style.opacity = (1-value/10);
	imgObject2.style.filter = 'alpha(opacity=' + (100-value*10) + ')';
}

