/*
Image rotator applet
By: PaxTech Multimedia
All rights reserved.
Portions by brainerror.net
*/
// array of images loaded via xml
var imageArray = [];
// current image for looping
var currentImage = -1;
// time (in ms) it takes to fade an image in or out
var fadeTime = 500;
// time (in ms) it waits before fading out and displaying the next image
var rotationTime = 5000;
var divID = "rotating_icons";

/*
init function
description: 
	loads the xml file and begins the slideshow
parameters:
	url - url of xml file
*/
function init(url) {	
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}	
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            for (var i=0; i<req.responseXML.getElementsByTagName("image").length; i++) {
				imageArray.push(req.responseXML.getElementsByTagName("image")[i].childNodes[0].nodeValue);
			}
			
			if (imageArray.length > 0) {
				replaceImage();
			}
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

/*
replaceImage function
description:
	replaces the existing image with the next one in line, and fades it in
*/
function replaceImage() {
	if (document.getElementById("rotate_image") != null) {
		document.getElementById(divID).removeChild(document.getElementById("rotate_image"));
	}

	currentImage++;
	if (currentImage >= imageArray.length) {		
		currentImage = 0;
	}
	var im = new Image();
	im.id = "rotate_image";
	im.name = "rotate_image";	
	im.onload = fadeIn;
	im.src = imageArray[currentImage];
	
	document.getElementById(divID).appendChild(im);
	
	changeOpac(0, "rotate_image");
}

/*
fadeIn function
description:
	fades an image in, and begins the interval for fading it out
*/
function fadeIn() {
	//alert("fading in "+imageArray[currentImage])
	opacity("rotate_image", 0, 100, fadeTime);	
	setTimeout("setTimeout('fadeOut()',rotationTime)", fadeTime);
}

/*
fadeOut function
description:
	fades an image out and then replaces it with the next image
*/
function fadeOut() {
	opacity("rotate_image", 100, 0, fadeTime);
	setTimeout("replaceImage()", fadeTime);
}

/*
opacity function
description:
	sets timers to change the opacity of an element
parameters:
	id - document id of element
	opacStart - % opacity to start at
	opacEnd - % opacity to end at
	millisec - time in ms it takes to change opacity
*/
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }		
    }	
}

/*
changeOpac function
description:
	changes the opacity of an element
parameters:
	opacity - the opacity to set
	id - document id of element
*/
//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 