var intervals = new Array()
var opacities = new Array()
var imgs = new Array()
var names = new Array()
var descr = new Array()
var fadeSpeed = 20
var hoverOn = false

function setimgopacity(name, value){ //Sets the opacity per the passed in value setting (0 to 1 and in between)
	var targetobject=imgs[name]
	var target2=names[name]
	var target3=descr[name]

	if (targetobject.filters && targetobject.filters[0]){ //IE syntax
		if (typeof targetobject.filters[0].opacity=="number"){ //IE6
			targetobject.filters[0].opacity=value*100
			target2.filters[0].opacity=value*100
			target3.filters[0].opacity=value*100
		}
		else {//IE 5.5
			targetobject.style.filter="alpha(opacity="+value*100+")"
			target2.style.filter="alpha(opacity="+value*100+")"
			target3.style.filter="alpha(opacity="+value*100+")"
		}
		}
	else if (typeof targetobject.style.MozOpacity!="undefined") {//Old Mozilla syntax
		targetobject.style.MozOpacity=value
		target2.style.MozOpacity=value
		target3.style.MozOpacity=value
	}
	else if (typeof targetobject.style.opacity!="undefined") { //Standard opacity syntax
		targetobject.style.opacity=value
		target2.style.opacity=value
		target3.style.opacity=value
	}
	else {//Non of the above, stop opacity animation
		//stopanimation()
		}
	}

function opacityanimation(which, dir){ //Gradually increase opacity function
	if (dir == "in"){
		opacities[which]+=1/fadeSpeed
	}
	else{
		opacities[which]-=1/fadeSpeed
	}

	if (opacities[which] > 1){
		opacities[which] = 1
		this.stopanimation(which)
	}
	if (opacities[which] < 0){
		opacities[which] = 0
		this.stopanimation(which)
	}
	this.setimgopacity(which, opacities[which])
}


function stopanimation(which){
	if (typeof intervals[which]!="undefined")
		clearInterval(intervals[which])
		intervals[which] = null
}

function init(){	
	imgs['matze'] = document.getElementById('matzeGlow')
	imgs['georg'] = document.getElementById('georgGlow')
	imgs['simon'] = document.getElementById('simonGlow')
	
	names['matze'] = document.getElementById('matzeName')
	names['georg'] = document.getElementById('georgName')
	names['simon'] = document.getElementById('simonName')

	descr['matze'] = document.getElementById('matzeDescr')
	descr['georg'] = document.getElementById('georgDescr')
	descr['simon'] = document.getElementById('simonDescr')

	opacities['matze'] = 0
	opacities['georg'] = 0
	opacities['simon'] = 0

	new countdown(22);
}

function fadeIn(which){
	stopanimation(which)
	var str = "opacityanimation(\""+which+"\",\"in\")"
	intervals[which] = setInterval(str, fadeSpeed)
}

function fadeOut(which){
	stopanimation(which)
	var str = "opacityanimation(\""+which+"\",\"out\")"
	intervals[which] = setInterval(str, fadeSpeed)	
}

Function.prototype.Timer = function (interval, calls, onend) {
			  var count = 0
			  var payloadFunction = this
			  var startTime = new Date()
			  var callbackFunction = function () {
				return payloadFunction(startTime, count)
			  }
			  var endFunction = function () {
				if (onend) {
				  onend(startTime, count, calls)
				}
			  }
			  var timerFunction =  function () {
				count++
				if (count < calls && callbackFunction() != false) {
				  window.setTimeout(timerFunction, interval)
				} else {
				  endFunction()
				}
			  }
			  timerFunction()
			}

		function countdown (seconds) {
		  //var element = document.getElementById(target);
		  var calculateAndShow = function () {
				switch (seconds){
					case 14: fadeIn('matze');
						break;
					case 12: fadeIn('simon');
						break;
					case 10: fadeIn('georg');
							fadeOut('matze');
						break;
					case 8: fadeOut('simon');
						break;
					case 6: fadeOut('georg');
						break;
				}
			 
			if (seconds >= 0) {
			  seconds--;
			} else {
			  return false;
			}
		  };

			var completed = function () {
				descr['matze'].style.visibility = 'visible';
				descr['georg'].style.visibility = 'visible';
				descr['simon'].style.visibility = 'visible';
				hoverOn = true
			//element.innerHTML = "<strong>Liftoff!<\/strong>";
		  };

		  calculateAndShow.Timer(200, Infinity, completed);
		}

function toggleImage(){
	var div = document.getElementById('startDiv')
	if (div.style.visibility == "hidden"){
		div.style.visibility = "visible";
	} else{
		div.style.visibility = "hidden";
	}
	//alert('hepp');
}
		