$(document).ready(function() {
	$(".nav li").addClass("cc");
	$(".nav li").hover( function(){
		$(this).addClass("over");
	},function(){
		$(this).removeClass("over");
	});


	// Mobile Nav
	var openNav = $('.open_nav'),
		nav = $('.nav'),
		subNav = $('.subNav'),
		touchDevice = (function () {
			return "ontouchstart" in window || "onmsgesturechange" in window
		})();

	openNav.click(function(){nav.stop().fadeToggle(500);return false});subNav.prev().on({'touchstart':function(){var openSubNav=$(this).parent(),active=openSubNav.hasClass('active');if(touchDevice&&!active){$('.active').removeClass('active');nav.find('li').removeClass('over');var parrentsArray=openSubNav.parents();for(var i=0;i<parrentsArray.length;i++){if($(parrentsArray[i]).is('li')){$(parrentsArray[i]).addClass('over')}}openSubNav.addClass('active over');return false}else return true}});

	$('.hamburger').click(function(){
		$(this).toggleClass('is-active');
	});

	$('.nav ul li').each(function(){
		var hasSubnav = $(this).find('ul').length;
		if ( hasSubnav >= 1 ){
			$(this).prepend('<div class="showSub" />');
		}
	});

	if ( $('.open_nav').is(':visible') == true ){
		$('.showSub').on('click', function(){
			$(this).parent('li').children('.subNav').slideToggle(200);
			$(this).toggleClass('open');
			$(this).parent('li').toggleClass('open');
		});
	};

	// Leere Texte unsichtbar
	$('#subhead .csc-textpic-text, .ib .inhalt, .livechat').each(function(){
		if ( $(this).html() ) {
			$(this).css('display', 'block');
		}
	});

	// Lang-Switch
	// $('.headnav .lang-switch LI.act A').click(function(e){
	// 	e.preventDefault();
	// 	$(this).parents('.lang-switch').toggleClass('act');
	// 	$('.headnav UL.lang-switch LI:not(:first-child)').fadeToggle(400);
	// });

	// $('.headnav .lang-switch').prepend( $('.headnav .lang-switch LI.act') );

	// Live-Chat Position
	var top = parseInt($('.livechat').css('top'), 10);

	// $(window).scroll(function(){
	//     $(".livechat").css("top", Math.max(250, top - $(this).scrollTop()));

	//     console.log($(window).scrollTop());

	//     if( $(window).scrollTop() > 1400 ) {
	//     	$('.livechat').addClass('stick');
	//     	$('.livechat').removeClass('fadeIn');
	//     } else {
	//     	$('.livechat').removeClass('stick');
	//     	$('.livechat').addClass('fadeIn');
	//     }

	// });

	// Select stylen
	$('select').wrap('<span class="select-wrap"></span>');

	//Lightbox
	$('a.lightbox').fancybox({
		openEffect: 'elastic',
		closeEffect: 'elastic',
		nextEffect : 'fade',
		prevEffect : 'fade',
		nextSpeed : '500',
		prevSpeed : '500',
		padding : 0,
		helpers: {
			overlay: {
				locked: false
			}
		}
	});

	// Slider Banner
	$('#subhead2').bxSlider({
		slideSelector: '.item',
		controls: ($("#subhead2 .item").length > 1) ? true: false,
		pager: false,
		mode: 'horizontal',
		speed: 3000,
		pause: 10000,
		touchEnabled: ($("#subhead2 .item").length > 1) ? true: false,
		auto: ($("#subhead2 .item").length > 1) ? true: false
	});

	// Slider
	$('.slider2').bxSlider({
		slideSelector: '.slider2 > .item',
		controls: ($(".slider2 > .item").length > 1) ? true: false,
		pager: ($(".slider2 > .item").length > 1) ? true: false,
		mode: 'horizontal',
		speed: 3000,
		pause: 10000,
		touchEnabled: ($(".slider2 > .item").length > 1) ? true: false,
		auto: ($(".slider2 > .item").length > 1) ? true: false
	});

	$('.logoslider .csc-textpic-center-inner').slick({
		// slide: '.csc-textpic-imagerow',
		slidesToShow: 4,
		slidesToScroll: 4,
		// autoplaySpeed: 500,
		autoplay: true,
		// speed: 10000,
		speed: 1000,
		// cssEase: 'linear',
		arrows: false,
		dots: true,
		responsive: [
		    {
		      breakpoint: 1300,
		      settings: {
		        slidesToShow: 3,
		        slidesToScroll: 3,
		      }
		    },
		    {
		      breakpoint: 900,
		      settings: {
		        slidesToShow: 2,
		        slidesToScroll: 2,
		      }
		    },
		  	{
		      breakpoint: 470,
		      settings: {
		        slidesToShow: 1,
		        slidesToScroll: 1,
		      }
		    },
		  ]
	});

	// NTH-CHILD Tabellen
	$( ".allcont table tr:nth-child(even)" ).addClass('even');
	$( ".allcont table tr:nth-child(odd)" ).addClass('odd');

	// Nav mit Unterpunkten bekommt eine Klasse
	$('.nav LI').find('UL').parent().addClass('has-sub');


	// Services ganze Box verlinken
	$('.services .col').each(function(){
		if ( $(this).find(".csc-textpic-imagewrap a").attr("href") != undefined) {
			$(this).click(function(e){
				window.location = "/" + $(this).find(".csc-textpic-imagewrap a").attr("href");
				return false;
			});
		} else {
			$(this).addClass('inactive');
		}
	});
	
	// Responsive-Skripte
	var responsive = function(){
		if (window.matchMedia("(min-width: 760px)").matches) {

			// CSS Animate It
			$('.cont > .item:not(.a-z), .cont > .gridwrapper').addClass('animatedParent animateOnce');

			$('.cont > .item:not(.a-z) > *, .cont > .gridwrapper > *').addClass('animated fadeInUpShort');

			$('.popup').parents('.animatedParent').removeClass('animatedParent animateOnce');
			$('.popup').parents('.animated').removeClass('animated fadeInUpShort');

		}
	};

	responsive();

	$(window).resize(function(){
		responsive();
	});

	// ScrollTo
	$('.totop').click(function(e){
		e.preventDefault();
		$('html, body').animate({ scrollTop: ($('#toTop').offset().top)}, 'slow');
	});

	// popup

	// Link finden und ganze Box verlinken
	$('.popup-teaser .csc-textpic-text > P').wrapInner('<a href="/" class="popup" target="_self"></a>');

	var actElem = 0;

	$('.popup').parents('.popup-teaser').click(function(){
		actElem = $(this);
		$(this).siblings('.popup-window').prependTo('body').fadeIn();
	})

	$('.popup').click(function(e){
		e.preventDefault();
	})

	$('.close').click(function(){
		$(this).parents('.popup-window').fadeOut().appendTo(actElem.parents('.popup-wrapper'));
	})

	// A-Z
	// Beim Scrollen an den Kopf sticken
	stickAZ();

	$(window).resize(function(){
		stickAZ();
	})
	
	// Inaktive Klassen vergeben, falls keine anscrollbaren Elemente
	$('.popup').each(function(){

		// Erster Buchstabe
		var char = $(this).text().charAt(0);

		// Vergleiche mit Listenelement und mache bei Bedarf transparent
		$('.a-z ul li').each(function(){

			if ( $(this).text() == char ) {
				$(this).addClass('active');
				return false;
			}

		});

	});

	// Bei Klick Scroll zum ersten Element mit Anfangsbuchstaben
	$('.a-z ul li').click(function(){

		// Erster Buchstabe des geklickten Elements
		var character = $(this).text();

		// Abstand zum Kopf
		var dist = 215;

		// Vergleiche mit Popup-Inhalt und scrolle zum Eltern-Element .popup-teaser
		$('.popup').each(function(){
			if ( $(this).text().charAt(0) == character ) {
				$('html, body').animate({ scrollTop: ( $(this).parents('.popup-teaser').offset().top - dist ) }, 'slow');
				return false;
			}
		});

	});

	// Ab 720 A-Z-Klapper
	$('.a-z > P').click(function(){
		$('.a-z').toggleClass('pullIn');
	});

	// Accordion
	$('.accordion .acc-content').hide();
	$('.accordion .acc-arrow, .accordion .acc-headline').click(function(){

		// Zuerst alle schließen
		$('.accordion .acc-content').slideUp();
		// Entferne .act-Klassen, außer diese
		$('.accordion').not($(this).parent()).removeClass('act');

		// Wenn dieser nicht bereits aktiv ist, öffne ihn
		if ( !$(this).parents('.accordion').hasClass('act') ) {
			$(this).siblings('.acc-content').slideToggle();
		}

		// Toggle act-Klasse
		$(this).parents('.accordion').toggleClass('act');
	});

	$('iframe[src*="youtube.com"]').each(function() {
        $(this).wrap('<div class="frame-wrap"></div>');
    }); 

	// Datenstrang	
	var lightgrey = "rgba(0,0,0,0.05)";
	var darkgrey = "rgba(0,0,0,0.2)";
	var blue = "rgba(0,68,148, 0.7)";

	if (window.matchMedia("(max-width: 760px)").matches) {

		if ( $('#smooth').length ) {
			// Datenstränge
			var wavesSmooth = new SineWaves({
			  el: document.getElementById('smooth'),
			  speed: 1,
			  // ease: 'SineInOut',
			  ease: function(percent, amplitude) {
			     return amplitude * (percent / 0.8);
			   },
			  rotate: 2,
			  waves: [
			    {
			       // Smooth
			      timeModifier: 1,
			      lineWidth: 3,
			      amplitude: 50,
			      wavelength: 100,
			      segmentLength: 1,
			      strokeStyle: darkgrey
			    }
			  ]

			});
		}

		if ( $('#smoothFooter').length ) {
			var wavesSmoothFooter = new SineWaves({
			  el: document.getElementById('smoothFooter'),
			  speed: 1,
			  // ease: 'SineInOut',
			  ease: function(percent, amplitude) {
			     return amplitude * (percent / 0.8);
			   },
			  rotate: 2,
			  waves: [
			    {
			       // Smooth
			      timeModifier: 1,
			      lineWidth: 3,
			      amplitude: 50,
			      wavelength: 100,
			      segmentLength: 1,
			      strokeStyle: darkgrey
			    }
			  ]

			});
		}

	} else {
		
		if ( ('#smooth').length ) {
			// Datenstränge
			var wavesSmooth = new SineWaves({
			  el: document.getElementById('smooth'),
			  speed: 1,
			  // ease: 'SineInOut',
			  ease: function(percent, amplitude) {
			     return amplitude * (percent / 0.8);
			   },
			  rotate: 2,
			  waves: [
			    {
			       // Smooth
			      timeModifier: 1,
			      lineWidth: 3,
			      amplitude: 100,
			      wavelength: 200,
			      segmentLength: 1,
			      strokeStyle: darkgrey
			    }
			  ]

			});
		}

		if ( $('#smoothFooter').length ) {
			var wavesSmoothFooter = new SineWaves({
			  el: document.getElementById('smoothFooter'),
			  speed: 1,
			  // ease: 'SineInOut',
			  ease: function(percent, amplitude) {
			     return amplitude * (percent / 0.8);
			   },
			  rotate: 2,
			  waves: [
			    {
			       // Smooth
			      timeModifier: 1,
			      lineWidth: 3,
			      amplitude: 100,
			      wavelength: 200,
			      segmentLength: 1,
			      strokeStyle: darkgrey
			    }
			  ]

			});
		}	
	}

});


