// *******************************************
// THREE FUNCTIONS TO CREATE THE IMAGE POP-UPS
// *******************************************

// FUNCTION 1: POPLP
// poplp = "popleftportrait" i.e. a box for portrait format 
// images that appears to the left of the "larger image" link. 
// This is for images on the right-hand side of the content area. 
poplp={
	popContainer:null,
	triggerClass:'popleftport',
	openPopupLinkClass:'popuplink',
	popupClass:'popup',
	displayPrefix:'Close ',
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
		var allLinks=document.getElementsByTagName('a');
		for(var i=0;i<allLinks.length;i++){
			if(!DOMhelp.cssjs('check',allLinks[i],poplp.triggerClass)){continue;}
			DOMhelp.addEvent(allLinks[i],'click',poplp.openPopup,false);			
			allLinks[i].onclick=DOMhelp.safariClickFix;
			allLinks[i].preset=allLinks[i].innerHTML;
		}
	},
	openPopup:function(e){
		var t=DOMhelp.getTarget(e);
		if(t.nodeName.toLowerCase()!='a'){
			t=t.parentNode;	
		}
		if(!poplp.popContainer){
			t.innerHTML=poplp.displayPrefix+'image';
			poplp.popContainer=document.createElement('div');
			DOMhelp.cssjs('add',poplp.popContainer,poplp.popupClass);
			DOMhelp.cssjs('add',t,poplp.openPopupLinkClass);
			var newimg=document.createElement('img');
			newimg.setAttribute('src',t.getAttribute('href'));
			poplp.popContainer.appendChild(newimg);
			document.body.appendChild(poplp.popContainer);
			poplp.positionPopup(t);
			poplp.popContainer.onclick = function() {
						poplp.killPopup();
						t.innerHTML=t.preset;
						DOMhelp.cssjs('remove',t,poplp.openPopupLinkClass);
						}
		} else {
			poplp.killPopup();
			t.innerHTML=t.preset;
			DOMhelp.cssjs('remove',t,poplp.openPopupLinkClass);
		}
		DOMhelp.cancelClick(e);
	},
	positionPopup:function(o){
		var x=0;
		var y=0;
		var h=o.offsetHeight;
		while (o != null){
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}
		poplp.popContainer.style.left=x-370+'px';
		poplp.popContainer.style.top=(y+h)-250+'px';
	},
	killPopup:function(e){
		poplp.popContainer.parentNode.removeChild(poplp.popContainer);
		poplp.popContainer=null;
		DOMhelp.cancelClick(e);
	}
}

DOMhelp.addEvent(window,'load',poplp.init,false);	
// -----------------------------------------------------------

// FUNCTION 2: POPLL
// popll = "popleftlandscape" i.e. creates a box above the 
// "larger image" link. This is for images on the right-hand side
// of the content area. The box needs to be above the link because
// there may not be enough screen space to the left of it.
popll={
	popContainer:null,
	triggerClass:'popleftland',
	openPopupLinkClass:'popuplink',
	popupClass:'popup',
	displayPrefix:'Close ',
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
		var allLinks=document.getElementsByTagName('a');
		for(var i=0;i<allLinks.length;i++){
			if(!DOMhelp.cssjs('check',allLinks[i],popll.triggerClass)){continue;}
			DOMhelp.addEvent(allLinks[i],'click',popll.openPopup,false);			
			allLinks[i].onclick=DOMhelp.safariClickFix;
			allLinks[i].preset=allLinks[i].innerHTML;
		}
	},
	openPopup:function(e){
		var t=DOMhelp.getTarget(e);
		if(t.nodeName.toLowerCase()!='a'){
			t=t.parentNode;	
		}
		if(!popll.popContainer){
			t.innerHTML=popll.displayPrefix+'image';
			popll.popContainer=document.createElement('div');
			DOMhelp.cssjs('add',popll.popContainer,popll.popupClass);
			DOMhelp.cssjs('add',t,popll.openPopupLinkClass);
			var newimg=document.createElement('img');
			newimg.setAttribute('src',t.getAttribute('href'));
			popll.popContainer.appendChild(newimg);
			document.body.appendChild(popll.popContainer);
			popll.positionPopup(t);
			popll.popContainer.onclick = function() {
						popll.killPopup();
						t.innerHTML=t.preset;
						DOMhelp.cssjs('remove',t,poplp.openPopupLinkClass);
						}
		} else {		
			popll.killPopup();
			t.innerHTML=t.preset;
			DOMhelp.cssjs('remove',t,popll.openPopupLinkClass);
		}
		DOMhelp.cancelClick(e);
	},
	positionPopup:function(o){
		var x=0;
		var y=0;
		var h=o.offsetHeight;
		while (o != null){
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}
		popll.popContainer.style.left=x-340+'px';
		popll.popContainer.style.top=(y+h)-400+'px';
	},
	killPopup:function(e){
		popll.popContainer.parentNode.removeChild(popll.popContainer);
		popll.popContainer=null;
		DOMhelp.cancelClick(e);
	}
}
DOMhelp.addEvent(window,'load',popll.init,false);	

// -----------------------------------------------------------

// FUNCTION 3: POPR
// popr = "popright" i.e. creates a box to the right of the 
// "larger image" link (for images on the left-hand side of the content area).
// This doesn't need "portrait" and "landscape" modes because
// there's plenty of space to the right of the image for both
// formats. 
popr={
	popContainer:null,
	triggerClass:'popright',
	openPopupLinkClass:'popuplink',
	popupClass:'popup',
	displayPrefix:'Close ',
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
		var allLinks=document.getElementsByTagName('a');
		for(var i=0;i<allLinks.length;i++){
			if(!DOMhelp.cssjs('check',allLinks[i],popr.triggerClass)){continue;}
			DOMhelp.addEvent(allLinks[i],'click',popr.openPopup,false);			
			allLinks[i].onclick=DOMhelp.safariClickFix;
			allLinks[i].preset=allLinks[i].innerHTML;
		}
	},
	openPopup:function(e){
		var t=DOMhelp.getTarget(e);
		if(t.nodeName.toLowerCase()!='a'){
			t=t.parentNode;	
		}
		if(!popr.popContainer){
			t.innerHTML=popr.displayPrefix+'image';
			popr.popContainer=document.createElement('div');
			DOMhelp.cssjs('add',popr.popContainer,popr.popupClass);
			DOMhelp.cssjs('add',t,popr.openPopupLinkClass);
			var newimg=document.createElement('img');
			newimg.setAttribute('src',t.getAttribute('href'));
			popr.popContainer.appendChild(newimg);
			document.body.appendChild(popr.popContainer);
			popr.positionPopup(t);
			popr.popContainer.onclick = function() {
						popr.killPopup();
						t.innerHTML=t.preset;
						DOMhelp.cssjs('remove',t,popr.openPopupLinkClass);
						}
		} else {		
			popr.killPopup();
			t.innerHTML=t.preset;
			DOMhelp.cssjs('remove',t,popr.openPopupLinkClass);
		}
		DOMhelp.cancelClick(e);
	},
	positionPopup:function(o){
		var x=0;
		var y=0;
		var h=o.offsetHeight;
		while (o != null){
			x += o.offsetLeft;
			y += o.offsetTop;
			o = o.offsetParent;
		}
		popr.popContainer.style.left=x+100+'px';
		popr.popContainer.style.top=(y+h)-220+'px';
	},
	killPopup:function(e){
		popr.popContainer.parentNode.removeChild(popr.popContainer);
		popr.popContainer=null;
		DOMhelp.cancelClick(e);
	}
}
DOMhelp.addEvent(window,'load',popr.init,false);
