var globalAutoFade		= false;
var globalAutoFadeDelay	= 3000;
var globalAutoFadeTimer	= null;
var globalFadeDelay		= 2000;
var globalFadePause		= false;
var globalCurrentFader	= 0;
var globalControl		= 'play';

function initFader (doAutoFade)
{
	$("div[id^=fader_]").each (function (i) {
		if (i == 0 && $.browser.msie) {
			$(this).css ('opacity', 0.999);			
		}
										 
		if (i > 0) {
			$(this).css ('opacity', 0);
		}

		$("#fader_to_" + i).bind ('click', i, function (e) {															 
			fader ('to', e.data);																	 
		});
	});
	
	$("#fader_next").bind ('click', function (e) {
		fader ('next', 0);												  
	});
	
	$("#fader_prev").bind ('click', function (e) {
		fader ('prev', 0);					  
	});		
	
	$("#fader_control").bind ('click', function (e) {												 
		faderControl ();		
	});	
	
	globalAutoFade = doAutoFade;
	
	if (globalAutoFade) {
		globalAutoFadeTimer = setTimeout ("fader ('next', 0)", globalAutoFadeDelay);
	}
}

function fader (type, id)
{
	// Timeout löschen?
	if (globalAutoFade) {
		clearTimeout (globalAutoFadeTimer);		
	}	
	
	// Nächstes Element ermitteln
	var nextId = 0;
	
	switch (type) {
		case 'to':
			nextId = id;
			break;
		case 'next':
			nextId = globalCurrentFader + 1;
			break;
		case 'prev':
			nextId = globalCurrentFader - 1;
			break;
	}
	
	if (nextId < 0) {
		nextId = $("div[id^=fader_]").length - 1;		
	}	
	
	if ($("#fader_" + nextId).length == 0) {
		nextId = 0;		
	}

	// Aktuelles Element ausblenden
	$("#fader_" + globalCurrentFader).animate ({opacity: 0.0}, globalFadeDelay, '', function () {
																									  
		$(this).hide ();
	});
	
	// Eventuell Pagination weitersetzen
	if ($("#fader_to_" + nextId).length) {
		$("a[id^=fader_to]").removeClass ('current');		
		$("#fader_to_" + nextId).addClass ('current');
	}
	
	// Aktuellen Fader festhalten
	globalCurrentFader = nextId;	
	
	// Nächstes Element einblenden
	$("#fader_" + nextId).show ();
	
	$("#fader_" + nextId).animate ({opacity: 1.0}, globalFadeDelay, function () {
		// Neuen Timeout setzen?
		if (globalAutoFade && globalControl == 'play' && !globalFadePause) {
			globalAutoFadeTimer = setTimeout ("fader ('next', 0)", globalAutoFadeDelay);		
		}																							  
	});
}

function faderControl ()
{
	if (globalAutoFade) {
		if (globalControl == 'play') {
			clearTimeout (globalAutoFadeTimer);			
			
			globalControl = 'stop';
			
			$("#fader_control").replaceWith ('<a href="javascript:;" id="fader_control" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'play\',\'\',\'../_ref/images/elem/elem_play_hover.gif\',1)"><img src="../_ref/images/elem/elem_play.gif" alt="Play" name="play" width="5" height="9" border="0" id="play" /></a>');						
		} else if (globalControl == 'stop') {
			globalAutoFadeTimer = setTimeout ("fader ('next', 0)", globalAutoFadeDelay);
			
			globalControl = 'play';
			
			$("#fader_control").replaceWith ('<a href="javascript:;" id="fader_control" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'stop\',\'\',\'../_ref/images/elem/elem_pause-hover.gif\',1)"><img src="../_ref/images/elem/elem_pause.gif" alt="Stop" name="stop" width="5" height="9" border="0" id="stop" /></a>');					
		}
		
		$("#fader_control").unbind ();
		$("#fader_control").bind ('click', function (e) {												 
			faderControl ();		
		});			
	}
}