var lastActiveSubNavigationId = 1;

function showSubNavigation(eleId) {
	if (jQuery.isNumeric(eleId)) {
		(!jQuery('.subNavigationContainer').is(':visible')) ? jQuery('.subNavigationContainer').fadeIn('slow') : "";

		if (lastActiveSubNavigationId != 0 && eleId != lastActiveSubNavigationId) {
			jQuery('.subNavigationContainer .navLinkId_'+lastActiveSubNavigationId).slideUp('fast');
			jQuery('.navactiveid'+lastActiveSubNavigationId).addClass('navInactive').removeClass('navActive navactiveid'+lastActiveSubNavigationId);
			
			if (jQuery('.parentIdForWelcomePic_'+lastActiveSubNavigationId).length) {
				jQuery('.parentIdForWelcomePic_'+lastActiveSubNavigationId).animate({"opacity": "0"}, {queue:false, duration:400});
			} else {
				jQuery('.parentIdForWelcomePic_0').animate({"opacity": "0"}, {queue:false, duration:400});
			}
			

		}
		if (eleId != lastActiveSubNavigationId) {
			jQuery('.subNavigationContainer .navLinkId_'+eleId).slideDown('slow');
			jQuery('#menu-mainNav_'+eleId).parent().parent().parent().parent().removeClass('navInactive').addClass('navActive navactiveid'+eleId);

			if (jQuery('.parentIdForWelcomePic_'+eleId).length) {
				jQuery('.parentIdForWelcomePic_'+eleId).animate({"opacity": "1"}, {queue:false, duration:400});
			} else {
				jQuery('.parentIdForWelcomePic_0').animate({"opacity": "1"}, {queue:false, duration:400});
			}
			
		}
		
		lastActiveSubNavigationId = eleId;
	}
}


var gallerySlide_movesRight  = 0;
var gallerySlide_movesLeft   = 0;
var gallerySlide_imagesWidth = 155;
var gallerySlide_imagesCount = 0;
var gallerySlide_init = true;

function gallerySlide_construct() {
	gallerySlide_imagesCount = parseInt(jQuery('#slideGallery_imagesCount').html());
	(gallerySlide_imagesCount > 4) ? gallerySlide_movesRight = gallerySlide_imagesCount - 4 : gallerySlide_movesRight = 0;
	gallerySlide_init = false;
}

function gallerySlide(slideWay) {
	if (!jQuery('.slideContent').is(':animated')) {
		(gallerySlide_init == true) ? gallerySlide_construct() : '';
								
		var newLeftPos = 0;
		if (slideWay == 1 && gallerySlide_movesRight > 0) {
			newLeftPos = parseInt(jQuery('.slideContent').css("left")) - 155;
			jQuery('.slideContent').animate({"left": newLeftPos+"px"});
			gallerySlide_movesLeft++;
			gallerySlide_movesRight--;
		} else if (slideWay == 0 && gallerySlide_movesLeft > 0) {
			newLeftPos = parseInt(jQuery('.slideContent').css("left")) + 155;
			jQuery('.slideContent').animate({"left": newLeftPos+"px"});
			gallerySlide_movesRight++;
			gallerySlide_movesLeft--;
		}
		
		gallerySlide_mouseUpdate();
	} 
}

function gallerySlide_mouseUpdate() {
	(gallerySlide_init == true) ? gallerySlide_construct() : '';
	
	(gallerySlide_movesLeft > 0) ? jQuery('.slideGallery_leftArrow').animate({"opacity": "1", queue:false}) : jQuery('.slideGallery_leftArrow').animate({"opacity": "0.2", queue:false});
	(gallerySlide_movesRight > 0) ? jQuery('.slideGallery_rightArrow').animate({"opacity": "1", queue:false}) : jQuery('.slideGallery_rightArrow').animate({"opacity": "0.2", queue:false});
}

function gallerySlide_mouseEnter() {
	(gallerySlide_init == true) ? gallerySlide_construct() : '';
	
	(gallerySlide_movesLeft > 0) ? jQuery('.slideGallery_leftArrow').animate({"opacity": "1", queue:false}) : '';
	(gallerySlide_movesRight > 0) ? jQuery('.slideGallery_rightArrow').animate({"opacity": "1", queue:false}) : '';
}

function gallerySlide_mouseLeave() {
	(gallerySlide_init == true) ? gallerySlide_construct() : '';
	
	jQuery('.slideGallery_leftArrow').animate({"opacity": "0.2", queue:false});
	jQuery('.slideGallery_rightArrow').animate({"opacity": "0.2", queue:false});	
}





function navigation_construct () {
	/*
	 * show sub navigation (second level)
	 */
	jQuery('.navigationContainer').find('a').each(function() {
		var eleId = jQuery(this).attr('id');
		eleId = eleId.replace('menu-mainNav_', '');
		jQuery(this).attr({"onclick": "showSubNavigation('"+eleId+"');"}).attr({"href": "javascript:void(0);"});
	});

	/*
	 * sub navigation (second level)
	 */
	var mainNavigationRuntime;

	jQuery('.rawNavigationContainer').find('li').each(function () {
		mainNavigationRuntime++;
		
		var liContent = jQuery(this).html();
		//liContent contains something like: <a id="menu-mainNav_32" class="main" href="/mediathek">Mediathek</a>
				
		var beginOfId = liContent.indexOf('id=');
	
		if (beginOfId != -1) {
			var beginOfIdPlus = 4;
		
			//handles the missing " after id= in Internet Explorer
			var safetyCheck = liContent.substring(beginOfId+3, beginOfId+4);
			if (safetyCheck != '"') {
				beginOfIdPlus = 3
			}
		
			var removedString = liContent.substring(0, beginOfId+beginOfIdPlus);
			liContent = liContent.replace(removedString, "");
			liContent = liContent.replace("menu-mainNav_", "");
																
			var charIsNumber = true;
			var charAtPos;
			var i = -1;
			while (charIsNumber == true) {
				i++;
				charAtPos = liContent.charAt(i);
				
		 		if ( (charAtPos == 0) || (charAtPos == 1) || (charAtPos == 2) || (charAtPos == 3) ||
				 	 (charAtPos == 4) || (charAtPos == 5) || (charAtPos == 6) || (charAtPos == 7) || 
				 	 (charAtPos == 8) || (charAtPos == 9) ) {
					charIsNumber = true;
				} else {
					charIsNumber = false;
				}
			}
			
			liContent = liContent.substring(0, i);
						
			jQuery('.subNavigationContainer').append('<div class="navLinkId_'+liContent+' subNavLinkContainer" style="display:none;"></div>');
			jQuery('.rawNavigationContainer').find('#menu-mainNav_'+liContent).next('ul').clone().appendTo('.navLinkId_'+liContent);
			
		}	
	});
	
	/*
	 * automatic navigation choose
	 */
	if (jQuery('.mainactive').length) {
		var currentActiveDocId = jQuery('.mainactive').attr("id");
		if (currentActiveDocId != "") {
			currentActiveDocId = currentActiveDocId.replace('menu-mainNav_', '');
			showSubNavigation(currentActiveDocId);
		}
	}
	
	/*
	 * find the last li (in the sub navigation (3 dots)) and remove the background-image 
	 */
	jQuery('.mainHeaderBackground').find('.subNavLinkContainer').each(function () {
		jQuery(this).find('li').last().addClass('lastSubNavLi');
	});
}

/*
 * breadcrumb
 */
function breadcrumb_construct() {
	jQuery('.breadcrumb').find('a').each(function() {
		jQuery(this).attr({"href": "javascript:void(0);"}).addClass('breadcrumbLink');
	});
}	 
