function initGallery(){
	// Check for DOM compliance
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;

	var gallery = document.getElementById("gallery");
	var thumbs = gallery.getElementsByTagName("a");
	var viewer = document.getElementById("viewer");
	var title = document.getElementById("image-title");
	var caption = document.getElementById("image-caption");
	
	// Load random image first
	var r = Math.floor(Math.random()*10);
	thumbs[r].className = "viewing";
	var imagesrc = thumbs[r].href;
	var newimage = thumbs[r].getElementsByTagName("img")[0];
	var newthumb = imagesrc.replace("full", "preview");
	newimage.setAttribute("src", newthumb);
	viewer.setAttribute("src", imagesrc);
	viewer.onclick = function(){
		window.open(imagesrc);
	}
	// Replace image title
	title.innerHTML = newimage.getAttribute("alt");
	// Replace image caption
	caption.innerHTML = newimage.getAttribute("title");
	
	for(var i=0;i<thumbs.length;i++){
		thumbs[i].onclick = function(){
			imagesrc = this.href;
			newimage = this.getElementsByTagName("img")[0];
			// Replace image source and make link to full-size
			viewer.setAttribute("src", imagesrc);
			viewer.onclick = function(){
				window.open(imagesrc);
			}
			// Replace image title
			title.innerHTML = newimage.getAttribute("alt");
			// Replace image caption
			caption.innerHTML = newimage.getAttribute("title");
			// Reset thumbs
			resetThumbs();
			// Replace thumbnail with larger thumbnail
			this.className = "viewing";
			var newthumb = imagesrc.replace("full", "preview");
			newimage.setAttribute("src", newthumb);
			this.blur();
			return false;				
		}
	}
}
window.onload = initGallery;

function resetThumbs(){
	var gallery = document.getElementById("gallery");
	var thumbs = gallery.getElementsByTagName("a");
	for(var i=0;i<thumbs.length;i++){
		thumbs[i].className = "";
		var thumbimg = thumbs[i].getElementsByTagName("img")[0];
		var thumbsrc = thumbimg.getAttribute("src");
		var resetthumb = thumbsrc.replace("preview", "thumb");
		thumbimg.setAttribute("src", resetthumb);
	}
	return true;
}