if (typeof findPos == 'undefined') {
function findPos(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}
}

var Gallery = {
	zpDomain: 'http://www.justicemapping.org',
	zpPath: '/wp-content/zenphoto/',
	initialized: false,
	albumsQuery: false,
	currentAlbum: false,
	cloneLink: false,
	activeImage: false,
	currentPos: 0,
	scrollStop: false,
	Fx: new Object(),
	init: function() {
		if (this.initialized) return;
		this.initialized = true;

		//$('sidegalleryh5').style.display = 'block';
		var tmpcloneLink = document.createElement('a');
		tmpcloneLink.href = 'javascript:void(0);';
		tmpcloneLink.onclick = Gallery.scrollImage;
		tmpcloneLink.id = 'gallery-up';
		tmpcloneLink.title = 'Previous image';
		$('sidegallery-images').insertBefore(tmpcloneLink, $('sidegallery-images').childNodes[0]);

		this.cloneLink = tmpcloneLink.cloneNode(true);
		this.cloneLink.onclick = Gallery.scrollImage;
		this.cloneLink.id = 'gallery-down';
		this.cloneLink.title = 'Next image';

		var loadingImage = document.createElement('div');
		loadingImage.id = 'loading-icon';
		Gallery.Fx.loading = new Fx.Opacity(loadingImage, {onComplete: function () { if (Gallery.albumsQuery) Gallery.albumsQuery.request(); }});
		Gallery.Fx.loading.set(0);
		$('sidegallery-images').insertBefore(loadingImage, $('sidegallery-images').childNodes[0]);
		this.albumsQuery = new Ajax(this.zpDomain + this.zpPath, {method: 'get', onComplete: this.albums});
		Gallery.Fx.loading.custom(0, 0.5);
	},
	scrollImage: function() {
		var nextDur = 250;
		if (Gallery.scrollStop) return false;
		if (!Gallery.activeImage)
			Gallery.activeImage = $('album-images').childNodes ? $('album-images').childNodes[0] : false;
		if (Gallery.activeImage) {
			var nextImg;
			if (this.id == 'gallery-up' && Gallery.activeImage.previousSibling)
				nextImg = Gallery.activeImage.previousSibling;
			else if (this.id == 'gallery-up') {
				nextImg = Gallery.activeImage.parentNode.childNodes[(Gallery.activeImage.parentNode.childNodes.length-1)];
				nextDur = 500;
			} else if (Gallery.activeImage.nextSibling)
				nextImg = Gallery.activeImage.nextSibling;
			else {
				nextImg = Gallery.activeImage.parentNode.childNodes[0];
				nextDur = 500;
			}
			var albumScroll = new fx.Style($('album-images').childNodes[0], 'marginTop', {duration:nextDur, onStart: Gallery.scrollLock, onComplete: Gallery.scrollLock});
			if (nextImg && nextImg.nodeName.toUpperCase() == 'LI') {
				Gallery.activeImage = nextImg;
				var toPos = Gallery.currentPos - nextImg.offsetTop;
				albumScroll.clearTimer();
				albumScroll.custom(Gallery.currentPos, toPos);
				Gallery.currentPos = toPos;
			}
		}
	},
	scrollLock: function() {
		Gallery.scrollStop = Gallery.scrollStop ? false : true;
	},
	albumPageQuery: function() {
		if (Gallery.albumsQuery) return false;
		Gallery.Fx.loading.custom(0, 0.5);
		Gallery.albumsQuery = new Ajax(this.href, {method: 'get', onComplete: Gallery.albums});
		return false;
	},
	checkHeight: function() {
		var pgHeight  = $('content-left').scrollHeight - (findPos($('album-images-outer')) - findPos($('content-left')));
		var boxHeight = $('album-images').childNodes[0].scrollHeight;
		pgHeight -= (pgHeight % boxHeight) + 10;
		if (pgHeight > $('album-images-outer').offsetHeight)
			$('album-images-outer').style.height = pgHeight+'px';
	},
	albums: function(response) {
		var i, a;
		var s_g_i = $('sidegallery-images-inner');
		s_g_i.innerHTML = response;
		Gallery.checkHeight();
		s_g_i.appendChild(Gallery.cloneLink);
		Gallery.albumsQuery = false;
		checkLightboxLinks(s_g_i);
		if (s_g_i.childNodes[1] && s_g_i.childNodes[1].className == 'pagelist') {
			for (i = 0; (a = s_g_i.childNodes[1].getElementsByTagName("a")[i]); i++)
				a.onclick = Gallery.albumPageQuery;
		}
		Gallery.Fx.loading.custom(0.5, 0);
	}
};