var gallery_id = 'crossfade_gallery';

//set the fader to start on load
window.onload = init_fader;


function init_fader() {

	setTimeout('fader_loop(null)',10000);

}

function fader_loop(current_image) {

	if (document.getElementById("crossfade_gallery_element_0") == null)
		return;

	if (current_image == null)
		current_image = 0;

	var number_images = fader_count_images();

	if (number_images < 2)
		return;

	var next_image = parseInt(current_image) + 1;
	if (next_image == number_images)
		next_image = 0;

	crossfade(current_image,next_image);

	setTimeout('fader_loop(\'' + next_image  + '\' )',8000);

}


function crossfade(last, next) {

	id_last = "crossfade_gallery_element_" + last;
	id_next = "crossfade_gallery_element_" + next;

	window.setTimeout("crossfade_step('" + id_last + "', '" + id_next + "', '" + 20 + "')", 0);
	window.setTimeout("crossfade_step('" + id_last + "', '" + id_next + "', '" + 40 + "')", 150);
	window.setTimeout("crossfade_step('" + id_last + "', '" + id_next + "', '" + 80 + "')", 300);
	window.setTimeout("crossfade_step('" + id_last + "', '" + id_next + "', '" + 100 + "')", 450);

}


function crossfade_step(id_last, id_next, percent) {

	set_div_opacity(id_last, 100 - parseInt(percent));
	set_div_opacity(id_next, percent);

}


/* set opacity 0 to 100 */
function set_div_opacity(id, opacity) {

	object = document.getElementById(id);	
	object.style.opacity = opacity / 100.0;
	object.style.filter = 'alpha(opacity=' + opacity + ')';

	if (opacity == 0)
		object.style.display = "none";
	else
		object.style.display = "block";

}


// count the images
function fader_count_images() {

	var start_node = document.getElementById(gallery_id);
	var elements = fader_getElementsByClass(document, "crossfade_gallery_element", "*");  
	return elements.length;	

}


 function fader_getElementsByClass(node,searchClass,tag) {  
     var classElements = new Array();  
	 if ( node == null )  
	     node = document;  
	 if ( tag == null )  
	     tag = '*';  
     var els = node.getElementsByTagName(tag); // use "*" for all elements  
     var elsLen = els.length;  
     var pattern = new RegExp("\\b"+searchClass+"\\b");  
     for (kk = 0, rr = 0; kk < elsLen; kk++) {  
	  if ( pattern.test(els[kk].className) ) {  
	  classElements[rr] = els[kk];  
	  rr++;  
	  }  
     }  
     return classElements;  
   }  

