//boolean: whether or not we are fading
var doingFade = true;

//use as a handle on the timeout loop
var timeout;

//variable to hold global opactiy
var globalOpacity = 0;

var imageNames = "a1/images/acvty_img.jpg a2/images/acvty_img.jpg a3/images/acvty_img.jpg a4/images/acvty_img.jpg".split(" ");

//next image index to be faded
var indexValue = Math.floor(Math.random()*imageNames.length-1)+1;

//variables for container/controller/target
var containerID = 'testcontainer';
var targetID    = 'target';
var container   = null;
var target      = null;

function startPhoto() {
    if (!document.getElementById) return;

    //set references to container && target ** a href
    container = document.getElementById(containerID);
    target = document.getElementById(targetID);

    setalpha(0);
    timeout = window.setTimeout("nextPhoto()", 4000);
}

function nextPhoto() {
	if (!document.getElementById) return;

    // only one transition at a time, please
	clearTimeout(timeout);

    //flip image and container
    switchContainer();

    //load next image
    target.src = imageNames[indexValue];

    //get new index differnet from current
    var newvalue = indexValue;
    while(newvalue == indexValue)
    {
        newvalue = Math.floor(Math.random()*imageNames.length);
    }
    indexValue = newvalue;

    //fade image in
	timeout = window.setTimeout("reveal('0')", 500);
}

function switchContainer() {
    if (!document.getElementById) return;

    //make container background current image
	container.style.backgroundImage = 'url(' + target.src + ')';

    //make image 0
    setalpha(0) ;
}

function reveal(opacity) {
	if (!document.getElementById) return;
	
	if (document.getElementById && opacity <= 100 ) {
		setalpha(opacity);
		
		opacity += 10;
		
		//store globally fo restart
		globalOpacity = opacity;
		
		//fade next step
		timeout = window.setTimeout("reveal("+opacity+")", 100);
	} else {
		//we are done .. load next photo in 5 seconds
		timeout = window.setTimeout("nextPhoto()", 5000);
		
		switchContainer();
	}
}

/** a derivation (or mangling) of a script from Scott Andrew (www.scottandrew.com) **/
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, true);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

function setalpha(opacity) {
	if (document.getElementById ) {
		//fade next step based onbrowser compatibility
		if (target.style.MozOpacity!=null) {
			target.style.MozOpacity = (opacity/100) - 0.001; //patrick h. lauke (http://www.splintered.co.uk/) workaround for Mozilla 'flash' bug - I _never_ would have caught that
		} else if (target.style.opacity!=null) {
			target.style.opacity = opacity/100;
		} else if (target.style.filter!=null) {
			target.style.filter = "alpha(opacity=" + opacity + ")";
		} else if (target.style.KhtmlOpacity!=null) {
			target.style.KhtmlOpacity = opacity/100;
		}
	}
}  
