var gallery = (function(){ 
	/*
		the following elements with these ID's need to exist:
		next
		currentphoto - this is the large photo 
		prev
		gallery - needs to be an element that contains img elements (thumbnails)
		
		naming convention is : 
		images/photos/
			photo_thumb_#.jpg
			photo_#.jpg
			
		easy :P
	*/
	
	document.getElementById('next').onclick = function () { gallery.next(); }
	document.getElementById('currentphoto').onclick = function () { gallery.next(); }
	document.getElementById('prev').onclick = function () { gallery.prev(); }

	var thumbs = document.getElementById('gallery').getElementsByTagName('img');
	var photoids = [];
	var photoids_keys = [];
	for (var i = 0, len = thumbs.length; i < len; i++) {
		var key = photoids.length;
		var photoid = thumbs[i].getAttribute('src').match(/images\/photos\/photo_thumb_(\d+).jpg/i)[1];
		photoids_keys[photoid] = key;
		photoids[key] = thumbs[i].getAttribute('src').match(/images\/photos\/photo_thumb_(\d+).jpg/i)[1];
		thumbs[i].onclick = function () { 
			var photoid = this.getAttribute('src').match(/images\/photos\/photo_thumb_(\d+).jpg/i)[1];
			gallery.change(photoid);
		}
	}

	return {
		photo: document.getElementById('currentphoto'),
		photos: thumbs.length-1,
		photoids: photoids,
		photoids_keys: photoids_keys,
		current: 1,
		next: function () { 
			if (this.current == this.photos) {
				this.current = 0;
			} else {
				this.current++;
			}
			this.photo.setAttribute('src', 'images/photos/photo_' + this.photoids[this.current] + '.jpg');
		},
		prev: function () { 
			if (this.current == 0) {
				this.current = this.photos;
			} else {
				this.current--;
			}
			this.photo.setAttribute('src', 'images/photos/photo_' + this.photoids[this.current] + '.jpg');
		},
		change: function (photoid) {
			this.current = this.photoids_keys[photoid];
			this.photo.setAttribute('src', 'images/photos/photo_' + photoid + '.jpg');
		}
	}
	
})();
