// Declaratie
var elem;
var zIndex;
var running_id;
var output_html;
var orginele_zindex = 1;
var rotatie_teller = 0;
var rotatie_totaal = 0;

// Kleine plugin die de hoogte zIndex bepaald
$(function(){
	var max_zindex = Math.max.apply(null,$.map($('body > *'), function(e,n){
		if ($(e).css('position')=='absolute')
			return parseInt($(e).css('z-index'))||1 ;
		})
	);
});

// Functie die de diavoorstelling initialiseerd
function fadeshow(bulk_afbeeldingen, width, height, borderwidth, delay, showInhoudsopgave, align_right) {

	// Initialisatie
	rotatie_teller = 1;
	running_id = Math.round(Math.random() * 10000000);
	rotatie_totaal = bulk_afbeeldingen.length;
	zIndex = orginele_zindex;

	// Inhoudsopgave tonen?
	if (typeof(showInhoudsopgave) == "undefined") {
		showInhoudsopgave = false;
	}

	// Container klaarzetten
	output_html  = '<div class="containment-for-fadeshow" id="containment-for-fadeshow-'+running_id+'" style="position: relative; overflow: hidden; width: '+width+'px; height: '+height+'px;">';

	for (index in bulk_afbeeldingen) {

		// Afbeelding preloaden
		var image = new Image;
		image.src = bulk_afbeeldingen[index][0];

		// Afbeeldingen invoeren
		output_html += '<img src="'+bulk_afbeeldingen[index][0]+'" alt="" class="fadeshow-rotating-image" counter="'+rotatie_teller+'" style="top: 0; width: '+width+'px; height: '+height+'px; z-index: '+zIndex+'; position: absolute;" />';

		// Teller ophogen
		rotatie_teller++;
	}

	// Container sluiten
	output_html += '</div>';

	// Inhoudsopgave maken indien nodig
	if (showInhoudsopgave) {
		output_html += '<div class="inhoudsopgave" id="fadeshow-inhoudsopgave">';
		output_html += '<div class="uitlijning">';
		var teller = 1;
		var class_html = '';
		for (index in bulk_afbeeldingen) {
			class_html = '';
			if (teller == 1) {
				class_html = ' class="actief"';
			}
			output_html += '	<div'+class_html+'>'+teller+'</div>';
			teller++;
		}
		output_html += '</div>';
		output_html += '</div>';
	}

	// Container wegschrijven naar browser
	document.write(output_html);

	// Teller verlagen (staat 1 te hoog)
	rotatie_teller--;

	// Instellen van alpha kanaal
	$('#containment-for-fadeshow-'+running_id+' img').hide();

	// Start van loop
	roteerFadeshowItem(showInhoudsopgave);
	window.setInterval(function(){
		roteerFadeshowItem(showInhoudsopgave);
	}, delay);
}

// Functie die "fade"
function roteerFadeshowItem(showInhoudsopgave) {
	$('#containment-for-fadeshow-'+running_id+' img[counter='+rotatie_teller+']').fadeOut("slow");
	rotatie_teller++;
	if (rotatie_teller > rotatie_totaal) {
		$('#containment-for-fadeshow-'+running_id+' img').css('z-index', orginele_zindex);
		zIndex = orginele_zindex;
		rotatie_teller = 1;
	}
	// Indien nodig inhoudsopgave mee laten sliden
	if (showInhoudsopgave) {
		var actief_nummer = $('#fadeshow-inhoudsopgave div.actief');
		$(actief_nummer).removeClass('actief');
		if ($(actief_nummer).next().length > 0) {
			$(actief_nummer).next().addClass('actief');
		} else {
			$('#fadeshow-inhoudsopgave div:first').addClass('actief');
		}
	}
	var elem = $('#containment-for-fadeshow-'+running_id+' img[counter='+rotatie_teller+']');
	$(elem).css('z-index',parseInt(zIndex) + 1);
	$(elem).fadeIn("slow");
}
