// Number of images to cycle through
var titleImages = 4;
// Start Image - This number must be the same as the image number set by default in the html file.
var startImage = 1;
// Array full of image sources
var imagenames = new Array();
// Build image array from titleImages
startImage++;
for(var i=1; titleImages>=i; i++){
	if(startImage>titleImages){
		startImage=1;
	}
	imagenames[i-1]="./images/title" + startImage + ".jpg";
	startImage++;
	//alert("Start Image: " + startImage + "\ni: " + i + "\nImage: " + imagenames[i-1]);
}

var pageEl = "banner";

// Set up the slideshow
var stillxfading = false;

// Speed in ms (1000 ms = 1 sec)
var slidespeed=7000;
var fadeT=20;
// Set to false for starters, since we change this in the first call
var keepplaying=true;

// IE wasn't happy with these in a function. ;-) 
var first=0;
var current=0;

// Kick it all off here
function startup(inarray){
    stillxfading = true;
    imagenames=inarray;
    imagearray=new Array();
    swappy=document.getElementById(pageEl);

    // Fade in magic
    setOpacity(swappy,100);//
    swappy.style.visibility = 'visible';
    fadeIn(pageEl,0);
	
    for (var i=0; i < imagenames.length; i ++){
		loadimg(i);
	}
	
	slide();
}

function loadimg(whichimg){ 
    // Preload the images into the object array
    var tempobj=new Image();
	
    tempobj.onload=function(){
		imagearray[whichimg]=tempobj;
    }
    tempobj.src=imagenames[whichimg];
}

// Next four are fade magic functions
function setOpacity(obj, opacity) {
    opacity = (opacity == 100)?99.999: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);

    // IE/Win
    obj.style.filter = "alpha(opacity="+opacity+")";
}

function fadeIn(objId,opacity) {
    if (document.getElementById) {
		obj = document.getElementById(objId);
	
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += fadeT;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}
		else{
			stillxfading = false;
		}
    }
}

function fadeOut(objId,opacity) {
    if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity >= 0) {
			setOpacity(obj, opacity);
			opacity -= fadeT;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
		}
		else{
			swappy.style.backgroundImage="url("+imagenames[current]+")"; 
			fadeIn(pageEl,0);
		}
    }
}

function crossfade(){
    // we don't want two fades going on at once. It gets confusing and flickers
    if(!stillxfading){
	stillxfading = true;
	fadeOut(pageEl,99.99);
    }
}


function slide(){

    if(keepplaying){
		last=imagearray.length - 1;
		current++;
	
		if (current > last){
			current=first;
		}
		if (imagearray[current] != null) crossfade(/*imagearray[current].src*/);
		setTimeout('slide()',slidespeed);
    }
}
