function photoViewer(global_self_reference) {
	this.global_self_reference = global_self_reference;
	this.index = 0;
	this.cache_previous = new Image;
	this.cache_next = new Image;
	
	this.setDiv = function (div_id) {
		this.div = document.getElementById(div_id);
	}
	
	this.loadJson = function (json_file) {
		this.json = json_file;
		ajaxRequest('GET', this.json, '', this.global_self_reference+'.loadJsonResponse');			
	}
	
	this.loadJsonResponse = function (text) {
		this.photoset = JSON.parse(text);
		this.cachePrevNext();
		this.showPhoto();
	}
	
	this.init = function (div_id, json_file, first_image) {
		this.setDiv(div_id);
		this.loadJson(json_file);
	}
	
	this.cachePrevNext = function() {
		if ((this.index + 1) < this.photoset.length) {
			this.cache_next.src = this.photoset[this.index + 1].image_url;
		} else {
			this.cache_next.src = this.photoset[0].image_url;
		}
		
		if ((this.index - 1) >= 0) {
			this.cache_previous.src = this.photoset[this.index - 1].image_url;
		} else {
			this.cache_previous.src = this.photoset[this.photoset.length - 1].image_url;
		}
	}
	
	this.showPhoto = function() {
		var code = '';
		code = '<div>';
		
		code+= '<center><table><tr><td>';
		
		code+= '<a href="javascript:' + this.global_self_reference + '.showNext()">';
		if (this.photoset[this.index].caption) {
			code+= '<img class="photoviewer_large_photo" alt="' + this.photoset[this.index].caption + '" src="' + this.photoset[this.index].image_url + '" />';
		} else {
			code+= '<img class="photoviewer_large_photo" src="' + this.photoset[this.index].image_url + '" />';
		}
		code+= '</a>';
		
		code+= '</td></tr><tr><td>';
		
		if (this.photoset[this.index].caption) {
			code+= '<div class="photo_caption">';
			code+= '<a href="javascript:' + this.global_self_reference + '.showNext()">';
			code+= this.photoset[this.index].caption 
			code+= '</a></div>';
		}
		
		code+= '</td></tr><tr><td>';
		
		code+= '<table style="width:100%;"><tr>';
		code+= '<td> : : <a href="javascript:self.location=self.location;">back to page</a> : : </td>';
		code+= '<td style="text-align: right; padding: 1em;">';
		code+= ' : : <a href="javascript:' + this.global_self_reference + '.showPrevious()">prev</a>';
		code+= ' : : ';
		code+= '<a href="javascript:' + this.global_self_reference + '.showNext()">next</a> : : </td>';
		code+= '</tr></table>';
		
		code+= '</td></tr></table></center>';
		
		code+= '</div>';
		
		this.div.innerHTML = code;
	}
	
	this.showNext = function() {
		this.index++;
		if (this.index >= this.photoset.length) {
			this.index = 0;
		}
		this.showPhoto();
		
		if ((this.index + 1) < this.photoset.length) {
			this.cache_next.src = this.photoset[this.index + 1].image_url;
		} else {
			this.cache_next.src = this.photoset[0].image_url;
		}
	}
	
	this.showPrevious = function() {
		this.index--;
		if (this.index < 0) {
			this.index =  this.photoset.length-1;
		}
		this.showPhoto();
		
		if ((this.index - 1) >= 0) {
			this.cache_previous.src = this.photoset[this.index - 1].image_url;
		} else {
			this.cache_previous.src = this.photoset[this.photoset.length - 1].image_url;
		}
	}
}

photo_viewer = new photoViewer('photo_viewer');
