Event.observe(window, 'load', function() { 
	sidebarHeight = $('sidebar').getHeight();
	leftContHeight = $('left').getHeight();
	
	topPadding = (leftContHeight - sidebarHeight);
	
	$('sidebar').setStyle({
	  top: topPadding + 'px',
	  position: 'relative'
	});
	
	
	if ($('photo') != null) {
		height = $('photo').getHeight();
		paddingTop = 404 - height;
	
		$('photo').setStyle({
			marginTop: (15 + paddingTop) + 'px',
			opacity: 1.0
		});
	}
	
	
	
	if ($('gallery') != null) {
		LoadMin(last, lastImage);
		
		photos = $$('.galleryphoto');
		
		$('overall').update(photos.length);
		
		$('navi-next').observe('click', function(event){
			if (page == photos.length) {
				index = 0;
			}
			else {
				index = page;
			}
			
			src = ReadRel(photos[index].id).n;
			id = ReadRel(photos[index].id).i;
			
			
			if ((page + 1) > photos.length) {
				page = 1;
			}
			else {
				page++;
			}
			ShowPhoto(src, id);
			
		});
	
		$('navi-prev').observe('click', function(event){
			if (page == 1) {
				index = (photos.length - 1);
			}
			else {
				index = (page - 2);
			}
				
			src = ReadRel(photos[index].id).n;
			id = ReadRel(photos[index].id).i;
			
			if (page > 1 && page <= photos.length) {
				page--;
			}
			else {
				page = photos.length;
			}
			ShowPhoto(src, id);
		});
	}
});

function SetPage() {
	$('annual').update(page);
}

function ShowPhoto(src, id) {
	if (id != last) {
		
		$('photo').setStyle({ opacity: 0.5 });
		
		var image = new Image();
		image.src = ReadRel('min' + id).d + 'is/638_404/' + src;
		image.onload = function() {
			$('photo').src = image.src;
			
			paddingTop = 404 - image.height;
			
			$('photo').setStyle({
				marginTop: (15 + paddingTop) + 'px',
				opacity: 1.0,
				width: image.width + 'px',
				height: image.height + 'px'
			});
			
			page = ReadRel('min' + id).x;
			
			LoadMin(id, src);
			LoadMinGs(last, lastImage);
			
			last = id;
			lastImage = src;
		}
		
		
		//$('photo').src = ReadRel('min' + id).d + 'is/638_404/' + src;
		
	}
}

function LoadMinGs(id, name) {
	$('min' + id).src = ReadRel('min' + id).d + 'crop/gs_70_70/' + name;
}

function LoadMin(id, name) {
	
	$('gallery-title').update($('min' + id).title);
	$('min' + id).src = ReadRel('min' + id).d + 'crop/70_70/' + name;
	SetPage();
}

function ReadRel(id) {
	return $(id).readAttribute('rel').evalJSON();
}
