APP._onReady = function() {
  
	$("select").each(function(){
		if (window.innerWidth > 480) {
	  	
			$(this).select2({
		      minimumResultsForSearch: Infinity,
		      dropdownAutoWidth: 'true'
		    });
		  
			var $eventSelect = $(this);
			if($eventSelect.select2('data')[0].text){
				$eventSelect.parent().find("label").addClass("not-empty-select");
			}
			
			$eventSelect.on("change", function (e) { $eventSelect.parent().find("label").addClass("not-empty-select"); });
		}
	})
	
	if($(".js-scrollTo").length){
		if($(window).width() < 768) {
  			$('html, body').animate({ scrollTop: $("body").find(".js-scrollTo").first().offset().top-$(".header-wrapper").height() }, 'slow');
		} else{
			console.log("test")
  			$('html, body').animate({ scrollTop: $("body").find(".js-scrollTo").first().offset().top-$(".header-wrapper").height()+ 60 }, 'slow');
		}
  	}
/*   
$(window).resize(function() {
  if (window.innerWidth > 480) {
     if ($("select").length > 1) {  	
      $("select").select2({
        minimumResultsForSearch: Infinity,
        dropdownAutoWidth: 'true'
      });
    } else {
       $("select").select2('destroy'); 
    }
  }
});
*/
	
	$.each(APP._ready, function(i, fn) {
		fn();
	});
	APP._ready = null;
};

