var initialTopic = 1;		// Defines the element, which will be shown as the initial topic (Range: 1-3).
var showTime = 5000;		// Defines, how long a topic will be shown until the auto-switcher will be executed (1000 = 1 second).
var fadeInSpeed = 750;		// Defines the fade-in-speed of the interchangeable selectedTopic-Container.
var fadeOutSpeed = 750;		// Defines the fade-out-speed of the interchangeable selectedTopic-Container.
$(document).ready(function(){
	timer = 0;
	initialTopic = initialTopic-1;
	currentTopic = initialTopic;
	newTopic = currentTopic;
	maxTopics = 0;
	// Check how many topics there are and write it into a variable.
	$(".hotTopics").find(".selectedTopic").find(".topic").each(function() {
		maxTopics++;
	});
	maxTopics = maxTopics-1;
	// Show the topic-target, that is selected as initialTopic.
	$(".hotTopics").find(".selectedTopic").find("#selectedTopic"+(currentTopic)).show();
	// Set the topic-trigger, that is selected as initialTopic, as active.
	$(".hotTopics").find(".topicSelector").find("#topicSelector"+(currentTopic)).find(".numberBorder").addClass("activeNumberBorder");
	$(".hotTopics").find(".topicSelector").find("a").attr("href", "javascript:void(0);");
	// Initialize hover-handlung of the toggle-triggers.
	$(".hotTopics").find(".topicSelector").find("a").hover(function(){
		$(this).find(".numberBorder").addClass("hoverNumberBorder");
	}, function() {
		$(this).find(".numberBorder").removeClass("hoverNumberBorder");
	});
	// Assign a aria-attribute to the topic-targets for accessibility.
	$(".hotTopics").find(".selectedTopic").find(".topic:visible").attr("aria-expanded", "true");
	$(".hotTopics").find(".selectedTopic").find(".topic:hidden").attr("aria-expanded", "false");
	// Assign a id to the topic-triggers and a aria-attribute to the topic-targets to get a relation for accessibility.
	$(".hotTopics").find(".topicSelector").find(".topic").each(function(index) {
			$(this).find("a").attr("id", "topicTrigger"+index );
			$(this).parent().parent().find(".selectedTopic").find("#selectedTopic"+index).attr("aria-controls", "topicTarget"+index);
		}
	);
	// Run topic-switch-process on click.
	$(".hotTopics").find(".topicSelector").find("a").click(function() {
		hotTopicsClickSwitcher(this);
	});
	// Run timer for the automatic switch-handling.
	startTimer();
	$(".hotTopics").find(".selectedTopic").find(".topicVideo > div").hide();
	$(".hotTopics").find(".selectedTopic").find(".playButton > a").click(function() {
		hotTopicsVideoSwitcher(this);
	});
});
// Timer-function for the automatic switch-handling.
function startTimer() {
	timer = setTimeout("hotTopicsAutoSwitcher()", showTime);
}
// Stops the automatic switch-handling, when the user selected a topic manually.
function stopTimer() {
	clearTimeout(timer);
}
// Runs the switch process, when the user selected a topic manually.
function hotTopicsClickSwitcher(e) {
	newTopic = $(e).parent().parent().find("a").index(e);
	// Set the switchType to 1, because the user selected a topic manually.
	switchType = 1;
	// Run switch-process.
	hotTopicsSwitchProcess();
}
// Runs the switch process, when the timer is reached.
function hotTopicsAutoSwitcher() {
	// Check if newTopic is greater or equal maxTopics
	if (newTopic >= maxTopics) {
		newTopic = 0;
	} else {
		newTopic = currentTopic+1;
	}
	// Set the switchType to 0, because it's a automatic switch.
	switchType = 0;
	// Run switch-process.
	hotTopicsSwitchProcess();
}
// Switches the topics and updates attributes for accessibility.
function hotTopicsSwitchProcess() {
	// Check if the newTopic is unlike currentTopic.
	if (currentTopic!=newTopic) {
		$(".hotTopics").find(".topicSelector").find(".topic").find(".numberBorder").removeClass("activeNumberBorder");
		$(".hotTopics").find(".topicSelector").find("#topicSelector"+newTopic).find(".numberBorder").addClass("activeNumberBorder");
		$(".hotTopics").find(".selectedTopic").find(".topic").fadeOut(fadeOutSpeed);
		$(".hotTopics").find(".selectedTopic").find("#selectedTopicVideo"+currentTopic).find(".videoContainer").hide();
		$(".hotTopics").find(".selectedTopic").find(".topicVideo").fadeOut(fadeOutSpeed);
		$(".hotTopics").find(".selectedTopic").find("#selectedTopic"+newTopic).fadeIn(fadeInSpeed);
		$(".hotTopics").find(".selectedTopic").find("#selectedTopic"+currentTopic).attr("aria-expanded", "false")
		$(".hotTopics").find(".selectedTopic").find("#selectedTopic"+newTopic).attr("aria-expanded", "true")
	}
	currentTopic = newTopic;
	// Checks if the user selected a topic manually.
	if (switchType == 1) {
		// Stop automatic switch-handling.
		stopTimer()
	} else {
		// Restart automatic switch-handling.
		startTimer();
	}
}
// Shows the topic-video, when the user clicked on the play-button.
function hotTopicsVideoSwitcher(e) {
	// Stop automatic switch-handling.
	stopTimer()
	$(".hotTopics").find(".selectedTopic").find(".topic").fadeOut(fadeOutSpeed);
	$(".hotTopics").find(".selectedTopic").find("#selectedTopicVideo"+currentTopic).fadeIn(fadeInSpeed, function() {
		$(".hotTopics").find(".selectedTopic").find("#selectedTopicVideo"+currentTopic).find(".videoContainer").show();
	});
}

