// edit by Batiaan de Vogel 08-2009

top.usePopupScript = 1;
top.popopscriptIEmode = 0;//is set to ie automatically if IE, then handle Iframe as well.

function swichPopupOnOff(val){
	top.usePopupScript = val;
}

function dumpstr(s, nodename){
	str='dump\n';
	cnt=0;
	
	for(X in s){
		if( nodename==null || X.indexOf(nodename)==0){
			str += 'str.'+X+' is '+s[X]+' \n';
			cnt++
			
			if(cnt==20){
				alert(str);
				cnt=0;
				str='dump\n';
			}
		}
	}
	alert(str);
}

var compstatus=0,compimg,mX,mY,dW,dH,misc1,misc2,misc3=0,mes1,mes2,mes3,fopX,fopY,mXfix,mYfix,topscroll,totaltop,tgtWidth,tgtHeight;//status of comp; imageObject; mouseX, mouseY, imgDIV width; imgDIV height; timers for window; measurements for X, Y and width,height of div;

function mousemove(evt) {
	if (navigator.userAgent.indexOf('Safari') == -1) { // Alles behalve safari         
		mY = evt.clientY + document.body.scrollTop;
		mX = evt.clientX + document.body.scrollLeft;
		totaltop = document.documentElement.scrollTop;
		topscroll = evt.clientY;
	} else{ // safari         
		mY = evt.clientY + document.documentElement.scrollTop;
		mX = evt.clientX + document.documentElement.scrollLeft;
		totaltop = document.body.scrollTop;
		topscroll = evt.clientY
		
		mY = evt.clientY + document.body.scrollTop;
		mX = evt.clientX + document.body.scrollLeft;
		totaltop = document.documentElement.scrollTop;
		topscroll = evt.clientY;
	}
	//alert(document.body.scrollTop +' en '+ document.documentElement.scrollTop +' maar '+ document.body.scrollHeight +' maar '+ document.body.offsetHeight);
	//dumpstr(evt);
	
	if(compstatus==4){//enlargement is currently shown, move it along with the mouse
		afterFade();
	}
}

function doMouseOver(img){//upon mouseover of the thumb
	if(top.usePopupScript==0)
		return true;
		
	if(compstatus==4 && compimg.src==img.getAttribute("compimgsrc")){//onmouseover of the currenly shown image, do not reset and show again, just keep it.
		unresetComp();
	}
	else if(compstatus==4){//onmouseover of another image than currently showing, reset and show the new image
		unresetComp();
		clearComp();
	}
	else if(compstatus!=0){//currently in the process of getting an image shown, reset and go the new image
		unresetComp();
		clearComp();
	}
	
	if(undefined!=document.getElementById("compIframe")){
		top.popopscriptIEmode = 1;
	}
	
	compimg = img;
	compstatus = 1;
	clearTimeout(misc1);
	misc1=setTimeout(getComp,10);
}

function getComp(){//called 500ms after mouseover, to start loading comp
	if(compstatus!=1)return;
	
	theimg = document.getElementById("compIMG");
	thethimg = document.getElementById("thIMG");
	
	compstatus = 2;
	
	//show the thumbnail enlarged first and set loading of the comp. When the comp is loaded, replace the thumb with the comp
	thethimg.src = compimg.getAttribute("src");
	
	//enlarge the th, get factor based on largest edge
	if(compimg.height>compimg.width){//portrait image
		ratio = compimg.width / compimg.height;
		thethimg.height = 350;
		thethimg.width = Math.round(350 * ratio);
		tgtHeight=350;
		tgtWidth=Math.round(350 * ratio);
	}
	else{
		ratio = compimg.height / compimg.width;
		thethimg.width = 350;
		thethimg.height = Math.round(350 * ratio);
		tgtWidth=350;
		tgtHeight=Math.round(350 * ratio);
	}
	
	//alert('thethimg.width='+thethimg.width+'\nthethimg.height='+thethimg.height);
	
	//alert('thethimg.src='+thethimg.src+'\nthethimg.width='+compimg.width+'\nthethimg.height='+compimg.height);
	//set caption of the image into the zoom, based on teh ALT text of the thumb
	captiontxt = compimg.getAttribute("alt");
	captiondiv = document.getElementById("captionDiv");
	captiondiv.innerHTML = captiontxt;
	//captiondiv.innerHTML = 'hallo';
	captiondiv.style.width = tgtWidth + "px";
	captiondiv.style.height = 35 + "px";

	showComp();
	
	theimg.src=compimg.getAttribute("compimgsrc");//load compimg into the empty imagetag
	theimg.onload=replaceTH;
	
}

