// 'images' must be defined as a global array in the HTML
var index = 0;
var fading = 1;
var opacity = 0;

function rotateBanner(){
	var obj = document.getElementById("banner_image");
	if (fading==0){			// FADE IN
		if(opacity<100) {	// If image is not opaque yet
	    	opacity+=2;		// increase opacity
	    	setOpacity(obj, opacity);
	    	setTimeout("rotateBanner()",15);
		}
		else{				// once image is opaque
			opacity=100;	// reset the opacity to 100%
			fading=1;		// fading out next
			if ((typeof images == "undefined") || (images.length <= 1)) delete rotateBanner; // if there's only one image, just leave it there
			else setTimeout("rotateBanner()",8000);  // otherwise hold the image onscreen for a bit, then rotate
		}
	}
	else if (fading==1){	// FADE OUT
		if(opacity>0) {		// If image is not invisible yet
	    	opacity-=2;		// decrease opacity
	    	setOpacity(obj, opacity);
	    	setTimeout("rotateBanner()",15);
		}
		else{				// once image is invisible
			opacity=0;		// reset the opacity to 0%
			fading=0;		// fading in next
			
			// then swap the image
			if ((typeof images != "undefined") && (typeof images[0] != "undefined")) obj.style.backgroundImage = "url(/images/header_bg/"+images[index]+".jpg)";
			else obj.style.backgroundImage = "url(/images/header_bg/rooftops.jpg)";
			// increment the image index
			index++;
			if ((typeof images != "undefined") && (index >= images.length)) index = 0;
			// preload the next image
			if ((typeof images != "undefined") && (typeof images[0] != "undefined")) {
				var pic = new Image(650,120);
				pic.src ="/images/header_bg/"+images[index]+".jpg";
			}
			// call the rotate function again
			rotateBanner();
		}
	}
	else fading=0; // reset to "fading in" in case of an invalid value
}

function preloadTabs() {  // this function was included here to avoid having to link another file
	if (document.images) {
		var pic0 = new Image(1,21);
		pic0.src ="/images/header/highlight.gif";
		var pic1 = new Image(88,28);
		pic1.src ="/images/header/selected_home.jpg";
		var pic2 = new Image(108,28);
		pic2.src ="/images/header/selected_about.jpg";
		var pic3 = new Image(152,28);
		pic3.src ="/images/header/selected_depts.jpg";
		var pic4 = new Image(112,28);
		pic4.src ="/images/header/selected_programs.jpg";
		var pic5 = new Image(93,28);
		pic5.src ="/images/header/selected_events.jpg";
		var pic6 = new Image(104,28);
		pic6.src ="/images/header/selected_partners.jpg";
		var pic7 = new Image(99,28);
		pic7.src ="/images/header/selected_contact.jpg";
	}
}

/*** 	This function was borrowed from Richard Rutter
		at http://clagnut.com/sandbox/imagefades/		***/
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}