//
// Do NOT modify this file!
// (C) Copyright Magnus Wedberg 2003, 2004, 2005, 2006 www.magnuswedberg.com
//


var queueArray = new Array(0);
var picImg = new Array(picSrc.length);
var picCached = new Array(picSrc.length);
var lLim = 0;
var uLim = 0;
var i = 0;

if ((ie < 4) || ((ns < 5) && (ns > 1)) || (opera > 0)) fade = false;


if (Array.prototype.push && ([0].push(true)==true))
        Array.prototype.push = null;

if(!Array.prototype.push)
{
function array_push()
        {
        for(p=0;p<arguments.length;p++)
                this[this.length] = arguments[p];
        return this.length;
        }
Array.prototype.push = array_push;
}

if(!Array.prototype.shift)
{
    function array_shift()
        {
        firstElement = this[0];
        this.reverse();
        this.length = Math.max(this.length-1,0);
        this.reverse();
        return firstElement;
        }
Array.prototype.shift = array_shift;
}


function markImagesAsUncached()
{
for (var n = 0; n < picSrc.length; n++)
        picCached[n] = false;
}
markImagesAsUncached();

function fireUpImage(imgNum)
{
if ((cacheBack != 0) || (cacheAhead != 0))
        {
        if (imgNum == 0)
                {
                uLim = cacheBack + cacheAhead;
                lLim = 0;
                }
        else if (imgNum >= (picSrc.length - cacheAhead - 1))
                {
                if (imgNum - cacheBack <= 0)
                        lLim = imgNum - cacheBack;
                uLim = imgNum + picSrc.length - 1 - imgNum;
                }
        else
                {
                uLim = imgNum + cacheAhead;
                lLim = imgNum - cacheBack;
                }
        }
else
        {
        uLim = imgNum;
        lLim = imgNum;
        }
                                        // phew, now we know what to precache
for (var n = lLim; n != uLim+1; n++)    // so do it
        {
        if (picCached[n] == false)
                {
                picCached[n] = true;
                picImg[n] = new Image();
                picImg[n].src = 'http://www.magnuswedberg.com/' + picPath + picSrc[n];
                }
        }
}


function fadeToGrey(i)
{
if ((ns >= 5) && (opera == 0))
        {
        document.getElementById('pic1').style.MozOpacity = 1-i/100;
        document.getElementById('pic2').style.MozOpacity = i/100;
        }
else if (khtml > 0)
        {
        document.getElementById('pic1').style.KhtmlOpacity = 1-i/100;
        document.getElementById('pic2').style.KhtmlOpacity = i/100;
        }
else if (opera > 8)
        {
        document.getElementById('pic1').style.opacity = 1-i/100;
        document.getElementById('pic2').style.opacity = i/100;
        }
else if (ie > 4)
        {
        document.all.pic1.style.filter = 'alpha(opacity=' + (100-i) +')';
        document.all.pic2.style.filter = 'alpha(opacity=' + i +')';
        }
}


function fadeLoop()
{
i = i + fadingSteps;
fadeToGrey(i);
if (fade == false)
	i = 0;
Visage();
}


function Visage()
{
if (fade == true)                       // if we should fade the image
        {
        if (i < 100)
                fadeID=setTimeout('fadeLoop()',fadingSpeed)
        else
                {
                document.images['pic1'].src = document.images['pic2'].src;
                centerMe('pic1');
                timeID=setTimeout('i = 0',takeItEasy);
                }
        }
else                                    // if not, just smack up the pic
	{
	fadeToGrey(100); // make sure the pic is 100% opaque, useful if user toggled fade during transition
	centerMe('pic1');
	document.images['pic1'].src = document.images['pic2'].src;
	}
}


