var resultsQuery = '';
var resultsCount = '';
function equalHeightMainCols(group) {
	group.height('auto');
	if (Modernizr.mq('(min-width: 1024px)')){
	//if ($(window).width() >= 1024){
		var tallest = 0;
		group.each(function () {
			var thisHeight = jQuery(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
		
	}
}	

function equalHeight(group) {
	group.height('auto');
		var tallest = 0;
		group.each(function () {
			var thisHeight = jQuery(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
}


function equalHeightBoxes(group) {
	group.height('auto');
		var tallest = 0;
		group.each(function () {
			var thisHeight = jQuery(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		if ($(window).width() >= 768){
			group.height(tallest);
		}
		if ($(window).width() >= 1024){	
			group.height('auto');
		}
}


function equalHeightBoxesHome(group) {
	group.height('auto');
		var tallest = 0;
		group.each(function () {
			var thisHeight = jQuery(this).height();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		if ($(window).width() >= 768){
			group.height(tallest);
		}
}


equalRowheight = function(container){

var currentTallest = 0,
	currentRowStart = 0,
	rowDivs = new Array(),
	$el,
	topPosition = 0;

$(container).each(function() {

	$el = $(this);
	$($el).height('auto')
	topPostion = $el.position().top;

	if (currentRowStart != topPostion) {
		for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
			rowDivs[currentDiv].height(currentTallest);
		}
		rowDivs.length = 0; // empty the array
		currentRowStart = topPostion;
		currentTallest = $el.height();
		rowDivs.push($el);
	} else {
		rowDivs.push($el);
		currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
	}
	for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
		rowDivs[currentDiv].height(currentTallest);
	}
 });
}




function getURLParam(strParamName) {
	var strReturn = "";
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1) {
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for (var iParam = 0; iParam < aQueryString.length; iParam++) {
			if (
				aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}

	if (strParamName == 'width' || strParamName == 'height') {
		strReturn = parseInt(strReturn);
	}

	if (strParamName == 'open') {
		if (strReturn.toLowerCase().indexOf('javascript') != -1 || strReturn.toLowerCase().indexOf('http') != -1) {
			strReturn = '';
		}
		strReturn = strReturn.replace(/[^a-z0-9-_\/]/gi,'');
	}

	return unescape(strReturn);
}

function isFlashEnabled()
{
	var hasFlash = false;
	try
	{
		var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
		if(fo) hasFlash = true;
	}
	catch(e)
	{
		if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) hasFlash = true;
	}
	return hasFlash;
}

jQuery(document).ready(function () {

	//hide the sublayer on click
    jQuery('ul.group li a').click(function(event) {
        $(this).next().hide();
    });

    var openUrl = getURLParam('open');
    var height = getURLParam('height');
    var width = getURLParam('width');

    if (openUrl != '') {
        $.fancybox({
            'href':decodeURIComponent(openUrl),
            'title':'',  
            'height':height,
			'maxHeight':height,
            'width':width,
            'autoScale': false,
   	    	'autoDimensions': false,
            'type':'iframe',
            'padding' : 0,
            afterLoad: function(current, previous) {
        	}
        });
    }

    // fancybox
    if (jQuery('a.fancybox').length) {
        jQuery('a.fancybox').fancybox({
            overlayOpacity:'0.7',
            overlayColor:'#000'
        });
    }

	faqHashOpener();
});

function faqHashOpener() {

    if (window.location.hash) {
        elemId = '#' + window.location.hash.substr(1);
        //alert(elemId);
        if (jQuery(elemId).length) {
            //alert('found');
            jQuery(elemId).trigger('click');
            jQuery(elemId).next().show();
            var triggerElem = jQuery(elemId);
            var topPosition = $(triggerElem).offset().top;

            //alert(topPosition);

            var leftPosition = $(triggerElem).offset().left;
            jQuery('html').animate({ scrollTop:topPosition }, { duration:'slow', easing:'swing'});


        }
        //alert(window.location.hash.substr(1));
    }
}

function decryptCharcode(n, start, end, offset) {
    n = n + offset;
    if (offset > 0 && n > end) {
        n = start + (n - end - 1);
    } else if (offset < 0 && n < start) {
        n = end - (start - n - 1);
    }
    return String.fromCharCode(n);
}
function decryptString(enc, offset) {
    var dec = "";
    var len = enc.length;
    for (var i = 0; i < len; i++) {
        var n = enc.charCodeAt(i);
        if (n >= 0x2B && n <= 0x3A) {
            dec += decryptCharcode(n, 0x2B, 0x3A, offset);
        } else if (n >= 0x40 && n <= 0x5A) {
            dec += decryptCharcode(n, 0x40, 0x5A, offset);
        } else if (n >= 0x61 && n <= 0x7A) {
            dec += decryptCharcode(n, 0x61, 0x7A, offset);
        } else {
            dec += enc.charAt(i);
        }
    }
    return dec;
}
function linkTo_UnCryptMailto(s) {
    location.href = decryptString(s, -3);
}

//Show Mainnavigation on larger screens
function showDesktopNav(){
	if(!$('html').hasClass('ie8')){
		$('.main-nav > ul').hide();
		$('.main-nav .subnav').hide();
		$('.main-nav .subsubnav').hide();
		$('.mobile_toggle').removeClass('mobile_sub_open');
		$('.mobile_navi_container').removeClass('mobile_navi_active');
		//if ($(window).width() >= 1024){
		if (Modernizr.mq('(min-width: 1024px)')){
			$('.main-nav > ul').show();
		}
	}
}
function showDesktopSearch(){
	if (Modernizr.mq('(min-width: 1024px)')){
	//if ($(window).width() > 1024){
		$('.mobile_search_btn').hide().removeClass('mobile_search_open');
		$('.main-nav > ul').removeClass('main-nav-mobile');
		$('.mobile_search_form').hide();
	}else{
		$('.mobile_search_btn').show();
		if($('.mobile_search_btn').hasClass('mobile_search_open')){
			$('.mobile_search_form').show();
		}
	}	
	
}

function tabletFooter(){
	//if (($(window).width() >= 768) && ($(window).width() < 1024)){
	//if	(Modernizr.mq('screen only and (min-width: 768px) and (max-width:1023px)')){
	if	(Modernizr.mq('(min-width: 768px) and (max-width: 1023px)')){
		if(!$('.footer_medium').length){
			console.log('Tablet footer');
			$('.footer_topnav > li').appendTo('.footer_left');
				var footer_raw = ''; 
				footer_raw = $('.footer_left > li');
				for(var i = 0; i < $(footer_raw).length; i+=3) {
				footer_raw.slice(i, i+3).wrapAll('<li class="footer_medium"><ul ></ul></li>');
			}
			
			$('.social-media').appendTo('.footer_medium:last');
			equalHeight(jQuery('.footer_medium'));
		}	
	}else{		
		$('footer.bottom').remove();
		$(saved).insertAfter('.main');

	}
}


function showSlider(){
	
		$('.slides').slick({
			autoplay: true,
			arrows:false,
			speed : 900,
			autoplaySpeed :5000,
			dots: true
		});

	if (Modernizr.mq('(max-width: 767px)')){
	//if($(window).width() < 768){
		if(!$('.news').find('.slick-initialized').length){
			$('.newsslideritem').parent().slick({
				autoplay: false,
				responsive:true,
				slide: 'article',
				arrows:false,
				dots:true
				//fade:true
			})
		}	
	}else{
		if($('.news').find('.slick-initialized').length){
			//$('.newsslideritem').parent().unslick();
			$('.news').find('.slick-initialized').unslick();
		}
	}	
}

saved = $('footer.bottom').clone().wrap('<div></div>').parent().html();


jQuery(document).ready(function () {
	saved = $('footer.bottom').clone().wrap('<div></div>').parent().html();
	if(!$('html').hasClass('ie8')){
		showDesktopNav();
		tabletFooter();	
		
	//equalHeightBoxes(jQuery('.follow .infoboxes .csc-default'));
	//equalHeightBoxes(jQuery('.follow .kontaktbox'));
	
	equalRowheight(jQuery('.follow .infoboxes .csc-default'));
	equalRowheight(jQuery('.follow .kontaktbox'));
	}
	equalHeightMainCols(jQuery('.follow .columns'));
	equalHeightBoxesHome(jQuery('.home .four'));
	
	if(isFlashEnabled()){
		$('html').addClass('flash');
	}else{
		$('html').addClass('no-flash');
	}
	if ($('.mobile_navi_container').length) {
		$('.head').on('click',' .mobile_navi_container',function(g){
			g.preventDefault();
			
			$('.mobile_search_btn').removeClass('mobile_search_open');
			$('.main-nav > ul').removeClass('main-nav-mobile');
			$('.mobile_search_form').hide();
			
			$('.main-nav > ul').toggle();
			if($('.mobile_navi_container').hasClass('mobile_navi_active')){
				$('.mobile_navi_container').removeClass('mobile_navi_active');
				$('.main-nav > ul').removeClass('main-nav-mobile');
			}else{
				$('.mobile_navi_container').addClass('mobile_navi_active');
				$('.main-nav > ul').addClass('main-nav-mobile');
			}
			
		});
	}
	if ($('.mobile_toggle').length) {
		$('.main-nav').on('click',' .mobile_toggle',function(e){
			e.preventDefault();
			
			if($(this).hasClass('mobile_sub_open')){
				$(this).removeClass('mobile_sub_open');
				$(this).parent().next('.subnav').hide();
				
			}else{
				$('.mobile_toggle').removeClass('mobile_sub_open');
				$('.main-nav .subnav').hide();
				$(this).addClass('mobile_sub_open');
				$(this).parent().next('.subnav').show();
			}	
		});
		
		$('.main-nav').on('click',' .mobile_subtoggle',function(e){
			e.preventDefault();
			
			if($(this).hasClass('mobile_sub_open')){
				$(this).removeClass('mobile_sub_open');
				$(this).parent().next('.subsubnav').hide();
				
			}else{
				$('.mobile_subtoggle').removeClass('mobile_sub_open');
				$('.main-nav .subsubnav').hide();
				$(this).addClass('mobile_sub_open');
				$(this).parent().next('.subsubnav').show();
			}	
		});
		
		
	}
	if ($('.footer_totop > a').length) {
		$('.wrapper').on('click',' .footer_totop > a',function(h){
			h.preventDefault();
			$("html, body").animate({ scrollTop: 0 }, "slow");
		});
	}
	
	
	if($('.mobile_search_btn').length){
		$('.head').on('click','.mobile_search_btn',function(k){
			k.preventDefault();
			
			$('.mobile_navi_container').removeClass('mobile_navi_active');
			$('.main-nav > ul').removeClass('main-nav-mobile');
			$('.main-nav > ul').hide();
			
			$('.mobile_search_form').toggle();
			if($(this).hasClass('mobile_search_open')){
				$(this).removeClass('mobile_search_open');
			}else{
				$(this).addClass('mobile_search_open');
			}
		});	
	}
	
	showSlider();

    if (jQuery('#printContent').length) {
        jQuery('#printContent').click(function() {
            print_specific_div_content()
        });

    }
	
});


$(window).load(function(){
	if(!$('html').hasClass('ie8')){
		
	}
	equalHeightMainCols(jQuery('.follow .columns'));
	equalHeightBoxesHome(jQuery('.home .four'));
	
	equalRowheight(jQuery('.follow .infoboxes .csc-default'));
	equalRowheight(jQuery('.follow .kontaktbox'));
})



$(window).smartresize(function(){
 	if(!$('html').hasClass('ie8')){
		showDesktopNav();
		showDesktopSearch();
		tabletFooter();
		
		
			//equalHeightBoxes(jQuery('.follow .infoboxes .csc-default'));
			//equalHeightBoxes(jQuery('.follow .kontaktbox'));
		
		equalHeightBoxesHome(jQuery('.home .four'));
		showSlider();
		
		equalRowheight(jQuery('.follow .infoboxes .csc-default'));
		equalRowheight(jQuery('.follow .kontaktbox'));

	}
	equalHeightMainCols(jQuery('.follow .columns'));
});


/**
 * toggle FAQ Items
 *
 * @param id        the id of the FAQ item to hide or show
 * @param single    true to show only one item at a time, false the open as many as you want
 */
function tx_irfaq_toggleFaq(id, single, hash) {

	if (single) {
		// show only one Q+A at a time
		tx_irfaq_toggleAll(false, hash);
		tx_irfaq_showHideFaq(id, true, hash);
	}
	else {
		// open as many Q+A as you like
		var hidden = (document.getElementById('irfaq_a_' + id + '_' + hash).className == 'tx-irfaq-dynans-hidden');
		tx_irfaq_showHideFaq(id, hidden, hash);
	}
}

/**
 * shows or hides a FAQ item at a time depending on the given status
 *
 * @param id        the id of the FAQ item to hide or show
 * @param show    true to show the item, false to hide it
 */
function tx_irfaq_showHideFaq(id, show, hash) {
	var faq_id = 'irfaq_a_' + id + '_' + hash; //answer
	var pm_id = 'irfaq_pm_' + id + '_' + hash; // plus/minus icon

	if (show) {
		document.getElementById(faq_id).className = 'tx-irfaq-dynans-visible';
		document.getElementById(pm_id).src = tx_irfaq_pi1_iconMinus;
	}
	else {
		document.getElementById(faq_id).className = 'tx-irfaq-dynans-hidden';
		document.getElementById(pm_id).src = tx_irfaq_pi1_iconPlus;
	}
}

/**
 * shows or hides all FAQ items with one click
 *
 * @param mode    true to show the items, false to hide them
 */
function tx_irfaq_toggleAll(mode, hash, count) {
	for (i = 0; i < count; i++) {
		tx_irfaq_showHideFaq(i + 1, mode, hash);
	}
}



function print_specific_div_content(){
    var win = window.open('','','left=0,top=0,width=670,height=477,toolbar=0,scrollbars=1,status =0');
    var content = "<html>";
    content += "<head>";
    content += '<link href="/typo3conf/ext/bgm_theme_sites/css/default.css" rel="stylesheet" type="text/css" /><style type="text/css">body {background-image:none;}div#printContent{display:none}</style>';
    content += '<link href="/typo3conf/ext/bgm_theme_sites/css/update.css" rel="stylesheet" type="text/css" />';
    content += '<base href="https://www.vsa.de/">';

    content += "</head>";
    content += "<body>";
    content += '<div class="content" style="width:620px;padding:10px;">';
    content += '<div id="sitelogo" style="width:600px;text-align:right;clear:both;"><img src="/fileadmin/new2012/images/vsa_sublogo.png" alt="" border=0" /></div><div style="clear:both;">';
    content += '<div id="printlink" style="clear:both;width:100px;margin-left:auto;margin-right:auto;margin-top:15px;margin-bottom:15px;"><form><input style="border:none;background-color:#fff" type="button" value="Seite drucken" onClick="window.print();widow.close()"></form></div>';
    content += jQuery('section.content').html() ;
    content += "</div>";
    content += "</body>";
    content += "</html>";
    win.document.write(content);
    win.window.location.reload();    // this is the secret ingredient
    win.focus();                     // not sure if this line is necessary
    win.print();
    win.document.close();
}