var expectedHash;

function checkIn() {
	if (expectedHash != window.location.hash) {
		expectedHash = window.location.hash;
		parseLocation();
	}
}

function findIndex ( arry,strng ) {
 for( var i = 0; i<arry.length; i++ ) {
  if( arry[i][0] == strng ) { return i; }
 }
 return -1;
}

function flipper(ref) { 

		if (ref.className=='selected') { return; }


		var item = ref.src.split("t_")[1].split(".")[0];
		var filing = item + ".jpg";
		var txt = gImages[findIndex(gImages,item)][1];
		var bigPic = document.getElementById('bigpic');

		clearSelected();
		ref.className='selected';
		bigPic.style.visibility="hidden";
		bigPic.style.height="250px";
		dynamicContent('caption',txt);
		window.location.hash='l_'+item;
		expectedHash = window.location.hash;
		bigPic.src=dir+filing;

	}

function resize(ref) {
		ref.style.height="auto";
		ref.style.visibility="visible";
	}

function startUp() {
	
 
	// Watches for back button
	// window.setInterval(checkIn,1000);

	// Populates gallery from .js that defines gImages
	if (document.getElementById && !document.all){

		rnge = document.createRange();
		gall = document.getElementById('gallery');
		rnge.setStartBefore(gall);

		for (i=0; i<gImages.length; i++) {
			var a = gImages[i][0]; // Base filename
			var b = gImages[i][1]; // Caption
			var c;
			b = b.replace("<em>","");
			b = b.replace("</em>","");
		
			c = '<a><img alt="Thumbnail" src="' + dir + 't_' + a + '.jpg" id="' + a + '" onClick=flipper(this); title="' + b + '" style="visibility: hidden;" onLoad="this.style.visibility=\'visible\'; document.getElementById(\'gallery\').style.background=\'white\'"></a>' + '\n\t\t\t';
		
			frgmt = rnge.createContextualFragment(c);
			gall.appendChild(frgmt);
     		}

	}
	else if ( document.all ) {
		
		var c = "";	
		for (i=0; i<(gImages.length - 1); i++) {
			var a = gImages[i][0]; // Base filename
			var b = gImages[i][1]; // Caption
	
			b = b.replace("<em>","");
			b = b.replace("</em>","");
		
			c += '<a><img alt="Thumbnail" src="' + dir + 't_' + a + '.jpg" id="' + a + '" onClick=flipper(this); title="' + b + '" style="visibility: hidden;" onLoad="this.style.visibility=\'visible\'; document.getElementById(\'gallery\').style.background=\'white\'"></a>' + '\n\t\t\t';
		}
		
		document.getElementById('gallery').innerHTML = c;

	}	

	parseLocation();



}

function parseLocation() {
	
	if (window.location.hash != "") {
		
		var item = window.location.hash.split('l_')[1];
		var filing = item + ".jpg";
		
		expectedHash = window.location.hash;
		
		dynamicContent('caption',gImages[findIndex(gImages,item)][1]);
		clearSelected();
		document.getElementById(item).className='selected'; 
		document.getElementById('bigpic').style.visibility="hidden";
		document.getElementById('bigpic').style.height="250px";
		document.getElementById('bigpic').src=dir+filing;
		
	}
}

function clearSelected () {
	var allImgs=document.getElementById('gallery').getElementsByTagName('img'); 
	for (i=0; i<allImgs.length; i++) { 
 		allImgs[i].className='';
 	}
}



