nFadeDelta = 4       //the % of fading for each step
fadeSpeed = 50       //the speed, lower = faster (sleep time between alpha updates)
holdtimefull = 5000  //the full visibility time
holdtimegone = 500   //the full invisibility time

//Intenal use vars
nOpacity = 0;
nImage = 0;
doFadeEffect = true;

activepanel = null; //The currently active fading panel
activeimage = null; //The currently active switching image

rootpanel = null; //The currently active root panel
destinationpanel = null; //The panel being fadded towards as the new root panel

altfadediv = null;

activesemidiv = null;

//does a looping fade on panels and flicks images on invisibility
function fadeImage() {
	
	if ( doFadeEffect ) {
		nOpacity = nOpacity+nFadeDelta;
		
		if ( document.getElementById && document.all ) {
			activepanel.style.filter="alpha(opacity=0)";
			activepanel.filters.alpha.opacity = nOpacity;
		}
		if ( document.getElementById && !document.all ) {
			activepanel.style.MozOpacity = 0 + "%";
			activepanel.style.MozOpacity = nOpacity + "%";
		}
		
		if ( nOpacity >= 100 ) {
			if (fadeimages.length > 1) {
				nFadeDelta=-nFadeDelta;
				setTimeout( "fadeImage()", holdtimefull );
			}
		} else if ( nOpacity <= 0 ) {
			nImage++;
			if (nImage >= fadeimages.length) nImage = 0;
			//activepanel = document.getElementById( activeimage );
			activeimage.src = fadeimages[nImage]
			nFadeDelta=-nFadeDelta;
			setTimeout( "fadeImage()", holdtimegone );
		} else {
			setTimeout( "fadeImage()", fadeSpeed );
		}
	}
	
}

//does onetime fadeout on panel, switches to targetpanel and calls fadeDone()
function fadeToPanel(targetpanel) {
	
	if ( activepanel != targetpanel ) {
		
		_activepanel = document.getElementById( activepanel );
		nOpacity = nOpacity+nFadeDelta;
		
		if ( document.getElementById && document.all ) {
			_activepanel.style.filter="alpha(opacity=0)";
			_activepanel.filters.alpha.opacity = nOpacity;
		}
		if ( document.getElementById && !document.all ) {
			_activepanel.style.MozOpacity = 0 + "%";
			_activepanel.style.MozOpacity = nOpacity + "%";
		}
		
		if ( nOpacity <= 0 && activepanel != targetpanel ) {
			//opacity hits zero on old panel
			_rootpanel = document.getElementById( rootpanel );	
			_rootpanel.style.display = "none";	
			
			rootpanel = targetpanel
			destinationpanel = ""
			
			activepanel = targetpanel;
			_activepanel = document.getElementById( activepanel );
			_activepanel.style.display = "block";
			
			nFadeDelta=-nFadeDelta;
			setTimeout( "fadeDone()", fadeSpeed );
		} else {
			setTimeout( "fadeToPanel(\""+targetpanel+"\")", fadeSpeed );
		}

	}
	
}

//if alternative panel specified, resets the fade function for the new panel
function fadeDone() {
	if ( altfadediv != "" ) {
		doFadeEffect = true;
		activepanel = altfadediv;
		altfadediv = "";
		nOpacity = 0;
		
		setTimeout( "fadeImage()", holdtimegone );
	}	
}

//abort fadeloop, and calls fade to target panel function
function fadePanel( _targetpanel ) {
	if ( !! document.getElementById ) {
		if ( !! document.getElementById( _targetpanel ) ) {
			targetpanel = document.getElementById( _targetpanel );
			doFadeEffect = false;
			if ( nFadeDelta > 0 )
				nFadeDelta=-nFadeDelta;
			fadeToPanel( targetpanel );
		}
	}
}

function fadeSemiPanel( _contentpanel,  newfadeimgs ) {
	if ( activesemidiv != null ) {
		activesemidiv.style.display = "none";
	}
	
	contentpanel = document.getElementById( _contentpanel )
	contentpanel.style.display = "block";
	activesemidiv = contentpanel;
}


//init page function preload and then starts fading loop
function pageInit() {
	
	activepanel = document.getElementById("fadediv");
	activeimage = document.getElementById("fadeimg");
	rootpanel = document.getElementById("fadediv")
	
	if ( document.getElementById ) {
		preloadimg = new Array();
		for ( i = 0; i < fadeimages.length; i++ ) {
			preloadimg[i] = new Image();
			preloadimg[i].src = fadeimages[i];
		}
		fadeImage();
	}
}