﻿// JScript File
bIE = (document.getElementById && document.all);
bNS = (document.getElementById && !bIE);

bFade = bIE || bNS;

//var Previews = new Array();

//<? echo($preview); ?>
//TODO: JSCript variables
//var iMaxImage = <?=$lastindex?>;

var iWait = new Image(); iWait.src = '/images/loading.gif';

var oImg; var oLoad, oCaption, oLinks, iLastIndex, iCurrentImage;

function Reveal(oDiv)
{
  oBack = document.getElementById('divAboutBack');
  sTop = parseInt(document.body.scrollTop).toString() + 'px';
	//sHeight = parseInt(document.body.scrollHeight).toString() + 'px';
	sHeight = getDocHeight().toString() + 'px';
  oBack.style.height = sHeight;	
	oBack.style.display = 'block';
  
  oDiv.style.visibility = 'hidden';
  oDiv.style.display = 'block';
  oDiv.style.top = (scrollY() + 50).toString() + 'px';
  oDiv.style.left = ((windowWidth() / 2) - (parseInt(oDiv.scrollWidth) / 2)).toString() + 'px';
  oDiv.style.visibility = 'visible';
}

function getDocHeight() 
{
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function nextPreview(iDir)
{
    iNextImage = iCurrentImage + iDir;
	
	if (iNextImage <= 0) iNextImage = 0;
	if (iNextImage >= iMaxImage) iNextImage = iMaxImage;
	
	oImg.style.display = 'none';
	oLinks.style.display = 'none';
	oCaption.style.display = 'none';
	
	showImage(iNextImage); 
}

function showImage(i)
{
    iCurrentImage = i;

    oBlack = document.getElementById('divGalleryBack');
    oImg = document.getElementById('imgPreview');
    oLoad = document.getElementById('imgLoading');
    oCaption = document.getElementById('txtCaption');
    oLinks = document.getElementById('divLinks');
	
    sImg = Previews[i][0];
	sCaption = Previews[i][1];

	sTop = parseInt(document.body.scrollTop).toString() + 'px';
	//sHeight = parseInt(document.body.scrollHeight).toString() + 'px';
	sHeight = getDocHeight().toString() + 'px';
  
    oBlack.style.height = sHeight;	
	oBlack.style.display = 'block';
	
	oImg.style.display = 'none';
	
	if (iLastIndex != i)
	{  
	    oImg.src = sImg;
		bForce = false;
		iLastIndex = i;
	}
	else
	{
        // same image - onload won't fire in Safari
        bForce = true;
	} 
	
	
	//alert(bForce);
	
	iHeight = windowHeight();
    iWidth = windowWidth();
	
	sTop = ((iHeight / 2) - (parseInt(oLoad.height) / 2) + scrollY()).toString() + 'px';
	sLeft = ((iWidth / 2) - (parseInt(oLoad.width) / 2)).toString() + 'px'
	
	oLoad.style.top = sTop;
	oLoad.style.left = sLeft;
	
	clipL = 0; clipR = oLoad.width; clipT = 0; clipB = oLoad.height;
	oLoad.style.clip = clipString();
	oLoad.style.display = 'block';

	oCaption = document.getElementById('txtCaption');
	oCaption.innerHTML = sCaption;
	
	if (bForce) { expandImage(); }
	
	pageTracker._trackPageview('/ShowImage/' + sImg);
}

function windowHeight()
{
    h = (isNaN(window.innerHeight)) ? document.documentElement.clientHeight : window.innerHeight;
	if (h == 0) h = document.body.clientHeight;
	return h;
}

function windowWidth()
{
    w = (isNaN(window.innerWidth)) ? document.documentElement.clientWidth : window.innerWidth;
	if (w == 0) w = document.body.clientWidth;
	return w;
}

function scrollY()
{
    y = (isNaN(window.pageYOffset)) ? document.documentElement.scrollTop : window.pageYOffset;
	if (y <= 0) y = document.body.scrollTop;
	return y;
}

function closePreview()
{
    oBlack.style.display = 'none';
	oImg.style.display = 'none';
	oLinks.style.display = 'none';
	oCaption.style.display = 'none';
}

var loadH = 0;
var loadW = 0;
var imgH = 0;
var imgW = 0;
var clipT = 0;
var clipR = 0;
var clipB = 0;
var clipL = 0;
var sAction = '';
var iInterval = 0;
var iTimeout = 0;
var iTrans = 0;

function expandImage()
{
	loadH = oLoad.height;
	loadW = oLoad.width;
	
	oImg.style.clip = 'rect(0px,0px,0px,0px)';
	oImg.style.display = 'block';
	
	imgH = oImg.height;
	imgW = oImg.width;
	
	//oImg.style.top = (scrollY() + 10).toString() + 'px';
	t = ((windowHeight() / 2) - (imgH / 2) + scrollY());
	if (t < (scrollY() + 10)) t = scrollY() + 10;
	oImg.style.top = t.toString() + 'px';
	oImg.style.left = ((windowWidth() / 2) - (imgW / 2)).toString() + 'px';
	
    sAction = '';
	doIt();
}

function clipString()
{
    sClip = 'rect(' + parseInt(clipT).toString() + 'px,' + parseInt(clipR).toString() + 'px,' + parseInt(clipB).toString() + 'px,' + parseInt(clipL).toString() + 'px)';
	return sClip;
}

function setTransparency(oElement, iValue)
{
    if (bIE) oElement.style.filter = 'alpha(Opacity=' + parseInt(iValue).toString() + ')';
	if (bNS) oElement.style.opacity = (iValue / 100);
}

function doIt()
{
  switch(sAction)
	{
	  case '':
		    clipT = 0; clipR = loadW; clipB = loadH; clipL = 0;
			sAction = 'ShrinkLoad';
			window.setInterval(doIt, 50);
		  break;
			
		case 'ShrinkLoad':
	        x = (loadW / 20);
	        clipL += x; clipR -= x;
			
			if (clipL > clipR) 
			{
		        oLoad.style.display = 'none';
		        // going to start image - width first
		        sAction = 'MainWidth';
				clipT = (imgH / 2) - (loadH / 2);
				clipB = (imgH / 2) + (loadH / 2);
				clipL = imgW / 2;
				clipR = imgW / 2;
				oImg.style.clip = clipString();
			}
			else
			{
			  oLoad.style.clip = clipString();
			}
		  break;
			
		case 'MainWidth':
		    x = imgW / 20;
			clipL -= x; clipR += x;
			oImg.style.clip = clipString();
			
			if (clipL < -10 && parseInt(clipR) > (imgW + 10))
			{
			  sAction = 'MainHeight';
			}

		  break;
			
		  case 'MainHeight':
		    y = imgH / 20;
			clipT -= y; clipB += y;
			oImg.style.clip = clipString();
			
			if (clipT < -10 && parseInt(clipB) > (imgH + 10))
			{
			  sAction = 'Caption';
				
				oCaption.style.width = (imgW / 2).toString() + 'px';
				oCaption.style.top = (parseInt(oImg.style.top) + imgH + 10).toString() + 'px';
				oCaption.style.left = (windowWidth() / 2).toString() + 'px';
				
				iTrans = 10;
				setTransparency(oCaption, iTrans);
				
				oCaption.style.display = 'block';
			}

		  break;
			
		case "Caption":
		  iTrans += 10;
			setTransparency(oCaption, iTrans);
			
			if (iTrans >= 100)
			{
			  sAction = 'Close';
			
				oLinks.style.top = (parseInt(oImg.style.top) + imgH + 10).toString() + 'px';
				oLinks.style.left = oImg.style.left;
				
				iTrans = 10;
				setTransparency(oLinks, iTrans);
				
				// hide non-working links
				document.getElementById('lnkPrev').style.visibility = (iCurrentImage > 0) ? 'visible' : 'hidden';
				document.getElementById('lnkNext').style.visibility = (iCurrentImage < iMaxImage) ? 'visible' : 'hidden';
				
				oLinks.style.display = 'block';
			}
			
		  break; 
			
		case "Close":
		  iTrans += 10;
			setTransparency(oLinks, iTrans);
			
			if (iTrans >= 100) sAction = 'Stop';
		  break;
	}
	
	if (sAction == 'Stop') 
	{
	  window.clearInterval(iInterval);
	}
}