function replaceTH(){
	theimg = document.getElementById("compIMG");
	thediv = document.getElementById("compDIV");
	thethimg = document.getElementById("thIMG");
	captiondiv = document.getElementById("captionDiv");
	theimg.style.display = 'block';
	thethimg.style.display = 'none';
	thediv.style.height = theimg.height + 35 + 'px';
	thediv.style.width = theimg.width + 'px';
	captiondiv.style.width = theimg.width + 'px';
}

function showComp(){//called upon load of comp, to start showing it
	if(compstatus!=2)return;
	compstatus = 3;
	fadeStart();
	fadeIn();
}

function resetComp(){//called to remove the comp
	misc2 = window.setTimeout(clearComp,200);
}

function unresetComp(){
	clearTimeout(misc2);
}

function clearComp(){
	clearTimeout(misc1);
	compstatus = 0;
	thediv = document.getElementById("compDIV");
	theimg = document.getElementById("compIMG");
	thethimg = document.getElementById("thIMG");
	thethimg.height=350;
	thediv.style.display = 'none';
	theimg.style.display = 'none';
	thethimg.style.display = 'none';
}

function gotoDetails(){
	window.location=compimg.parentNode.href;
}

//internal functions for positioning and fading compimage and div
function fadeStart(){//set div and img to start position and visibitity for fading in
	//alert('fadestart')
	if(compstatus!=3) return;
	
	thediv = document.getElementById("compDIV");
	theimg = document.getElementById("compIMG");
	thethimg = document.getElementById("thIMG");
	

/*	if (window.innerHeight && window.scrollMaxY) {
	// Firefox         
	yWithScroll = window.innerHeight + window.scrollMaxY;         
	xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac         
	yWithScroll = document.body.scrollHeight;         
	xWithScroll = document.body.scrollWidth;     
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari         
	yWithScroll = document.documentElement.scrollHeight;         
	xWithScroll = document.documentElement.scrollWidth;       
	}     

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		by = window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		by = document.body.scrollTop;
	} else {
		//IE6 standards compliant mode
		by = document.documentElement.scrollTop;
	}*/

	docwidth=document.all? Xbody().scrollLeft+Xbody().clientWidth : pageXOffset+window.innerWidth-15;
	docheight=document.all? Math.min(Xbody().scrollHeight, Xbody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight);
	//alert('docwidth'+docwidth+'\n mX'+mX)
	//alert('docheight'+docheight+'\n mY'+mY)
	//alert('docwidth'+xWithScroll+'\n mX'+mX)
	//alert('docheight'+yWithScroll+'\n mY'+mY)

	SpaceUnderTH = docheight - mY;
	
	trgtwidth = tgtWidth;
	trgtheight = tgtHeight;
	
	if(mX > docwidth-400){
		pvLeft = mX-20;
		fopX = -1;
	}
	else{
		pvLeft = mX+20;
		fopX = 1;
	}
	if(SpaceUnderTH < 400){
		pvTop = mY-20;
		fopY = -1;
	}
	else{
		pvTop = mY+20;
		fopY = 1;
	}
	
	if(fopX<0)//move to the left
		theleft = mes1 - trgtwidth - 10;
	else
		theleft = mes1;
	
	thediv.style.left = (theleft>1?theleft:1) + "px";
	if(fopY==-1){//move up
		thetop = mes3 * trgtheight;
		thediv.style.top = ((totaltop + topscroll) - (trgtheight + 45)) + "px";
	}
	else{
		thetop = mes3 * trgtheight;
		thediv.style.top = ((totaltop + topscroll) + 20) + "px";
	}
	thediv.style.width = '1px';
	thediv.style.height = '1px';
	thediv.style.display = 'block';
	theimg.style.display = 'none';
	thethimg.style.display = 'block';
	  //alert('thediv.style.top' + thediv.style.top)
	 //alert('thediv.style.display'  + thediv.style.display)
	//alert('top.popopscriptIEmode1'+top.popopscriptIEmode1)

	if(top.popopscriptIEmode==0){
		trgtwidth = theimg.width;
		trgtheight = theimg.height;
		thediv.style.width = trgtwidth+'px';
		thediv.style.height = trgtheight+'px';
		thediv.style.zIndex = '999';
		
		docwidth=document.all? Xbody().scrollLeft+Xbody().clientWidth : pageXOffset+window.innerWidth;
		docheight=document.all? Math.min(Xbody().scrollHeight, Xbody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight);
		//alert('docwidth'+docwidth+'\n mX'+mX)
		var firefoxelft = mX;
		if  (mX + trgtwidth > docwidth)	{
			firefoxelft = docwidth - (trgtwidth + 100)
		}
		var firefoxtop = totaltop+topscroll;
		
		if  (mY + trgtheight > docheight){
			firefoxtop = firefoxtop -(trgtheight + 100);
		}
		
		thediv = document.getElementById("compDIV");
		thediv.style.top = firefoxtop+'px';
		thediv.style.left = firefoxelft+'px';
		//alert('thediv.style.top'+thediv.style.top)
		//alert('thediv.style.left'+thediv.style.left)
	}
	mes1 = pvLeft;
	mes2 = pvTop;
	
	mXfix = mX;
	mYfix = mY;
	
	mes3 = 0.1;
	
}


