// JavaScript Document


/* general variables */

function fadeInit(index, startDelay) 
{
	if (document.getElementById) 
	{
		eval("slides_" + index + " = new Array");
		var node = eval ("gallery_" + index + ".firstChild");
		while (node) 
		{
			if (node.nodeType==1) 
			{
			    eval("slides_" + index + ".push(node)");
			}
			node = node.nextSibling;

        }
        var thisSlides = eval("slides_" + index);
        for (i = 0; i < thisSlides.length; i++) 
		{
			/* loop through all these child nodes and set up their styles */
		    thisSlides[i].style.position = 'absolute';
		    thisSlides[i].style.top = 0;
		    thisSlides[i].style.zIndex = 0;
			/* set their opacity to transparent */
		    SetOpacity(i, 0, thisSlides);
}
		
		/* make the list visible again */
        eval("gallery_" + index + ".style.visibility = 'visible';");
		
		/* initialise a few parameters to get the cycle going */
        eval("currentImage_" + index + "=0;");
        eval("previousImage_" + index + "=thisSlides.length-1;");
        eval("opacity_" + index + "=100;");
        eval("SetOpacity(currentImage_" + index + ", 100, thisSlides);");
        eval("thisSlides[currentImage_" + index + "].style.zIndex = 100;");
        //Start the crossfade
        if (startDelay == 0)
		{
		    PrepareNextSlide(true, index);
		}
		else
		{
		    window.setTimeout("PrepareNextSlide(true, " + index + ")", startDelay * 1000);
		}
	}
}

function SetOpacity(imageNumber, opacity, thisSlides) 
{
	/* helper function to deal specifically with images and the cross-browser differences in opacity handling */
    var obj = thisSlides[imageNumber];
	if (obj.style) 
	{
		if (obj.style.MozOpacity!=null) 
		{  
			/* Mozilla's pre-CSS3 proprietary rule */
			obj.style.MozOpacity = (opacity/100) - .001;
		} 
		else if (obj.style.opacity!=null) 
		{
			/* CSS3 compatible */
			obj.style.opacity = (opacity/100) - .001;
		} 
		else if (obj.style.filter!=null) 
		{
			/* IE's proprietary filter */
			obj.style.filter = "alpha(opacity="+opacity+")";
		}
	}
}

function crossfade(opacity, index) 
{
    var thisSlides = eval("slides_" + index);
    /* make sure the current image is on top of the previous one */
    if (opacity == 0) 
    {
        //alert("setting zindex prev" + previousImage + ", " + currentImage + "=" + currentImage);
        eval("thisSlides[previousImage_" + index + "].style.zIndex = 0;");
        eval("thisSlides[currentImage_" + index + "].style.zIndex = 100;");
    }

	if (opacity < 100) 
	{
		//Alter opacity
	    eval("SetOpacity(currentImage_" + index + ", opacity, thisSlides);");
		//SetOpacity(previousImage,100-opacity);
		
		opacity += 2;
		eval("timer_" + index + " = window.setTimeout('crossfade(' + opacity + ', " + index + ")', 25);");
		eval("fadeInProgress_" + index + " = true;");
	} 
	else 
	{
	    var playing = eval("playing_" + index);
	    if (playing)
	    {
	        PrepareNextSlide(true, index);
	    }
	    else
	    {
	        eval("clearTimeout(timer_" + index + ");");
	    }
	}
}



function PrepareNextSlide(fadeIn, index)
{
    var thisSlides = eval("slides_" + index);

    /* make the previous image - which is now covered by the current one fully - transparent */
    eval("SetOpacity(previousImage_" + index + ", 0, thisSlides);");

    var curImg = eval("currentImage_" + index);
    if (!fadeIn) 
    {
	    var prevImg = eval("previousImage_" + index);
	    thisSlides[prevImg].style.zIndex = 0;
	    thisSlides[curImg].style.zIndex = 100;
	    SetOpacity(curImg, 100, thisSlides);
	    //PrepareNextSlide(true);
	}
	
	//Change the description
	eval("slidedesc_" + index + ".innerHTML = arrDescs_" + index + "[curImg];");
	eval("slideno_" + index + ".innerHTML = 'Slide ' + (curImg + 1) + ' of ' + thisSlides.length;");
	//slidedesc.innerHTML = currentImage;
	
	/* current image is now previous image, as we advance in the list of images */
	eval("previousImage_" + index + " = currentImage_" + index + ";");
	eval("currentImage_" + index + "+=1;");
	var curImg = eval("currentImage_" + index);
	if (curImg >= thisSlides.length) 
	{
		/* start over from first image if we cycled through all images in the list */
	    eval("currentImage_" + index + "=0;");
	}
	
	//alert(slides[currentImage].id);
	/* start the crossfade */
	eval("opacity_" + index + " = 0;");
	eval("fadeInProgress_" + index + " = false;");
	var playing = eval("playing_" + index);
    if (playing)
    {
        eval("timer_" + index + " = window.setTimeout('crossfade(' + opacity_" + index + " + ', " + index + ")', showImgFor_" + index + ");");
    }
}



