var noHide = 1;

var steps = (document.all) ? 20  : 50;
var cnt;
var stepSize;
var itsbox, itsboxImg, itsBoxClose;
var cw, ch, cx, cy;
var xStep, yStep;
var tid;
var realWid, realHgt;
var notLoaded = 1;
var standardbody;
var animating = 0;

var ivCurrentImage = -1;

function ivShowDiv(idx, picID) 
{	   
    ivCurrentImage = idx;
    
	var idStr = mgImgCollection[idx].id;
	var len = idStr.length;
	var idx = idStr.indexOf('-') + 1;
	var newImgName = idStr.substr(idx, len - idx);
		 
	if (animating == 1)
	{
		hideWindow();
	}
		
	if (notLoaded == 1)	// first time initialisation
	{
		standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
		notLoaded = 0;
	}

    if (document.getElementById)
    {
		itsbox = document.getElementById('itsbox');

  		itsboxImg = document.createElement("IMG");
    	itsboxImg.id = "itsbox-img";
								
		var newImage = 'photos/' + newImgName + '.jpg'
				
		itsboxImg.loaded = 0;		
		
		itsboxImg.onload = ivLoaded;
		
		itsboxImg.src = newImage;	
	}
}

function ivLoaded()
{  
	cw = standardbody.clientWidth;
	ch = standardbody.clientHeight;
    vw = Math.max(standardbody.scrollWidth, standardbody.clientWidth);
    vh = Math.max(standardbody.scrollHeight, standardbody.clientHeight);
    
	var scrollVal = ivGetScrollXY();
	cx = scrollVal.x + cw / 2;
	cy = scrollVal.y + ch / 2;
	
	itsbox.style.left = cx + "px";
	itsbox.style.top = cy + "px";	
	
	imageLoading = 0; // no longer loading an image
	statusdiv.style.visibility =  "hidden";

	realWid = itsboxImg.width;
	realHgt = itsboxImg.height;
	
	itsboxImg.width = 0;
	itsboxImg.height = 0;
	
	itsbox.style.visibility = "hidden";
	itsbox.style.display = "inline";
	
	var d_nested = document.getElementById("itsbox-img"); 
	if (d_nested)
	{
		var throwawayNode = itsbox.removeChild(d_nested); 
	}
			
	returnValue = itsbox.appendChild(itsboxImg);		
	
	cnt = 1;
		
	itsboxOverlay = document.getElementById('itsbox-overlay');
	itsboxOverlay.style.height = vh + "px";
	itsboxOverlay.style.width = vw + "px";
	
	xStep = realWid / (2 * steps);	
	yStep = realHgt  / (2 * steps);	
	
	xStepSize = realWid / steps;
	yStepSize = realHgt / steps;
		
	var newX = cx - xStep;
	var newY = cy - yStep;
	
	itsbox.style.left = newX + "px";
	itsbox.style.top = newY + "px";
	
	itsbox.style.visibility = "visible";
	
	this.loaded = 1; 
	animating = 1;
	
	tid = window.setInterval("ivCheckload()", 10);  
}

function setOpacity(obj, value) 
{
	obj.style.opacity = value/100;
	obj.style.filter = 'alpha(opacity=' + value + ')';
}

function ivCheckload()
{			
	if (animating == 1)
	{
		var dx, dy;
		var opLevel = 100;
		if ((xStepSize * cnt) < realWid)
		{
			opLevel = (cnt * 100 / steps);
			itsboxImg.width = cnt * xStepSize;
			itsboxImg.height = cnt * yStepSize;
			
			dx = cx - (cnt * xStep);
			dy = cy - (cnt * yStep);
		}
		else
		{
			itsboxImg.width = realWid;
			itsboxImg.height = realHgt;
						
			dx = cx - (realWid / 2);
			dy = cy - (realHgt / 2);
				
			window.clearInterval(tid);
			animating = 0;
						
			itsboxClose = document.getElementById("itsbox-close"); 
			itsBoxNav = document.getElementById("itsbox-nav"); 
			
		    if (itsBoxNav)
		    {		        
		    	itsBoxNav.style.display = "block";
		   	}
			
		    if (itsboxClose)
		    {
		        itsboxClose.style.display = "block";
		    }
		    else
		    {
  		        itsboxClose = document.createElement("div");
    	        itsboxClose.id = "itsbox-close";
    	        itsboxClose.title = "Close";
    	        itsboxClose.onclick = hideWindow;
    	        itsbox.appendChild(itsboxClose);			      
		        // var throwawayNode = itsbox.removeChild(itsboxClose); 		      
		    }
		}
		
		//alert(cx + " " + cy + " := " + dx + " " + dy);
				
		setOpacity(itsboxImg, opLevel);
		itsbox.style.left = dx + "px";
		itsbox.style.top = dy + "px";
		cnt++;
	}
	return true;
}
 
function hideWindow()
{
	if (noHide == 1)
	{
		if (animating == 1)
		{
			window.clearInterval(tid);
			animating = 0;
		}
    	itsboxClose.style.display = "none";  
    	itsbox.style.display = "none";	
    	itsboxOverlay.style.height = "0px";	  
	}
	
	noHide = 1;
}

function ivGetScrollXY()
{
    var scrOfX = 0, scrOfY = 0;
    if (typeof(window.pageYOffset) == 'number')
    {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    }
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    }
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return {x:scrOfX, y:scrOfY};
}

function ivChangeImage(dir)
{
    var newImage = ivCurrentImage + dir;
    
    if (newImage < 0) 
    {   
        newImage = mgImgCollection.length - 1;
    }
    else if (newImage > mgImgCollection.length - 1)
    {
        newImage = 0;
    }
    
    itsboxClose.style.display = "none";  
    itsBoxNav.style.display = "none";
     
    ivShowDiv(newImage, null);
    
    return false;
}

function ivOnResize()
{
    if (notLoaded == 0)
    {
        if (itsbox.style.display != "none")
        { // resize the overlay box
	        cw = standardbody.clientWidth;
	        ch = standardbody.clientHeight;
            vw = Math.max(standardbody.scrollWidth, standardbody.clientWidth);
            vh = Math.max(standardbody.scrollHeight, standardbody.clientHeight);
	                	
	        var scrollVal = ivGetScrollXY();
            cx = scrollVal.x + cw / 2;
            cy = scrollVal.y + ch / 2;	     
              	
	        itsboxOverlay = document.getElementById('itsbox-overlay');
        	
	        itsboxOverlay.style.height = vh + "px";
	        itsboxOverlay.style.width = vw + "px";	
	        
	        if (animating == 0)
	        {
		        dx = cx - (realWid / 2);
		        dy = cy - (realHgt / 2);	
		                
		        itsbox.style.left = dx + "px";
		        itsbox.style.top = dy + "px";
	        }
	    }
	}
}


