var tx_akreferences_colors = ['#b4ff00', '#FFA800', '#9C00FF', '#00CCFF']


function tx_akreferences_tagswitch(tagId, ref, status, allTags) {

	if(status == undefined) {	// status is not given, so simply change it
		tx_akreferences_tagstatus[tagId] = !tx_akreferences_tagstatus[tagId];
	}
	else {	// status is given, set it
		tx_akreferences_tagstatus[tagId] = status;
	}
	
		// change CSS
	if(tx_akreferences_tagstatus[tagId]) {
		if(!allTags) {
				// rand color
			randCol = tx_akreferences_colors[Math.floor(Math.random()*tx_akreferences_colors.length)];
			$('#reference-taglink-'+tagId).css('color', randCol);
		} else {
			$('#reference-taglink-'+tagId).css('color', '');
		}
		$('#reference-taglink-'+tagId).addClass('active');
	}
	else {
		$('#reference-taglink-'+tagId).css('color', '');
		$('#reference-taglink-'+tagId).removeClass('active');
	}
	
		// show/hide references
	tx_akreferences_showactives();
}

function tx_akreferences_showactives() {
	
	for(ri=0, rn=tx_akreferences_references.length; ri<rn; ri++) { // action for all active references
		var referenceId = tx_akreferences_references[ri]; 
		var tagCount = 0; // variable to store how many active tags the current reference has
		for(ti=0, tn=tx_akreferences_references_tags[referenceId].length; ti<tn; ti++) { // action for all tags in this reference
			var tagId = tx_akreferences_references_tags[referenceId][ti];
			if(tx_akreferences_tagstatus[tagId] == true) {
				tagCount++;
			}
		}
		
		if(tagCount == 0) {
			$('#reference-image-active-'+referenceId).fadeOut(tx_akreferences_fadeduration);
		}
		else if(tagCount > 0) {
			$('#reference-image-active-'+referenceId).fadeIn(tx_akreferences_fadeduration);
		}
	}
}

function tx_akreferences_alltags(status, ref) {
	for(i=0, n=tx_akreferences_tags.length; i<n; i++) {
		tx_akreferences_tagswitch(tx_akreferences_tags[i], ref, status, true);
	}
}


function tx_akreferences_show(referenceId) {
	
		// create overlay, when not yet done
	if(!document.getElementById('reference-overlay')) {
		$('body').append('<div id="reference-overlay" style="display:none;" onclick="tx_akreferences_close();"><div id="reference-overlay-inner">&nbsp;</div></div>');
	}
	
		// move reference element inside overlay element
	if($('#reference-overlay').find('#reference-detail-'+referenceId).toArray().length <= 0) {
		$('#reference-detail-'+referenceId).prependTo('#reference-overlay');
	}
	
		// fade in element and layer
	$('#reference-overlay').fadeIn(tx_akreferences_fadeduration);
	$('#reference-detail-'+referenceId).fadeIn(tx_akreferences_fadeduration);
	
		// disable body scrolling
	$('body').css({overflow: 'hidden'});
	$('body').css({overflow: 'hidden'});
}


function tx_akreferences_close() {
	$('#reference-overlay').fadeOut(tx_akreferences_fadeduration, function() { $('body').css({overflow: ''}); /* enable body scrolling */ });
	$('.reference-detail').fadeOut(tx_akreferences_fadeduration);
}

window.onload = function() {
	$('.reference-image-inactive').css('left', '0'); // bring inactive images to visible area, when document ist loaded
	$('.reference-tags').fadeIn(tx_akreferences_fadeduration); // show tag navigation
}
