
// ---------------
// Init on DOM ready
// ---------------

$(function() {
	// ---------------
	// Init all functions that have been deposited in initJS - allows to put them there for onReady-execution before jQuery is loaded
	// add functions like so:  initJS.push(function(){alert('yes!')});
	// ---------------
	for (var ix = 0; ix < initJS.length; ix++)  initJS[ix]();



	// ---------------
	// Init smooth scroll on all links
	// - except for the slider arrow links
	// - except for links containing an id OTHER than that of the current page (would prevent links from working!)
	// - except for links containing tx_news_pi1 params for a different news single item (would prevent links from working!)
	// ---------------
    var curPidParam = window.location.search.match(/\bid=.+?\b/g);  // e.g. "id=16" or "id=home"
    var curPidParamRegex = curPidParam ? new RegExp('\\b'+curPidParam+'\\b') : /$./;  // $. will NEVER match anything!!!
    var curTXnewsParam = window.location.search.match(/\btx_news_pi1(%5B|\[)news(%5D|\])=.+?\b/g);  // e.g. "tx_news_pi1[news]=6"
    var curTXnewsParamRegex = curTXnewsParam ? new RegExp('\\b'+curTXnewsParam.slice(-1)[0]+'\\b') : /^((?!tx_news_pi1).)*$/;  // this will ALWAYS match, if NOT tx_news_pi1 parameter given!!!
    $('a[href*=#]:not([data-toggle])').not('.carousel a').filter(	function(){ 
    	var thisHref = $(this).attr('href');
    	if( typeof(thisHref)!='string')  thisHref='';

    	var hrefIsEmpty = thisHref==='';
    	var hrefIsHashOnly = (thisHref.indexOf('#')===0 && thisHref.indexOf('.ge_lightbox_content')===false);  // ge_lightbox_content must be excluded, otherwise LBs will scroll background
    	var hrefIsOtherPid = thisHref.match(curPidParamRegex) === null;
    	var hrefIsOtherTXnews = thisHref.match(curTXnewsParamRegex) === null;

    	var scrollWithinSamePage =  hrefIsEmpty || hrefIsHashOnly || ( !hrefIsOtherPid && !hrefIsOtherTXnews );

			/*
			console.log('-----');
			console.log($(this)[0].outerHTML);
			console.log(thisHref);
			console.log('-');
			console.log(curPidParamRegex);
			console.log(curTXnewsParamRegex);
			console.log('-');
			console.log('hrefIsEmpty: '+hrefIsEmpty);
			console.log('hrefIsHashOnly: '+hrefIsHashOnly);
			console.log('hrefIsOtherPid: '+hrefIsOtherPid);
			console.log('hrefIsOtherTXnews: '+hrefIsOtherTXnews);
			console.log('-');
			console.log('scrollWithinSamePage: '+scrollWithinSamePage);
			*/
		
			return scrollWithinSamePage; 
	}).smoothScroll();




	// ---------------
	// If URL contains section hash-param (e.g. #c4321), check whether target is an accordion, then open it
	// ---------------
	var curSectionParam = window.location.hash.substr(1);
	if(curSectionParam)  $('#'+curSectionParam+'>div.ge_accordion_item>div.panel-collapse').collapse('show');




	// ---------------
	// Init GridElement "Select from Content Elements"
	// ---------------
	$('.ge_select_from_ces > select.gesfc_sel').each(function(){
		var gridElId = $(this).parent().parent('.grid_el').attr('id');  // e.g. c1234
		if(Cookies.get(gridElId))  $(this).val(Cookies.get(gridElId));
	});
	geSfcUpdateCEs(true);
	$('.ge_select_from_ces > select.gesfc_sel').change(function(){
		var gridElId = $(this).parent().parent('.grid_el').attr('id');  // e.g. c1234
		Cookies.set(gridElId, $(this).val());
		geSfcUpdateCEs(false);
	});

	function geSfcUpdateCEs(firstInit){
		$('.ge_select_from_ces > select.gesfc_sel').each(function(){
			var selectIsHidden = $(this).is(':hidden') || $(this).parents('.geSfcWillHide').length >0;
			$(this).children('option').each(function(){
				var gridElSelector = '#'+$(this).val();
				if( selectIsHidden || ( $(this).val()!='' && !$(this).is(':selected') ) ){
					if(firstInit) $(gridElSelector).hide()
					else          $(gridElSelector).addClass('geSfcWillHide').slideUp( function(){ $(this).removeClass('geSfcWillHide'); $.fn.matchHeight._update(); } );
				}else{
					if(firstInit) $(gridElSelector).show()  // not really necessary
					else          $(gridElSelector).addClass('geSfcWillShow').slideDown( function(){ $(this).removeClass('geSfcWillShow'); $.fn.matchHeight._update(); } );
				}
			});
		});
	}




	// ---------------
	// Init Orga Tree (e.g. used for Executives & BOD)
	// ---------------
	$('.orga_tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
	$('.orga_tree li.parent_li > span').on('click', function (e) {
		var children = $(this).parent('li.parent_li').find(' > ul > li');
		if (children.is(":visible")) {
			children.hide('fast', $.fn.matchHeight._update );  // hide and update matchHeight columns when hide finishes
			$(this).attr('title', 'Expand this branch').find(' > i').addClass('glyphicon-plus-sign').removeClass('glyphicon-minus-sign');
		} else {
			children.show('fast', $.fn.matchHeight._update );  // show and update matchHeight columns when show finishes
			$(this).attr('title', 'Collapse this branch').find(' > i').addClass('glyphicon-minus-sign').removeClass('glyphicon-plus-sign');
		}
		e.stopPropagation();
	});
	//$('.orga_tree li.parent_li > span.start_closed').click();
	$('.orga_tree li.parent_li > span.start_closed').parent().find('> ul > li').hide();
	$('.orga_tree li.parent_li > span.start_closed').attr('title', 'Expand this branch').find(' > i').addClass('glyphicon-plus-sign').removeClass('glyphicon-minus-sign');
    




	// ---------------
	// Put click event on Ham icon to toggle mobile menu
	// ---------------
	$('.sidebar-offcanvas [data-toggle="offcanvas"]').click(function () {
		$('.row-offcanvas').toggleClass('active');
		$(this).toggleClass("collapsed");
	});
	



	// ---------------
	// MatchHeight
	// ---------------
	// Solve problem of heavy scroll jumping when clicking Tabs and Accordions!
	$.fn.matchHeight._maintainScroll = true;
	// Same height for neighboring icon boxes
	// Using the first div child (instead of the iconbox itself) allows for images in a second div to stay alined!
	$('.ge_iconbox > div:first-of-type').matchHeight(true);  // param = row detection
	// ---------------
	// Same height for neighboring columns
	// NOT activated for Multi Column Content Elements any more: 
	// multi-col CEs get matchHeight now through data-mh attribute
	// this allows for grouping which is needed for correct height calc of nested multi-cols
	// OFF: $('.contentCol .row > [class^="col-"]').matchHeight(true);
	// OFF: $('.megamenu .row > [class^="col-"]').matchHeight(true);
	// ---------------
	// Recalc heights after any accordion (or other collapsible) was opened
	$('.collapse').on('shown.bs.collapse', function () {
		$.fn.matchHeight._update();
	})
	// Recalc heights after any tab was activated
	$('a[data-toggle="tab"]').on('shown.bs.tab', function () {
		$.fn.matchHeight._update();
	})
	// ---------------
	// Massive problems with nested column CEs:
	// height of outer element is not updated correctly (no respecting height of inner element) on first calculation
	// probably a bug in matchHeight but not reproducable in reduced context.
	// Hack: Recalc after some millisecs
	setTimeout( function(){ $.fn.matchHeight._update(); }, 500);
	// ---------------
	// Images loaded after height has been matched can mess up the structure – recalc heights with each late img load:
	$(".contentCol img").one("load", function() {
  	$.fn.matchHeight._update();
	});
	



	// ---------------
	// Lightbox effect – using iLightBox
	// ---------------
	$('.lightbox').iLightBox({ 
		skin: 'dark'
	});
	// used for separate groups, e.g. Orga Trees:
	$('.lightboxGroup2').iLightBox({ 
		skin: 'metro-white'
	});
	$('.lightboxGroup3').iLightBox({ 
		skin: 'metro-white'
	});






	// ---------------
	// CONTINUE LOAD TIME INITS HERE.....
	// ---------------


	
});





