var $ = null;
var currentWindowWidth = null;

var personTypeClones = null;
var personFagomradeClones = null;
var personRegionClones = null;
var personByClones = null;
var traeningTypeClones = null;
var traeningFacilitetClones = null;

var ref_galleri_count = 0;
var ref_active_quote = null;
var ref_current_margin_left = 0;

jQuery(function() {
    $ = jQuery;
   
    currentWindowWidth = $(window).width();

    setTimeout(function() {
        if($('#wpvq-final-score:visible').length > 0) {
            console.log(12);
            $('.after-news-banner-mobile').remove();
            $('.after-news-banner-desktop').remove();
        }
        
//        if($('.wpvq').length > 0 && $('div.wpvq-single-page:visible').attr('id') !== 'wpvq-page-0') {
//            $('.banner.sub-header').remove();
//        }
    }, 1000);

    traeningTypeClones = $('#traeningType').find('option').not(':first');
    traeningFacilitetClones = $('#traeningFacilitet').find('option').not(':first');
    $('#traeningType').on('change', function(e) {
       var typeType = $(this).val();
       if(typeType != '')
       {
          if($(this).find(':selected').data('ttypefaciliteter') !== '')
          {
             var ttypefaciliteter = $(this).find(':selected').data('ttypefaciliteter').split(',');

             $('#traeningFacilitet option').not(':first').remove();
             for(var name in ttypefaciliteter)
             {
                var facilitet_parts = ttypefaciliteter[name].split('=');

                $('#traeningFacilitet').append('<option value="'+facilitet_parts[1]+'">'+facilitet_parts[0]+'</option>');
             }
          } else
          {
             $('#traeningFacilitet option').not(':first').remove();
             $('#traeningFacilitet').append(traeningFacilitetClones);
          }
       } else
       {
          $('#traeningType option').not(':first').remove();
          $('#traeningType').append(traeningTypeClones);

          $('#traeningFacilitet option').not(':first').remove();
          $('#traeningFacilitet').append(traeningFacilitetClones);
       }

 //      fgItemSearch.filterSearchCriterias(true);
    });

    if($('.item-referencer').length > 0) {
       var quotes = $('.item-referencer-wrapper blockquote');
       var quote_width = $(quotes[0]).outerWidth();
       var visible_quotes_count = $('.item-referencer-wrapper').width() / $(quotes[0]).outerWidth();
       var min_margin_left = (quotes.length - visible_quotes_count) * $(quotes[0]).outerWidth() * -1;
       var max_margin_left = 0;
       ref_current_margin_left = parseInt($('.item-referencer-quotes').css('marginLeft'));

       $('.item-referencer-quotes').css('width', $(quotes[0]).outerWidth() * quotes.length);

       $('.arrow').on('click', function(e) {
          var new_margin = ref_current_margin_left;

          if($(this).hasClass('right')) { console.log((ref_current_margin_left - quote_width));
             if((ref_current_margin_left - quote_width) >= min_margin_left) {
                new_margin = ref_current_margin_left - quote_width;
             }
          } else if ($(this).hasClass('left'))
          { 
             if((ref_current_margin_left + quote_width) <= max_margin_left) {
                new_margin = ref_current_margin_left + quote_width;
             }
          }

          if(new_margin !== ref_current_margin_left) {
             ref_current_margin_left = new_margin;

             $('.item-referencer-quotes').animate({ marginLeft: ref_current_margin_left+'px' }, 1000, 'swing')
          }
       });
    }

    if($(".fg_gallery").length > 0)
       $(".fg_gallery").fancybox();

    if($('.open-person-contact').length > 0 && $('.wpcf7-form').length > 0)
    {
       $('.open-person-contact').on('click', function(e) {
          $('.item-contactform:not(:visible)').slideDown();

          if($(this).hasClass('top')) {
             var target = $('div.wpcf7');

             // Does a scroll target exist?
             if (target.length) {
                // Only prevent default if animation is actually gonna happen
                event.preventDefault();

                $('html, body').animate({
                   scrollTop: target.offset().top - 60
                }, 1000, function() {
                   // Callback after animation
                   // Must change focus!
                   var $target = $(target);
                   $target.focus();
                   if ($target.is(":focus")) { // Checking if the target was focused
                      return false;
                   } else {
                      $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
                      $target.focus(); // Set focus again
                   };
                });
             }
          }
       });
    }
});

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}