//init on page load
Event.observe(window, 'load', initLightbox2PlusLinks, false);

function initLightbox2PlusLinks(e){

		var images=document.images;
		for(i=0;i<document.images.length;i++){		

		//register event handler that updates the lightbox
		Event.observe(images[i], 'click', createLinkElements, false);
		}		
}

function createLinkElements(){

	//create anchor elements
	if($('stimuli_bottomLinks') == null){
	var objButtomLinks = document.createElement("div");
	objButtomLinks.setAttribute('id','stimuli_bottomLinks');		

	var objBottomHighresLink = document.createElement("a");
	objBottomHighresLink.setAttribute('id','stimuli_bottomHighres');
	objBottomHighresLink.setAttribute('target', '_self');
	objBottomHighresLink.innerHTML = 'HIGHRES';
	objButtomLinks.appendChild(objBottomHighresLink);
	
	var objBottomLowresLink = document.createElement("a");
	objBottomLowresLink.setAttribute('id','stimuli_bottomLowres');
	objBottomLowresLink.setAttribute('target', '_self');
	objBottomLowresLink.innerHTML = 'LOWRES';
	objButtomLinks.appendChild(objBottomLowresLink);	

	//add anchors to lightbox Image Data
	var img_data = $('stimuli_imageData');
	img_data.appendChild(objButtomLinks);	
	
	//register event handler for updating the links
	Event.observe($('stimuli_lightboxImage'), 'load', handleLightboxClick, false);
	}
		
}

function handleLightboxClick(e){
	//find the current image href
	var img_href = e.findElement('img').src;
	
	//set href
	var highres_href = img_href.replace("normal","highres");
	$('stimuli_bottomHighres').setAttribute('href', highres_href);
	var lowres_href = img_href.replace("normal","lowres");
	$('stimuli_bottomLowres').setAttribute('href', lowres_href);
}

