jQuery(document).ready(function() {
	new WOW().init();
//news backlink [begin]
	var referrer =  document.referrer;
	jQuery(".news-single-backlink a, .news-backlink-wrap a").click(function() {
		window.location = referrer;
//    history.back();
    return false;
  });
//news backlink [end]
//To top link
// hide #back-top first
	jQuery("#back-top").hide();
	// fade in #back-top
	jQuery(function () {
		jQuery(window).scroll(function () {
			if (jQuery(this).scrollTop() > 100) {
				jQuery('#back-top').fadeIn();
			} else {
				jQuery('#back-top').fadeOut();
			}
		});

		// scroll body to 0px on click
		jQuery('#back-top a').click(function () {
			jQuery('body,html').animate({
				scrollTop: 0
			}, 800);
			return false;
		});
	});
//Menü responsive
	jQuery("#menutitle").click(function(){
		jQuery("#menutitle_icon").toggleClass("opend");
		var winHeight = jQuery(window).height();
		jQuery(".submenuicon").hide();
		jQuery("#dropdown").animate({
			width: "toggle",
			height: '+=' + winHeight
			},500, function() {
    		jQuery(".submenuicon").show();
 		});
	});
//	jQuery("#dropdown").find('a.hassub').after('<span class="submenuicon"><span class="arrow-down"></span></span>');
	jQuery('.submenuicon').click(function(){
		jQuery(this).nextAll('ul:first').slideToggle();
		jQuery(this).toggleClass("opend");
	});
	jQuery("#headerTopIcon").click(function(){
		jQuery("#headerTop").toggleClass("opend");
	});
//Hauptmenü mitscrollen lassen
/*
var headerHeight = jQuery("#headerTopWrap").outerHeight();
jQuery("#headerTopWrap").css("height", headerHeight+"px");
jQuery(window).scroll(function() {
//	var topOffset = jQuery("#headerTop").outerHeight();
	var topOffset = headerHeight -(jQuery("#headerNav").outerHeight());
	if ((jQuery(this).scrollTop() > topOffset) && (window.innerWidth > 992)){
		jQuery("#headerNav").addClass("fixed");
	}else{
		jQuery("#headerNav").removeClass("fixed");
	}

});
*/
//parallax
jQuery.RDParallax();
//slider
jQuery(".swiper .swiper-container").each(function(index, element){
    var $this = jQuery(this);
    $this.swiper({
			pagination: $this.find(".swiper-pagination")[0],
			nextButton: $this.find(".swiper-button-next")[0],
			prevButton: $this.find(".swiper-button-prev")[0],
			slidesPerView: 1,
			paginationClickable: true,
			spaceBetween: 30,
			speed: 1500,
			centeredSlides: true,
			autoplay: 4500,
			autoplayDisableOnInteraction: true,
			loop: true,
			effect: 'fade'
    });
});
//slider textmedia
jQuery(".ce-row .swiper-container").each(function(index, element){
    var $this = jQuery(this);
    $this.swiper({
			pagination: $this.find(".swiper-pagination")[0],
			nextButton: $this.find(".swiper-next")[0],
			prevButton: $this.find(".swiper-prev")[0],
			slidesPerView: 1,
			paginationClickable: true,
			spaceBetween: 30,
			speed: 1500,
			centeredSlides: true,
			loop: true,
			effect: 'fade'
    });
});
//accordion
jQuery('.accordion').each(function(){
    options = jQuery(this).find('.options').text();
    if(options) {
        options = jQuery.parseJSON(options);
    } else {
        options = [];
    }
    jQuery(this).find('.ui-accordion').accordion(options);
});
//url setzen
jQuery('.ui-accordion-header').click(function() {
		history.pushState('', '', jQuery(this).find('a').attr('href'));
});
//Inhalt direkt anspringen
jQuery('.ui-accordion-header').each(function() {
  if (document.location.hash === jQuery(this).find('a').attr('href') && !jQuery(this).hasClass('ui-state-active')) {
      jQuery(this).click();
  }
});
//masonry

jQuery('.grid').masonry({
  // options
columnWidth: '.grid-sizer',
itemSelector: '.grid-item',
percentPosition: true
});

resizeViewport();
jQuery(window).resize(function() {
	resizeViewport();
}).bind('orientationchange', function() {
	resizeViewport();
});

function resizeViewport() {

	var width = jQuery(window).width(),
		height = jQuery(window).height();
		//alert(width);
//Originalwerte vorbelegen
	jQuery(".row").removeClass('row-eq-height');
	jQuery(".ce-column").css("max-width","");

//Änderungen bei 992px
	if ((width) > '992'){
		//Hintergründe gleiche Höhe
		jQuery(".row").has('.teaser').each(function(){
			jQuery(this).addClass('row-eq-height');
		});
		jQuery(".row").has('article.layout-2').each(function(){
			jQuery(this).addClass('row-eq-height');
		});
		jQuery(".row").has('article.featured-content').each(function(){
			jQuery(this).addClass('row-eq-height');
		});
		jQuery(".row").has('.news-list-item').each(function(){
			jQuery(this).addClass('row-eq-height');
		});
	}
//Änderungen bei 768px
	if ((width) > '768'){
		//Bildbreite
		//Anzahl Bilder
		jQuery("#main div.ce-textpic").each(function(){
			cols = jQuery(this).find(".ce-gallery").attr("data-ce-columns");
			if (cols){
				maxWidth = jQuery(this).width();
			 	//abzüglich Abstand zwischen den Bildern
			 	margin = 15;
			 	marginValue = (cols-1)*margin;
			 	colWidth = Math.floor((maxWidth-marginValue)/cols);
				jQuery(this).find("div.ce-column").css("max-width",colWidth);
			}
		});
	}
}
//Suchbox
jQuery(".suchboxlupe").click(function(){
		//jQuery("#suchbox").toggleClass("active");
		jQuery(this).parent("div").toggleClass("active");
		jQuery("#tx-indexedsearch-searchbox-sword").focus();
});
/*
jQuery("#tx-indexedsearch-searchbox-sword").focus(function(){
		jQuery("#suchbox").addClass("active");
});
jQuery("#tx-indexedsearch-searchbox-sword").focusout(function(){
		jQuery("#suchbox").removeClass("active");
});
*/
//Klasse btn zu bnt-primary hinzufügen
	jQuery(".btn-primary").addClass("btn");
//für touchscreens overlay auf Menü länger vorhalten
// From https://github.com/Modernizr/Modernizr/blob/master/modernizr.js
var isTouch = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
if (isTouch){
	jQuery('#nav ul li .nav_overlay').css("transition","all 1s ease 0s");
}
//Datepicker
/*
jQuery.datepicker.regional['de'] = {clearText: 'löschen', clearStatus: 'aktuelles Datum löschen',
							 closeText: 'schließen', closeStatus: 'ohne Änderungen schließen',
							 prevText: '<zurück', prevStatus: 'letzten Monat zeigen',
							 nextText: 'Vor>', nextStatus: 'nächsten Monat zeigen',
							 currentText: 'heute', currentStatus: '',
							 monthNames: ['Januar','Februar','März','April','Mai','Juni',
							 'Juli','August','September','Oktober','November','Dezember'],
							 monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
							 'Jul','Aug','Sep','Okt','Nov','Dez'],
							 monthStatus: 'anderen Monat anzeigen', yearStatus: 'anderes Jahr anzeigen',
							 weekHeader: 'Wo', weekStatus: 'Woche des Monats',
							 dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
							 dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
							 dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
							 dayStatus: 'Setze DD als ersten Wochentag', dateStatus: 'Wähle D, M d',
							 dateFormat: 'dd.mm.yy', firstDay: 1,
							 initStatus: 'Wähle ein Datum', isRTL: false};
jQuery.datepicker.setDefaults(jQuery.datepicker.regional['de']);

*/

//jQuery( "#skd-checkin" ).datepicker( jQuery.datepicker.regional[ "de" ] );
var dateFormat = "yy-mm-dd",
	from = jQuery( "#skd-checkin" )
		.datepicker({
			dateFormat: "yy-mm-dd",
			numberOfMonths: 1
		} )
		.on( "change", function() {
			to.datepicker( "option", "minDate", getDate( this ) );
	}),
	to = jQuery( "#skd-checkout" ).datepicker({
		defaultDate: "+1w",
		dateFormat: "yy-mm-dd",
		numberOfMonths: 1
	})
	.on( "change", function() {
		from.datepicker( "option", "maxDate", getDate( this ) );
	});

function getDate( element ) {
	var date;
	try {
		date = jQuery.datepicker.parseDate( dateFormat, element.value );
	} catch( error ) {
		date = null;
	}

	return date;
}



//Dropdown Menü mit Tabulator
dropdown('dropdown', 'hover', 350);


});

