var newpic = new Image();
fadeout = 0;
fading = 0;
nextfad = "";
nextout = 0;
copacity = 100;
fadestep = 3;
fadespeed = 0.5;
if (navigator.appName.indexOf("Explorer")  != -1) {
	fadespeed = 1;
	fadestep = 9;
}


function itr_init_headerimage(){
  newpic = document.getElementById('newpic');
  setOpacity(newpic,0)
}

function itr_swap_headerimage(image){
	fadeIn(image);
}

function itr_restore_headerimage(){
	fadeOut();
 
}


//set the opacity of an element to a specified value
function setOpacity(obj, o) {
		if (o > 100) o = 100;
		if (o < 0) o = 0;
    obj.style.opacity = (o / 100);
    obj.style.MozOpacity = (o / 100);
    obj.style.KhtmlOpacity = (o / 100);
    obj.style.filter = 'alpha(opacity=' + o + ')';
    copacity = o;
}

function fadeIn(img) {
	if (img == origimg) { return true; }
	if (newpic.src == img) { return true;	}

	if (fading == 1) {
		nextfad = img;
		nextout = 0;
	} else {
		newpic.src = img;
		fading = 1;
		fadeout = 0;
		setOpacity(newpic,0);
		continueFadeImage(newpic,1,fadespeed, -1);
	}
}

function fadeOut() {
	//document.getElementById('maincontent').innerHTML += 'fadeout<br>';
	if (fading == 1) {
		nextout = 1;
		nextfad = origimg;
	} else {
		fading = 1;
		fadeout = 1;
		continueFadeImage(newpic,copacity,0.1, -1);
	}
}

//set an increased opacity and check if the image is done blending
function continueFadeImage(image, opacity, speed, pause, caption) {
	if (fadeout == 0) {
		opacity = opacity + fadestep;
	} else {
		opacity = opacity - fadestep;
	}
	setOpacity(image,opacity);
	if (opacity < 105 && opacity > 0) {
	  	setTimeout(function() {continueFadeImage(image, opacity, speed, pause, caption)}, speed);
	  
	} else {
		fading=0;
		if (nextfad != '') {
			tmp = nextfad;
			nextfad = '';
			if (fadeout == 0) {
				if (nextout == 0) {
					fadeOut();
					nextfad = tmp;
					nextout = 0;
				} else {
					fadeOut();
				}
			} else {
				if (nextout == 1) {
					fadeOut();
				} else {
					fadeIn(tmp);
				}
			}
		}
	}
}


//set the opacity to a new value and continue the fading
function fadeImage(image, opacity, speed, pause, caption) {
    if(opacity == 0 && pause == -1) opacity = 100;
    setOpacity(image,opacity);
    continueFadeImage(image, opacity, speed, pause, caption);
}