APP._onLoad = function() {
	var header, options, $cookie, $cookieClose, $selectEvent;
  
	APP.showHideMobileHeader.init();
	APP.showHide.init();
	APP.rows.init();
  APP.mobileTable.init();
	
	 /*$(".accordion").tabs(
		".accordion div.pane",
	    {
	    	
	    	tabs: 'h3.js-main-acc', 
	    	effect: 'slide', 
	    	initialIndex: null,
	    	//initialIndex: 5,
	    	onClick: function(event, tabIndex) {
				var thisTab = this.getCurrentTab();
    			$('html, body').scrollTop({top: thisTab.offset().top - thisTab.outerHeight(), behavior: 'smooth'});
			}
	    }
	);*/
	
	$('.accordion h3.js-main-acc').on('click', function(e) {
	    e.preventDefault();
	    $(this).toggleClass("current");
	    var dropdownName = $(this).find("span").html();
	    $(this).next('.accordion div.pane').not(':animated').slideToggle();
	    if(window.outerWidth < 500) {
		    $('html, body').animate({
		        scrollTop: $(this).offset().top-60
		    }, 300);
	    }
	    
	    var eventActionText = "Klik Rozbalovací položka '" + dropdownName + "'";
	    
	    if($(this).parent().parent().parent().is("#list-accordion")){
		    if($(this).hasClass("current")) {
		    	dataLayer.push({event:"gtm.click"
	                ,eventCategory: "User Scoring"
	                ,eventAction: eventActionText
				})
		    }
	    }
	    
	    var eventActionTextFaq = "Klik FAQ '" + dropdownName + "'";
	    
	    if($(this).parent().parent().parent().is("#list-accordion-faq")){
		    if($(this).hasClass("current")) {
		    	dataLayer.push({event:"gtm.click"
	                ,eventCategory: "User Scoring"
	                ,eventAction: eventActionTextFaq
				})
		    }
	    }
	  });
	
	$('.js-branch-tabs').tabs(
		"div.tab-bodies > div"
	);
	
	$(".tabs").tabs(
		"div.tab-bodies > div.tab-body-wrapper",
		{
			initialIndex: -1
		}
	);
	
	$('.js-date-range').datepicker({
		format: "dd.mm.yyyy",
		weekStart: 1,
		endDate: "0d",
		orientation: "bottom"
	});
	
	var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1;
    var yyyy = today.getFullYear();
    var hours = today.getHours();
    
    var currentHour = hours;
    var explicitDay = "";
    var newDatesDisabled = [
    	"9/1/2018"
    	];
    
 
    var datesDisabled = [
	    "30/3/2018",
	    "1/4/2018",
	    "1/5/2018",
	    "8/5/2018",
	    "5/7/2018",
	    "6/7/2018",
	    "28/9/2018",
	    "28/10/2018",
	    "17/11/2018",
	    "24/12/2018",
	    "25/12/2018",
	    "26/12/2018",
	    "1/1/2019"
	];

	
/*	$('#id_call-time').change(function() {
		explicitDay = "";
		newDatesDisabled = [];
		$('.js-date').val("");
		$('.js-calc-date').val("");
		
		if($(this).val() == "zavolame-1") {
			newDatesDisabled = datesDisabled.slice();
			
			if(parseInt(currentHour) >= 12) {
			    explicitDay = dd+'/'+mm+'/'+yyyy;
			    newDatesDisabled.push(explicitDay);
		    } 

		    $('.js-date').datepicker('remove');
		    $('.js-calc-date').datepicker('remove');
		    
		    setDatePicker(newDatesDisabled);
		    setCalcDatePicker(newDatesDisabled);

		} else {
			newDatesDisabled = datesDisabled.slice();
			if(parseInt(currentHour) >= 16) {
			    explicitDay = dd+'/'+mm+'/'+yyyy;
			    newDatesDisabled.push(explicitDay);
		    } 
			
			$('.js-date').datepicker('remove');
			$('.js-calc-date').datepicker('remove');
			setDatePicker(newDatesDisabled);
			setCalcDatePicker(newDatesDisabled);
		}		
	});
*/
	
    //inicializace datepickru po nacteni stranky
    if($('#id_call-time').val() == "zavolame-1") {
    	newDatesDisabled = datesDisabled.slice();
    	
    	if(parseInt(currentHour) >= 12) {
		    explicitDay = dd+'/'+mm+'/'+yyyy;
		    newDatesDisabled.push(explicitDay);
	    } 
		setDatePicker(newDatesDisabled);
		setCalcDatePicker(newDatesDisabled);
	    
    } else {
    	newDatesDisabled = datesDisabled.slice();
    	if(parseInt(currentHour) >= 16) {
		    explicitDay = dd+'/'+mm+'/'+yyyy;
		    newDatesDisabled.push(explicitDay);
	    }
    	
		setDatePicker(newDatesDisabled);
		setCalcDatePicker(newDatesDisabled);
    }

	function setDatePicker(datesDisabled) {
		$('.js-date').datepicker({
			format: "dd.mm.yyyy",
			weekStart: 1,
			endDate: '+3d',
			orientation: "bottom",
			datesDisabled: datesDisabled
		});
	}
	
	function setCalcDatePicker(datesDisabled) {
		$('.js-calc-date').datepicker({
			format: "dd.mm.yyyy",
			weekStart: 1,
			orientation: "bottom"
		});
	}

	$(document).on("change", ".js-date", function(){
		var date = new Date();
		var month = (date.getMonth()+1);
		if (month < 10) {
			var monthFormat = String("0" + month);
		} else {
			monthFormat = month;
		}
		var currentdate = new Date(); 
		var minutes;
		if (date.getMinutes() < 10) {
			minutes = String("0", date.getMinutes())
		}else {
			minutes = date.getMinutes();
		}
	    var datetime = date.getHours() + "00" + minutes;
		var today = date.getDate()+"."+monthFormat+"."+date.getFullYear();
		var pickedDay = $(this).val();
		console.log(datetime);
		if ((today === pickedDay) && (datetime > 110000)){
			
			if($("#id_call-time").val() ==="MORNING"){
				$('option[value="MORNING"]').prop('disabled', !$('option[value="MORNING"]').prop('disabled'));
				$("#id_call-time").val(null).trigger('change.select2');
				$("#id_call-time").select2();
				$("label[for='id_call-time']").removeClass("not-empty-select");
			} else{
				$('option[value="MORNING"]').prop('disabled', !$('option[value="MORNING"]').prop('disabled'));
				$("#id_call-time").select2({
					minimumResultsForSearch: Infinity,
	    			dropdownAutoWidth: 'true'
				});
			}
		} else {
			$('option[value="MORNING"]').prop('disabled',false, !$('option[value="MORNING"]').prop('disabled', false));
			$("#id_call-time").select2({
				minimumResultsForSearch: Infinity,
    			dropdownAutoWidth: 'true'
			});
		};
	});
  
  	$("a.external").each(function(i) {
    	$(this).attr("target", "_blank");
  	});
  	
  	$('a[href*=\\#]:not([href=\\#])').click(function() {
	    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var target = $(this.hash);
			target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
			if (target.length) {
				$('html,body').animate({
					scrollTop: target.offset().top+40
				}, 1000);
	        	return false;
			}
	    }
	});
	
	if($('.js-scroll-to').length) {
		if ($('.js-scroll-to').data("offset")){
			var offsetTop = $('.js-scroll-to').data("offset");
		} else {
			var offsetTop = 0;
		}

	    $('html, body').animate({
	        scrollTop: $('.js-scroll-to').offset().top - offsetTop
	    }, 'slow');
	}

  	
  	$(".js-open-search").click(function() {
  		$(this).addClass("flipHide");
  		
  		var heightTop;
  		
  		if($(".js-pruh.js-promo").is(":visible")){
  			var heightTop = $(".js-pruh.js-promo").outerHeight() + $("#header").outerHeight();
  		} else {
  			var heightTop = $("#header").outerHeight();
  		}
  		
  		$(".searching-wrapper").css({
  			top: heightTop
  		})
  		
  		$(".search-overlay").css({
  			top: heightTop
  		})
  		
  		$(".search-overlay").fadeIn();
  
  		$('.searching-wrapper').slideDown(400, function(){
  			$(".js-d-search").first().focus();
  		});
  		if($(window).width()<480){
  			$("a.xs-btn-toggle.js-close-search").show();
  			$("span.xs-btn-toggle.js-close-search").show();
  			
  			$("button.js-mobile-header.open").hide();
  		}
	});
	
	$(".js-close-search").click(function() {
  		$('.searching-wrapper').fadeOut(function(){
    		$(".js-open-search").removeClass("flipHide")
    		$(".js-d-search").first().blur();
  		});
  		$(".search-overlay").fadeOut("slow");
  		if($(window).width()<480){
  			$("a.js-close-search").hide();
  			$("span.js-close-search").hide();
  			$("button.js-mobile-header.open").show();
  		}
	});
	
	$(".search-overlay").click(function(){
			$('.searching-wrapper').slideUp( 400, function(){
    		$(".js-open-search").removeClass("flipHide")
    		$(".js-d-search").first().blur();
  		});
  		$(this).slideUp(400);
  		if($(window).width()<480){
  			$("a.js-close-search").hide();
  			$("button.js-mobile-header.open").show();
  		}
	})