/**
Original from http://blakehaswell.com/lab/dropdown/deux/#
 * Adds and removes classes to a list of links to allow keyboard accessibility
 *
 * @param string dropDownId
 * @param string hoverClass
 * @param int mouseOffDelay
 */

function dropdown(dropdownId, hoverClass, mouseOffDelay) {

	if(dropdown = document.getElementById(dropdownId)) {
		var listItems = dropdown.getElementsByTagName('li');
		for(var i = 0; i < listItems.length; i++) {
//			listItems[i].onmouseover = function() { this.className = addClass(this); }
//			listItems[i].onmouseout = function() {
//				var that = this;
//				setTimeout(function() { that.className = removeClass(that); }, mouseOffDelay);
//				this.className = that.className;
//			}

			var anchor = listItems[i].getElementsByTagName('a');
			anchor = anchor[0];
			anchor.onfocus = function() { tabOn(this.parentNode); }
			anchor.onblur = function() { tabOff(this.parentNode); }
		}
	}

	function tabOn(li) {
		if(li.nodeName == 'LI') {
			li.className = addClass(li);
			tabOn(li.parentNode.parentNode);
		}
	}

	function tabOff(li) {
		if(li.nodeName == 'LI') {
			li.className = removeClass(li);
			tabOff(li.parentNode.parentNode);
		}
	}

	function addClass(li) { return li.className + ' ' + hoverClass; }
	function removeClass(li) { return li.className.replace(hoverClass, ""); }
}
