nFadeDelta = 5      //the % of fading for each step
fadeSpeed = 2      //the speed, lower = faster (delay between updates)
holdtimefull = 5000 //the full visibility time
holdtimegone = 500  //the full invisibility time

//Intenal use vars
nOpacity = 0;
nImage = 0;
doFadeEffect = true;
activepanel = "fadediv"; //The currently active fading panel
activeimage = "fadeimg"; //The currently active switching image

rootpanel = "fadediv"; //The currently active root panel
destinationpanel = ""; //The panel being fadded towrds as the new root panel

altfadediv = "";

activesemidiv = null;

//does a looping fade on panels and flicks images on invisibility
function fadeImage() {
	
	if ( doFadeEffect ) {
		_fadediv = document.getElementById( activepanel );
		nOpacity = nOpacity+nFadeDelta;
		
		if ( document.getElementById && document.all ) {
			_fadediv.style.filter="alpha(opacity=0)";
			_fadediv.filters.alpha.opacity = nOpacity;
		}
		if ( document.getElementById && !document.all ) {
			_fadediv.style.MozOpacity = 0 + "%";
			_fadediv.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;
			_fadeimg = document.getElementById( activeimage );
			_fadeimg.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 ) ) {
			doFadeEffect = false;
			if ( nFadeDelta > 0 )
				nFadeDelta=-nFadeDelta;
			fadeToPanel( targetpanel );
		}
	}
}

//
function fadeFullPanel( targetpanel ) {
	if ( !! document.getElementById ) {
		if ( !! document.getElementById( targetpanel ) ) {
			document.getElementById( targetpanel ).style.filter = "";
			fadePanel( targetpanel );
		}
	}
}

function fadeSemiPanel( replacementpanel, fadepanel, contentpanel, newimage, newfadeimgs ) {
	if ( !! document.getElementById ) {
		if ( !! document.getElementById( replacementpanel ) ) {
			
			
			if ( activesemidiv != null ) {
				activesemidiv.style.display = "none";
			}
			_contentpanel = document.getElementById( contentpanel )
			_contentpanel.style.display = "block";
			activesemidiv = _contentpanel;
			
			if ( activepanel != fadepanel ) {
				altfadediv = fadepanel;
				fadeimages = newfadeimgs;
			}
			
			//set the fading image pointer to the new image
			activeimage = newimage;
			_activeimage = document.getElementById( activeimage )
			
			//check what image is next in the slide rotation
			nImage = 0;
			for ( i = 0; i < newfadeimgs.length; i++ ) {
				if ( _activeimage.src.indexOf( newfadeimgs[i] ) > 0 ) {
					nImage = i;
					break;
				}
			}
			
			// Hvis du ikke er på dette panel, eller på vej til det...
			if ( rootpanel != replacementpanel && destinationpanel != replacementpanel ) {
				destinationpanel = replacementpanel;
				fadePanel( replacementpanel );
			}
		}
	}
}

document.onreadystatechange=readystatehandle;

function readystatehandle()
{
	if (document.readyState=="complete")
	{
		if ( document.getElementById ) {
			preloadimg = new Array();
			for ( i = 0; i < fadeimages.length; i++ ) {
				preloadimg[i] = new Image();
				preloadimg[i].src = fadeimages[i];
			}
			setTimeout( "fadeImage();", 125 );
		}
	}
}