/*	
	$('.js-deactive-ch').change(function() {
		$('#js-deactive').find('.slice-col').each(function () {
			if($(this).css('opacity') == "0.2" )  { 
			    $(this).removeAttr('href');
			}
		});
	});
  	*/
  	$('.js-slidedown').on('click', function() {
		var selector = $(this).data('selector');
		
		$(selector).slideToggle();
	});
	
/**				var last_location = $.cookie('last_location'),
			    current_location = document.URL;
			
			//Initial check if cookie is set and not equal to current location
			if (last_location && last_location !== current_location) {
			    window.location = last_location;
			}
			
			//Set pattern
			var pattern = new RegExp(window.location.host);
			
			//On any link click test if the href does not have a match with the pattern
			$('a').click(function() {
			    var href = $(this).attr('href');
			    if (pattern.test(href) !== true) {
			        //If no match found, the link is external and adding current url to the cookies
			        $.cookie('last_location', document.URL, {
			            expires: 1,
			            path: '/'
			        });
			        console.log($.cookie)
			    } else {
			        //Else if link is internal remove cookie and continue as normal
			        $.removeCookie('last_location', {
			            path: '/'
			        });
			    }
			});**/
  	
  	$cookie = $("#js-cookies-stripe");
  	$cookieClose = $("#js-cookie-close");
  	$emergency = $(".js-promo");
  	$emergencyClose = $(".js-promo-close.emergency-close");
  	$infoClose = $(".js-promo-close.info-close");
  	$emergencyInfo = $(".js-pruh-emergency");
  	$infoInfo = $(".js-pruh-info");
  	//$emergencyInfo = $(".js-pruh").eq(1);
  	$emergencyPop = $(".js-tlacitko");
  	$emergencyLista = $('.js-odd-promo');
	$searchCookie = $('.searching-wrapper button');
  	$cookieClose.click(function() {
  		Cookies.set('stripe', 'true');
  		$cookie.hide();
	});
	
	$emergencyClose.click(function() {
  		Cookies.set('emergency', 'true', { expires: 1 });
  		$emergencyInfo.slideUp(200);
  		/*$(".header-wrapper").removeClass("emergency-head").addClass("emergency-small-head");
  		$emergency.slideToggle(function() {
  			$emergencyLista.show();
  			$emergencyPop.slideDown(200).parent().parent().addClass("emergency-closed").removeClass("emergency-open");
  		});*/
	});
	
	$infoClose.click(function() {
  		Cookies.set('info', 'true', { expires: 1 });
  		$infoInfo.slideUp(200);
	});
	
	$emergencyPop.click(function() {
  		$emergency.slideToggle(function() {
  			$emergencyPop.slideUp(200).parent().parent().addClass("emergency-open").removeClass("emergency-closed");
  		});
	});
	/*if($(".js-pruh.js-promo").length){
		if(!($(".js-pruh.js-promo").is(":visible"))){
			$(".header-wrapper").addClass("emergency-closed");
		} else if($(".js-pruh.js-promo").is(":visible")){
			$(".header-wrapper").addClass("emergency-open");
		}
	}*/
	
 /* 	if (Cookies.get('stripe')) {
  		$cookie.hide();
  	} else {
  		$cookie.show();
  	}*/
  
  	$.each(APP._load, function(i, fn) {
    	fn();
  	});
  	APP._load = null;
};

$(APP._onReady);

$(document).ready(function(){
	$(window).on("load", APP._onLoad);	
})