// AUSSERHALB VON READY
function gleicheHoehe(cont,el,breakpoint)
{
	var docWidth = $(document).width();
	var container = $(cont);
	function reset(){
		container.each(function() {
			var sameHeight = $(this).find(el);
			sameHeight.each(function(){
				$(this).css('height', '');
			});
		});
	}
	if(docWidth > breakpoint || breakpoint == undefined)
	{
		reset();
		container.each(function() {
			var sameHeight = $(this).find(el);
			var highestItem = 0;
			sameHeight.each(function(){
				if($(this).outerHeight() > highestItem)
				highestItem = $(this).outerHeight();
			});
			sameHeight.outerHeight(highestItem);
		});
	}
	else
	{
		reset();
	}
}

// Dennis: Stick A-Z (Partners)
function stickAZ() {
	if ( window.matchMedia("(min-width: 960px)").matches && $('.a-z').length ) {

		var offsetAZ = $('.a-z').offset().top;

		$(window).scroll(function(){
			if ( $(this).scrollTop() >= offsetAZ - 165) {
				$('.a-z').addClass('fixed');
			} else {
				$('.a-z').removeClass('fixed');
			}
		})
	}
}

$( window ).load(function() {

    // var cookieText = 'This website uses cookies to ensure you get the best experience on our website.';
    // var cookieMoreText = 'Learn more';

    // // Cookie Meldung
    // window.cookieconsent.initialise({
    //     "palette": {
    //         "popup": {
    //             "background": "rgba(255,255,255,0.9)",
    //             "text": "#404040"
    //         },
    //         "button": {
    //             "background": "#004494",
    //             "text": "#ffffff",
    //             // "border": "#ffffff"
    //         }
    //     },
    //     // "position": "top",
    //     "content": {
    //         "message": cookieText,
    //         "dismiss": "Ok",
    //         "link": cookieMoreText,
    //         "href": "contact/data-privacy/"
    //     }
    // });


    // Datenstrang platzieren
    if (window.matchMedia("(max-width: 760px)").matches) {
    	$('.head + .datenstrang, .bel9 .head + .datenstrang').css('top', $('.allcont').offset().top - ( $(window).width() / 4) );
    }
});