/*****

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

Please leave this notice intact. 

*****/

// Background color of the surrounding document, to use with the IE alpha fix.
var background_color = '#FFC969';

function xfade_attach(name) {
    window.addEventListener ?
        window.addEventListener("load",function(){xfade_init(name)},false) :
        window.attachEvent("onload",function(){xfade_init(name)});
}

// Call xfade_attach() from viewpalooza.php, with the names of the
// divs to attach.

function xfade_init(idname) {
    var images = new Array();
    var cIndex = 0;
    
	if(!document.getElementById || !document.createElement)return;
	css = document.createElement("link");
	css.setAttribute("href","");	// This may need to be set to a file.css at some point
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
    // Don't append the CSS tag, since there's no css file
	//document.getElementsByTagName("head")[0].appendChild(css);
    
    if (!document.getElementById(idname)) {
        return;
    }
    
	images = document.getElementById(idname).getElementsByTagName("img");
	for(i=1; i<images.length; i++) { 
        // Mark all images except the first as unshown
        images[i].xOpacity = 0;        
    }
	images[0].style.display = "block";
	images[0].xOpacity = .99;    

    function do_xfade() {
        cOpacity = images[cIndex].xOpacity;
        nIndex = images[cIndex+1]?cIndex+1:0;
        nOpacity = images[nIndex].xOpacity;
        cOpacity-=.05; 
        nOpacity+=.05;
        images[nIndex].style.display = "block";
        images[cIndex].xOpacity = cOpacity;
        images[nIndex].xOpacity = nOpacity;
        
        setOpacity(images[cIndex]); 
        setOpacity(images[nIndex]);
        
        if(cOpacity<=0) {
            images[cIndex].style.display = "none";
            cIndex = nIndex;
            setTimeout(do_xfade, 8000);
        } else {
            setTimeout(do_xfade, 50);
        }
        
        function setOpacity(obj) {
            var opacity;
            if(obj.xOpacity >= .99) {
                obj.xOpacity = .99;
            }
            
            obj.style.opacity = obj.xOpacity;
            obj.style.MozOpacity = obj.xOpacity;
            
            // Special handing for Internet Explorer & alpha transparency
            //
            // IE uses filters to control alpha tansparency. But, if you use the filters
            // to set alpha with transparent PNG files, you end up with ugly-looking
            // garbage. The solution is to use the gradient filter to set a background
            // color; then the alpha blending works.
            //
            // However, there's a drawback. This means that the PNG is no longer really
            // transparent, and shouldn't be placed over a non-solid background.
            // Fortunately, the images over a non-solid background don't rotate.
            //
            // See:
            // http://msdn.microsoft.com/en-us/library/ms532847(v=vs.85).aspx
            // http://stackoverflow.com/questions/1251416/png-transparency-problems-in-ie8
            if (obj.filters) {
                obj.style.background = 'transparent';
                obj.style.zoom = 1;
                obj.filters.item('DXImageTransform.Microsoft.Alpha').opacity = obj.xOpacity * 100;
                obj.filters.item('DXImageTransform.Microsoft.gradient').startColorstr = background_color;
                obj.filters.item('DXImageTransform.Microsoft.gradient').endColorstr   = background_color;
                obj.filters.item('DXImageTransform.Microsoft.Alpha').enabled = 1;
                obj.filters.item('DxImageTransform.Microsoft.gradient').enabled = 1;
            }
        }    
    }
    
    // Don't run the cross-fading if there's only one image.
    // This helps with the IE alpha transparency fix (above).
    if (images.length > 1) {
    
        // Initialize filters, for IE.
        for (i = 0; i < images.length; i++) {
            if (images[i].style.filter !== undefined) {
                var alphaValue = (i == 0) ? 100 : 0;
            
                images[i].style.filter += 'progid:DXImageTransform.Microsoft.Alpha(' + 
                    alphaValue + ')';
                images[i].style.filter += 
                    'progid:DXImageTransform.Microsoft.gradient(startColorstr=' + background_color +
                    ',endColorstr=' + background_color + ')';
            }   
        }
    
        setTimeout(do_xfade, 4000); // This starts things going and is only called once    
    }
}


