function xfade(parent_div_id, pause_time, fade_time) {
	var imgs = [];
	var current_div_num=0;

	/* rotation */
	function perform_fade() {
		var last_div_num = current_div_num;
		current_div_num = (current_div_num + 1) % imgs.length;
		new Effect.Parallel([
			new Effect.Fade(imgs[last_div_num], { 'sync': true, 'from': 1.0, 'to': 0.0 }),
			new Effect.Appear(imgs[current_div_num], { 'sync': true, 'from': 0.0, 'to': 1.0 }) ],
         { 'duration': fade_time});
	}

	/* setup */
	function start_me_up() {
		var parent_div = $(parent_div_id);
		if (!parent_div) return false;
		var parent_width = parent_div.scrollWidth;
		var parent_height = parent_div.scrollHeight;
		var parent_aspect_ratio = parent_width/parent_height;
		/* Get the images to rotate */
		imgs = $$('div#' + parent_div.id + ' img.fade-item');
		if (imgs.length == 0) return false;
		/* Size and center each image */
		for (var i=0 ; i < imgs.length ; i++) {
			var img = imgs[i];
			var img_width = img.width;
			var img_height = img.height;
			/* If the image has never been visible, IE will not set or show the width/height. */
			if (img_width==0) {
				var img_proxy = new Image();
				img_proxy.src = img.src;
				img_width = img_proxy.width;
				img_height = img_proxy.height;
			}
			var img_aspect_ratio = img_width/img_height;
			var scale_factor = 1;
			if (img_width > parent_width || img_height > parent_height ||
						(img_width < parent_width && img_height < parent_height)) {
				/* scale image down or up */
				if (img_aspect_ratio > parent_aspect_ratio) {
					scale_factor = parent_width/img_width;
				} else {
					scale_factor = parent_height/img_height;
				}
			}
			if (img_width == 0 && false) {
				alert("Scale factor of 0:\n"+
							"parent width " + parent_width + "px\n" +
							"parent height: " + parent_height + "px\n" +
							"img width " + img_width + "px\n" +
							"img height: " + img_height + "px\n");

			} else if (img_width > 0 && img_height > 0 && scale_factor != 1) {
				img.width = Math.round(img_width * scale_factor);
				img.height = Math.round(img_height * scale_factor);
				if (img.width < parent_width) {
					img.style.left = Math.round((parent_width-Math.round(img_width * scale_factor))/2).toString()+'px';
				}
				if (img.height < parent_height) {
					img.style.top = Math.round((parent_height-Math.round(img_height * scale_factor))/2).toString()+'px';
				}
			}
		}
		setInterval(perform_fade,pause_time*1000);
		Effect.Appear(imgs[current_div_num], { 'duration': fade_time, 'from': 0.0, 'to': 1.0 });
	}

	add_body_onload(start_me_up);
}
