/* original by MediaWiki project - http://www.mediawiki.org/wiki/Extension:Javascript_Slideshow */

include("portal/scripts/scriptaculous/prototype.js");
include("portal/scripts/scriptaculous/scriptaculous.js?load=effects");


var slideshowDivs = [];
var currentDivIndexes = [];
 
function getChildDivs(id) {
	var parent = document.getElementById(id);
	var spacer = document.getElementById(id+"Spacer");
	if(!spacer || !parent) return [];
	var childDivs = [];
	var i;
	for (i=0; i < parent.childNodes.length; i++) {
		var child = parent.childNodes[i];
		if (child.tagName == 'DIV') {
			childDivs[childDivs.length] = child;
		}
	}
	return childDivs;
}
 
function getInitialDivIndex(id, sequence) {
	var index = -1;
	if (sequence == 'forward') {
	  index = 0;
	} else if (sequence == 'backward') {
	  index = (slideshowDivs[id].length)-1;
	} else if (sequence == 'random') {
		index = Math.floor(Math.random()*slideshowDivs[id].length);
	}
	show(slideshowDivs[id][index]);
	return index;
}
 
function getNextDivIndex(id, sequence) {
	var index = -1;
	if (sequence == 'forward') {
		index = currentDivIndexes[id] + 1;
		if (index == slideshowDivs[id].length) {
			index = 0;
		}
	} else if (sequence == 'backward') {
		index = currentDivIndexes[id] - 1;
		if (index == -1) {
			index = slideshowDivs[id].length - 1;
		}
	} else if (sequence == 'random') {
		index = currentDivIndexes[id];
		if (slideshowDivs[id].length > 1) {
			while (index == currentDivIndexes[id]) {
				index = Math.floor(Math.random()*slideshowDivs[id].length);
			}
		}
	}
 
	return index;
}
 
function getNode(id, index) {
	return slideshowDivs[id][index];
}
 
function doTransition(currentNode, newNode, transition) {
	if (transition == 'cut') {
		hide(currentNode);
		show(newNode);
	} else if (transition == 'fade') {
		currentNode.style.zIndex = 2;
		newNode.style.zIndex = 1;
		setOpacity(newNode,1);
		var fadeTransition = new Effect.Opacity(currentNode, {from: 1.0, to: 0.0, duration: 1, afterFinish: function() { currentNode.style.zIndex = 0; }});	//simplified
	} else if (transition == 'blindDown') {
		currentNode.style.zIndex = 1;
		newNode.style.zIndex = 2;
	 	new Effect.BlindDown(newNode, {duration: 1.0});
	}
}

 
function slideshow(id, sequence, transition) {
	var newIndex = getNextDivIndex(id, sequence);
	doTransition(getNode(id, currentDivIndexes[id]), getNode(id, newIndex), transition);
	currentDivIndexes[id] = newIndex;
}
 

function startSlideshow(id, refresh, sequence, transition) {
	slideshowDivs[id] = getChildDivs(id);
	if (slideshowDivs[id].length > 1) {
		currentDivIndexes[id] = getInitialDivIndex(id, sequence);
		setInterval("slideshow('" + id + "', '" + sequence + "', '" + transition + "');", refresh);
		$(id).className += " slideShow";
		$(id+"Spacer").className += " slideShow";
	}
}
 

function hide(node) {
   if (node != null) {
		setOpacity(node, 0);
		node.style.zIndex = 0;
	}
}

 
function show(node) {
   if (node != null) {
		setOpacity(node, 1);
		node.style.zIndex = 1;
	}
}
 
function setOpacity(node, value) {
   if (node != null) {
		node.style.opacity = value;
		node.style.filter = 'alpha(opacity = ' + (value * 100) + ')';
	}
}


function $(id){ return document.getElementById(id); }

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
} 

addEvent(window, "load", function(){ startSlideshow("imageHeaderImages",10000,"forward","fade"); }, false);

function include_dom(filename){
	var js=document.createElement("script");
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', filename);
	var head=document.getElementsByTagName('head').item(0);
	head.appendChild(js);
}
function include(script_filename) {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="' + script_filename + '">');
    document.write('</' + 'script' + '>');
}


