top.usePopupScript = 0;
top.popopscriptIEmode = 0;//is set to ie automatically if IE, then handle Iframe as well.

function swichPopupOnOff(val){
	top.usePopupScript = val;

}

var compstatus=0,compimg,mX,mY,dW,dH,misc1,misc2,misc3=0,mes1,mes2,mes3,fopX,fopY,mXfix,mYfix;//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 mouse is moved outside the area of the original thumb, clear comp 
	mX = evt.clientX + document.body.scrollLeft;
	mY = evt.clientY + document.body.scrollTop;
	
	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(typeof(document.all['compIframe']) != 'undefined'){
		top.popopscriptIEmode = 1;
	}
	else {

	}
	compimg = img;	
	compstatus = 1;
	clearTimeout(misc1);
	misc1=setTimeout(getComp,500);
}

function getComp(){//called 500ms after mouseover, to start loading comp
	if(compstatus!=1)return;
	compstatus = 2;
	theimg = document.getElementById("compIMG");
	theimg.src=compimg.getAttribute("compimgsrc");//load compimg into the empty imagetag
	theimg.onload=showComp;
}

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,250);
}

function unresetComp(){
	clearTimeout(misc2);
}

function clearComp(){
	clearTimeout(misc1);
	compstatus = 0;
	thediv = document.getElementById("compDIV");
	theimg = document.getElementById("compIMG");
	thediv.style.display = 'none';
	theimg.style.display = 'none';
	if(top.popopscriptIEmode==1){
		theiframe = document.getElementById("compIframe");
		theiframe.style.visibility = 'hidden';
	}
}

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
	if(compstatus!=3)return;
	
	thediv = document.getElementById("compDIV");
	theimg = document.getElementById("compIMG");
	
	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);
	
	trgtwidth = theimg.width;
	trgtheight = theimg.height;
	
	if(mX > docwidth-400){
		pvLeft = mX-10;
		fopX = -1;
	}
	else{
		pvLeft = mX+10;
		fopX = 1;
	}
	if(mY > docheight-400){
		pvTop = mY-10;
		fopY = -1;
	}
	else{
		pvTop = mY+10;
		fopY = 1;
	}
	
	//alert('going to open preview\ndocwidth='+docwidth+'\ndocheight='+docheight+'\nmX='+mX+'\nmY='+mY+'\npvLeft='+pvLeft+'\npvTop='+pvTop);
	
	thediv.style.left = pvLeft;
	thediv.style.top = pvTop;
	
	thediv.style.width = '1px';
	thediv.style.height = '1px';
	thediv.style.display = 'block';
	theimg.style.display = 'block';
	mes1 = pvLeft;
	mes2 = pvTop;
	
	mXfix = mX;
	mYfix = mY;
	
	mes3 = 0.1;
	if(top.popopscriptIEmode==1){
		theiframe = document.getElementById("compIframe");
		theiframe.style.left = pvLeft;
		theiframe.style.top = pvTop;
		theiframe.style.width = '1px';
		theiframe.style.height = '1px';
		theiframe.style.visibility = 'visible';
	}
	//alert('start position:\mleft: '+mX+'\ntop:'+mY);
}

function fadeIn(){
	if(compstatus!=3)return;
	
//	alert('fadeIncall '+misc3+'\nthediv.style.left='+thediv.style.left+'\nthediv.style.width='+thediv.style.width);
	thediv = document.getElementById("compDIV");
	theimg = document.getElementById("compIMG");
	
	trgtwidth = theimg.width;
	trgtheight = theimg.height;
	
	if(mes3 < 1){
		mes3 = mes3 + 0.1;
		
		thediv.style.width = mes3 * trgtwidth;
		thediv.style.height = mes3 * trgtheight;
		
		if(fopX<0)//move to the left
			theleft = mes1 - (mes3 * trgtwidth);
		else
			theleft = mes1;
		
		if(fopY<0)//move up
			thetop = mes2 - (mes3 * trgtheight);
		else
			thetop = mes2;
		
		thediv.style.left = theleft>1?theleft:1;
		thediv.style.top = thetop>1?thetop:1;
		
		misc1 = window.setTimeout(fadeIn, 20);
	}
	else{
		compstatus = 4;
		if(top.popopscriptIEmode==1){
			theiframe = document.getElementById("compIframe");
			theiframe.style.left = thediv.style.left;
			theiframe.style.top = thediv.style.top;
			theiframe.style.width = trgtwidth + 2;
			theiframe.style.height = trgtheight + 2;
		}
	}
}

function afterFade(){//position image upon mouse move
	mes1 = mes1 + mX - mXfix;
	mes2 = mes2 + mY - mYfix;
	mXfix = mX;
	mYfix = mY;
	
	if(fopX<0)//move to the left
		theleft = mes1 - (mes3 * trgtwidth);
	else
		theleft = mes1;
	
	if(fopY<0)//move up
		thetop = mes2 - (mes3 * trgtheight);
	else
		thetop = mes2;
	
	thediv.style.left = theleft>1?theleft:1;
	thediv.style.top = thetop>1?thetop:1;
	
	if(top.popopscriptIEmode==1){
		theiframe = document.getElementById("compIframe");
		theiframe.style.left = thediv.style.left;
		theiframe.style.top = thediv.style.top;
	}
}

function Xbody(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