function MoveControls(show, index)
{
    var ctls = document.getElementById("slidectrls_" + index);
    var ctlTop = eval("ctlTop_" + index);
    var ctlPos = eval("ctlPos_" + index);
    if (show) 
    {
        
        if (ctlPos > ctlTop)
        {
            eval("showInProgress_" + index + " = true;");
            eval("hideInProgress_" + index + " = false;");
            eval("ctlPos_" + index + " -= 2;");
            eval("ctltimer_" + index + " = window.setTimeout('MoveControls(true, " + index + ")', 10);");
        }
        else
        {
            eval("showInProgress_" + index + " = false;");
            eval("clearTimeout(ctltimer_" + index + ");");
        }
    }
    else
    {
        if (ctlPos < (ctlTop + 38))
        {
            eval("showInProgress_" + index + " = false;");
            eval("hideInProgress_" + index + " = true;");
            eval("ctlPos_" + index + " += 2;");
            eval("ctltimer_" + index + " = window.setTimeout('MoveControls(false, " + index + ")', 10);");
        }
        else
        {
            eval("hideInProgress_" + index + " = false;");
            eval("clearTimeout(ctltimer_" + index + ");");
        }
    }

    var ctlPos = eval("ctlPos_" + index);
    ctls.style.marginTop = ctlPos + "px";
    
}




function ShowControls(index)
{
    var showInProgress = eval("showInProgress_" + index);
    if (!showInProgress)
    {
        eval("clearTimeout(ctltimer_" + index + ");");
        MoveControls(true, index);
    }
}



function HideControls(index)
{
    var hideInProgress = eval("hideInProgress_" + index);
    if (!hideInProgress)
    {
        eval("clearTimeout(ctltimer_" + index + ");");
        MoveControls(false, index);
    }
}



function NextSlide(index)
{
    eval("window.clearTimeout(timer_" + index + ");");
    PrepareNextSlide(false, index);
	//slidedesc.innerHTML = "cur = " + currentImage + ", prev = " + previousImage;
}




function PrevSlide(index)
{
    eval("window.clearTimeout(timer_" + index + ");");
    var thisSlides = eval("slides_" + index);
    var fadeInProgress = eval("fadeInProgress_" + index);
    if (fadeInProgress == true) 
    {
        eval("fadeInProgress_" + index + " = false;");
        eval("SetOpacity(currentImage_" + index + ", 0, thisSlides);");
    }

    eval("currentImage_" + index + "-=2;");
    var currentImage = eval("currentImage_" + index);
    if (currentImage == -1)
    {
        eval("currentImage_" + index + " = thisSlides.length - 1;");
    }
    else if (currentImage < -1)
    {
        eval("currentImage_" + index + " = thisSlides.length - 2;");
    }
    eval("SetOpacity(previousImage_" + index + ", 0, thisSlides);");
    PrepareNextSlide(false, index);
	//slidedesc.innerHTML = "cur = " + currentImage + ", prev = " + previousImage;
}




function StartStop(index)
{
	//slidedesc.innerHTML = "cur = " + currentImage + ", prev = " + previousImage;
    eval("playing_" + index + "= !playing_" + index + ";");
    var link = document.getElementById("startstop_" + index);
    var img = document.getElementById("playpause_" + index);

    //link.innerHTML = playing ? "Pause" : "Play";
    var playing = eval("playing_" + index);
    eval("img.src = playing ? pauseImage_" + index + ".src : playImage_" + index + ".src;");
    
    if (playing)
    {
        eval("timer_" + index + " = window.setTimeout('crossfade(0, " + index + ")', showImgFor_" + index + ");");
    }
    else
    {

        var fadeInProgress = eval("fadeInProgress_" + index);
        if (!fadeInProgress)
        {
            eval("window.clearTimeout(timer_" + index + ");");
        }
    }
}




function dn() 
{
}