/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/


window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, 
    imgs = new Array(),
    anchors =  new Array();
    zInterval = null, 
    current=0, 
    iTimeoutID = null,
    pause=false;
    
function so_init() 
{
	if(!d.getElementById || !d.createElement)return;
	
	css = d.createElement("link");
	css.setAttribute("href","/CommonStyleSheets/xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);
	
	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	anchors = d.getElementById("imageContainer").getElementsByTagName("a");

	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	anchors[0].style.zIndex = "998";
	imgs[0].style.zIndex = "998";
	
    // myDelayArray is passed in from BBRImageSlideShowControlChild.ascx filled 
    // with delay values for each image (pulled from the XML file)
	iTimeoutID = setTimeout(so_xfade, myDelayArray[current]);
}

function so_xfade()
{
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;
	
	// Set the z-index for the anchor and the image so that the correct HREF is 
	// associated with the image displayed
	for(i=1;i<anchors.length;i++) anchors[i].style.zIndex = "0";
	anchors[nIndex].style.zIndex = "998";
	
	for(i=1;i<imgs.length;i++) imgs[i].style.zIndex = "0";
	imgs[nIndex].style.zIndex = "998";
	
	cOpacity -= .05; 
	if (cOpacity < 0.0) cOpacity = 0.0;
	
	nOpacity += .05;
	if (nOpacity > 0.99) nOpacity = 0.99;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if (cOpacity<=0) 
	{
		imgs[current].style.display = "none";
		current = nIndex;
		// see note above about how the myDelayArray is filled with delay values
		iTimeoutID = setTimeout(so_xfade, myDelayArray[current]);
	} 
	else 
	{
		iTimeoutID = setTimeout(so_xfade, 50);
	}
	
	function setOpacity(obj) 
	{
		if(obj.xOpacity>.99) 
		{
			obj.xOpacity = .99;
			return;
		}
		
		// myBrowserName is set in the Web Control BBRImageSlideShowControlChild and passed here
        if ((myBrowserName == "NETSCAPE") || 
            (myBrowserName == "OPERA") ||
            (myBrowserName == "FIREFOX") ||
            (myBrowserName == "APPLEMAC-SAFARI") ||
            (myBrowserName == "SAFARI"))
        {
            obj.style.opacity = obj.xOpacity;
        }
        else if (myBrowserName == "IE")
        {
            obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
        }
        else
        {
            obj.style.MozOpacity = obj.xOpacity;
        }
	}
}

function moveToPix (direction)
{
    var nIndex = 0;
    
    // kill the fade function (that is waiting on a timer)
    clearTimeout (iTimeoutID);
    
    // force a move to the next image (forward or reverse)
    if (direction == 1)  // forward
    {
        nIndex = current+1<imgs.length?current+1:0;
        //alert ("nIndex = " + nIndex + "    current = " + current);
        if (document.getElementById("slideshow_RightShiftImageDown").style.visibility=="visible")
        {
            // wait for a moment and then switch shift image back
            setTimeout(setRightShifterUp, 250);
        }
    }
    else  // reverse
    {
        nIndex = current==0?imgs.length-1:current-1;
        //alert ("nIndex = " + nIndex + "    current = " + current);
        if (document.getElementById("slideshow_LeftShiftImageDown").style.visibility="visible")
        {
            // wait for a moment and then switch shift image back
            setTimeout(setLeftShifterUp, 250);
        }
    }
   
    imgs[nIndex].style.display = "block";
    
    // show the new one
    // myBrowserName is set in the Web Control BBRImageSlideShowControlChild and passed here
    if ((myBrowserName == "NETSCAPE") || 
        (myBrowserName == "OPERA") ||
        (myBrowserName == "FIREFOX") ||
        (myBrowserName == "APPLEMAC-SAFARI") ||
        (myBrowserName == "SAFARI"))
    {
        imgs[current].style.opacity = 0.0;
        imgs[nIndex].style.opacity = .99;
    }
    else if (myBrowserName == "IE")
    {
        imgs[current].style.filter = "alpha(opacity=0)";
        imgs[nIndex].style.filter = "alpha(opacity=100)";
    }
    else
    {
        imgs[current].style.MozOpacity = 0.0;
        imgs[nIndex].style.MozOpacity = .99;
    }
    
    // Set the z-index for the anchor and the image so that the correct HREF is 
	// associated with the image displayed
	for(i=1;i<anchors.length;i++) anchors[i].style.zIndex = "0";   // clear everything
	anchors[nIndex].style.zIndex = "998";
	
	for(i=1;i<imgs.length;i++) imgs[i].style.zIndex = "0";         // clear everything
	imgs[nIndex].style.zIndex = "998";
    
    // set the array to the correct display values
    imgs[current].xOpacity = 0.0;
    imgs[nIndex].xOpacity = .99;
    
    // reset the "current" pointer to reflect the image now displayed
    current = nIndex;

    // restart the timer on the fade function
    iTimeoutID = setTimeout(so_xfade, myDelayArray[current]);
}

function setLeftShifterUp ()
{
     document.getElementById("slideshow_LeftShiftImageDown").style.visibility="hidden";
     document.getElementById("slideshow_LeftShiftImage").style.visibility="visible";
}

function setRightShifterUp ()
{
    document.getElementById("slideshow_RightShiftImageDown").style.visibility="hidden";
    document.getElementById("slideshow_RightShiftImage").style.visibility="visible";

    return;                       
}