function queuePic()
{
// window.status=queueArray;
if ((i == 0) && (queueArray.length > 0))        // previous pic is finished. Fine, swap
        {
        if ((picImg[(queueArray[0])].complete == true))
                {
                picCurrent = queueArray.shift();
		if (slideshow == true)
			{
			if (picCurrent == (picSrc.length-1))
				{
				if (loopSlideshow == true)
					{
					fireUpImage(0);
		                	queueArray.push(0);
					}
				else
					toggleSlideshow(); // for magnuswedberg.com
				}
			else
				{
				fireUpImage(picCurrent+1);
		                queueArray.push(picCurrent+1);
				}
			}
                document.images['pic2'].src = picImg[picCurrent].src;
                document.images['loadpic'].style.visibility = 'hidden';
                fadeToGrey(0);
                centerID=setTimeout('centerMe(\'pic2\')',10);
		timeID2=setTimeout('Visage()',150);
                }
        else 
                document.images['loadpic'].style.visibility = 'visible';
        }
if (slideshow == true)
	queueID=setTimeout('queuePic()',takeItEasy);
else
	queueID=setTimeout('queuePic()',250);
}


function addToStack(target)
{
if (target > (picSrc.length-1))
        target=picSrc.length-1;
if (target < 0)
        target=0;

if (ns == 4)
        {
	// old and crusty code, meant for Pauls site; should be updated for general use
        tempString = (location.href.substring(0,location.href.indexOf('?')));
        if (tempString == '') tempString = location.href;
        location.href = (tempString  + '?pic=' + target + '&scroll=' + delUnit(scrollBox.left));
        }
else
        {
        fireUpImage(target);
        if ((queueArray[(queueArray.length-1)] != target) && (target != picCurrent))
                queueArray.push(target);
        }
}


function setRez(size)
{
picPath = "/pics/" +size+ "/";
for (var n = 0; n < picSrc.length; n++)
        picCached[n] = false;
fireUpImage(picCurrent);
queueArray.push(picCurrent);
}


function startSlideShow(startPic,changeTime,loopForever)
{
if (startPic != picSrc.length)
        {
	slideshow = true;
	loopSlideshow = loopForever;
	oldTakeItEasy = takeItEasy;
	takeItEasy = changeTime;
        addToStack (startPic);
        // showID=setTimeout('startSlideShow('+(startPic+1)+','+changeTime+','+loopForever+')',changeTime);
	// above code lead to a race condition. Heh
        }
}


function stopSlideShow()
{
queueArray.length = 0;
slideshow = false;
loopSlideshow = false;
takeItEasy = oldTakeItEasy;
}


function centerMe(targetPic)
{
if ((picX > 0) && (picY > 0))
{
if (picImg[picCurrent].width < picImg[picCurrent].height)
	{
	if (hires)
		bonusHeight = 134;
	else
		bonusHeight = 88;	// for magnuswedberg.com
	infoBoxWidth = 98;	// ditto, setting info box width (this information from the reDUHndancy department department)
	}
else
	{
	bonusHeight = 0;
	infoBoxWidth = 240;
	}
if (ns >= 5)
        {
        document.getElementById(targetPic + 'Layer').style.left = (picX - (picImg[picCurrent].width/2) + 'px');
        document.getElementById(targetPic + 'Layer').style.top = (bonusHeight + picY - (picImg[picCurrent].height/2) + 'px');
        }
else if (ie >= 4)
        {
        document.all[(targetPic + 'Layer')].style.left = (picX - (picImg[picCurrent].width/2) + 'px');
        document.all[(targetPic + 'Layer')].style.top = (bonusHeight + picY - (picImg[picCurrent].height/2) + 'px');
        }
if (ie > 3)
	document.all.photoInfo.style.width = infoBoxWidth + 'px';
else
	document.getElementById('photoInfo').style.width = infoBoxWidth + 'px';
if (infoSrc[picSrc[picCurrent]])
	writeInLayer ('photoInfo', infoSrc[picSrc[picCurrent]]);
else
	writeInLayer ('photoInfo', '');
}
}

function startMeUp()
{
if (ns != 4)
        {
        fireUpImage(picCurrent);                                // start caching
        queueArray.push(picCurrent);                            // loads the first big picture
        queuePic();                                             // starts the checking loop
        }
}

