var globalOpacity = 0;
var container   = null;
var target      = null;
var control     = null;
var imageRotateTime = null;

function startPhoto() {
	if (!document.getElementById) return;
	container = document.getElementById('imgcontainer');
	target = document.getElementById('target');
	setalpha(0);
	timeout = window.setTimeout("nextPhoto()", imageRotateTime );
}

function nextPhoto(){
	clearTimeout(timeout); // only one transition at a time, please
	switchContainer();//flip image and container
	target.src = 'media/images/divisions/'+imageNames[indexValue]; //load next image
	var newvalue = indexValue; //get new index differnet from current
	while(newvalue == indexValue) {
		newvalue = Math.floor(Math.random()*imageNames.length);
	}
	indexValue = newvalue;
	timeout = window.setTimeout("reveal('0')", 250); //fade image in
}


function switchContainer(){
	//make container background current image
	container.style.backgroundImage = 'url(' + target.src + ')';
	//make image 0
	setalpha(0) ;
}

function reveal(opacity){
	if (opacity <= 100){
		setalpha(opacity);
		opacity += 10;
		globalOpacity = opacity; //store globally fo restart
		timeout = window.setTimeout("reveal("+opacity+")", 100); //fade next step
	}else{
		//we are done .. load next photo in 5 seconds
		timeout = window.setTimeout("nextPhoto()", imageRotateTime );
		switchContainer();
	}
}

function setalpha(opacity){
	//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;
	}
}

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;
	}
}