function fadeIn(){
	if(compstatus!=3)return;
	
	thediv = document.getElementById("compDIV");
	theimg = document.getElementById("compIMG");
	thethimg = document.getElementById("thIMG");
	
	trgtwidth = tgtWidth;
	trgtheight = tgtHeight;
	
	mes3 = 1;
	compstatus = 4;
	thediv.style.width = trgtwidth + 'px';
	thediv.style.height = trgtheight + 35 + 'px';


	if(fopX<0)//move to the left
		theleft = mX - theimg.width - 10;
	else
		theleft = mX + 10;
	
	thediv.style.left = (theleft>1?theleft:1) + "px";
	
	if(fopY==-1){//move up
		thetop = mY - theimg.height - 40;
	}
	else{
		thetop = mY + 10;
	}
	
	thediv.style.top = (thetop>1?thetop:1) + "px";
	
	captiondiv = document.getElementById("captionDiv");
	captiondiv.style.width = trgtwidth + 'px';
}

function afterFade(){//position image upon mouse move
	thediv = document.getElementById("compDIV");
	theimg = document.getElementById("compIMG");
	thethimg = document.getElementById("thIMG");
	
	trgtwidth = tgtWidth;
	trgtheight = tgtHeight;
	
	mes3 = 1;
	compstatus = 4;
	thediv.style.width = trgtwidth + 'px';
	thediv.style.height = trgtheight + 35 + 'px';


	if(fopX<0)//move to the left
		theleft = mX - theimg.width - 10;
	else
		theleft = mX + 10;
	
	thediv.style.left = (theleft>1?theleft:1) + "px";
	
	if(fopY==-1){//move up
		thetop = mY - theimg.height - 40;
	}
	else{
		thetop = mY + 10;
	}
	
	thediv.style.top = (thetop>1?thetop:1) + "px";
	
	captiondiv = document.getElementById("captionDiv");
	captiondiv.style.width = trgtwidth + 'px';
	
	/*mes1 = mes1 + mX - mXfix;
	mes2 = mes2 + mY - mYfix;
	mXfix = mX;
	mYfix = mY;

	if(fopX<0)//move to the left
		theleft = mes1 - theimg.width - 10;
	else
		theleft = mes1;
	
	thediv.style.left = (theleft>1?theleft:1) + "px";
	if(fopY==-1){//move up
		thetop = mes3 * trgtheight;
		thediv.style.top = ((totaltop + topscroll) - (theimg.height + 45)) + "px";
	}
	else{
		thetop = mes3 * trgtheight;
		thediv.style.top = ((totaltop + topscroll) + 20) + "px";
	}
	
	thediv.style.left = mX + "px";
	thediv.style.top = mY + "px";
	*/
}

function Xbody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}