
    
$( document ).ready(function() {

	bind_loader();

	console.log($(window).width())
	if($(window).width() < 767){
		$('.navbar-collapse').css("width",$(window).width()+"px");
		$(window).resize(function (){
			$('.navbar-collapse').css("width",$(window).width()+"px");
		})
	}


	// When the user clicks anywhere outside of the modal, close it
	/*window.onclick = function(event) {
		if (event.target == modal) {
			modal.style.display = "none";
		}
	}*/

	$('#search_mobile_btn').on("click", function () {
		$('.mobile_search').toggle();
	});

	if($(window).width() < 991 && $(window).scrollTop() > 300){
		$('#search_mobile_btn').show();
	}

	$(window).scroll(function(e){
		console.log($(window).width());
		if($(window).width() > 991){
			var $el = $('.fixedElement');
			var isPositionFixed = ($el.css('position') == 'fixed');
			if ($(this).scrollTop() > 300 && !isPositionFixed){
				$el.css({'position': 'fixed', 'top': '0px','left':'0px','right': '0px','z-index':'99'});
				$('.search_box').css('margin-top','0px');
			}
			if ($(this).scrollTop() < 300 && isPositionFixed){
				$el.css({'position': 'static', 'top': '0px'});
				$('.search_box').css('margin-top','120px');
			}
		}else{

			var $el = $('.fixedElement');
			if ($(this).scrollTop() > 300){
				$el.hide();
				$('#search_mobile_btn').show();
			}
			if ($(this).scrollTop() < 300){
				$el.show();
				$('#search_mobile_btn').hide();
				$('.mobile_search').hide();
			}

		}
	});

	//SELECT 2 FOR COUNTRY AND REGION SEARCH//
	$('.country_selector').select2({
		templateResult: formatState,
		placeholder: selectCountry,
		minimumResultsForSearch: Infinity
	});

	$('.region_selector').select2({
		placeholder: selectRegion,
		minimumResultsForSearch: Infinity,
		disabled: true
	});

	$('.select_new').select2({
		minimumResultsForSearch: Infinity
	});


	function formatState (state) {
		if (!state.id) {
			return state.text;
		}
		var baseUrl = GlobalJSBasePath+"img/countryflags";
		var $state = $(
			'<span><img class="img-flag" style="height: 20px;"/> <span></span></span>'
		);
		$state.find("span").text(state.text);
		$state.find("img").attr("src", baseUrl + "/" + state.element.dataset.iso + ".png");

		return $state;
	};

	$('.country_selector').on('select2:select', function (e) {
		var data = e.params.data;
		var href = data.element.dataset.href;

		$('.country_select_id').val(data.id);
		$('#search_form').attr("action", href);
		$('.region_selector').select2('destroy');
		$('.region_selector').select2({
			placeholder: selectRegion,
			minimumResultsForSearch: Infinity,
			disabled: false,
			ajax: {
				url: GlobalJSBasePath +"ajaxcore/country_regions.php",
				data: function (params) {
					var query = {
						country_id: data.id,
						term: params.term
					}
					return query;
				},
				dataType: 'json',
				processResults: function (result) {
					// Transforms the top-level key of the response object from 'items' to 'results'
					return {
						results: result
					};
				}
			},
		});
	});
	$('.region_selector').on('select2:select', function (e) {
		var data = e.params.data;
		var href = data.href;
		$('.region_select_id').val(data.id);
		$('#search_form').attr("action", href);
	});

     //-----------------------------------------------------//

    jQuery('.numbersOnly').keyup(function () { 
        this.value = this.value.replace(',','.');
        //this.value = this.value.replace(/[^0-9\.]/g,'');
        this.value = this.value.replace(/(?!^-)[^0-9.]/g,'');
        
        
    });

    $(function() {
	   $('.datepicker').datepicker({
		       prevText: '&#x3c;zurück', prevStatus: '',
		        prevJumpText: '&#x3c;&#x3c;', prevJumpStatus: '',
		        nextText: 'Vor&#x3e;', nextStatus: '',
		        nextJumpText: '&#x3e;&#x3e;', nextJumpStatus: '',
		        currentText: 'heute', currentStatus: '',
		        todayText: 'heute', todayStatus: '',
		        clearText: '-', clearStatus: '',
		        closeText: 'schließen', closeStatus: '',
		        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'],
		        dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		        dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
		        dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			    showMonthAfterYear: false,			      	      			      
			    dateFormat:'dd.mm.yy'
		    } );
	  });
 	
});


      

function bind_auto_complete()
{

	    $(".search-field").autocomplete({
	        minLength: 2,
			source: GlobalJSBasePath +"ajaxcore/campsites/search.php",
	        focus: function (event, ui) {
	            $("#project").val(ui.item.label);
	            return false;
	        },
	        select: function (event, ui) 
	        {
				$('.search-field').val(ui.item.label);
	        	if(ui.item.type=="campsite") 
		        {
		         	$('.search_campsite_id').val(ui.item.id); // save selected id to hidden input
		         	$('.target_link').val(ui.item.target_link); // save selected id to hidden input
		        }

		        //window.location.href=ui.item.target_link;
	            //$("#project").val(ui.item.label);
	            //$("#project-id").val(ui.item.value);
	            //$("#project-description").html(ui.item.desc);
	            //$("#project-icon").attr("src", "images/" + ui.item.icon);

	            return false;
	        },
			response: function (event,ui){
				console.log(ui.content);
			},
			open: function() {
				$("ul.ui-menu").width( $(this).innerWidth() );
			}
	    })
        .data("ui-autocomplete")._renderItem = function (ul, item) {
        return $("<li>")
            .data("data-id", item.id)
            .append("<a>"+ item.label + "</a>")
            .appendTo(ul);
    };

	

}

function bind_auto_complete_save()
{
	 $('#search-field').focus();
	 $('#search-field').autocomplete({
       source: GlobalJSBasePath +"ajaxcore/campsites/search.php",
       html: true, 
       select: function (event, ui) 
       {
         	$('#search-field').val(ui.item.label); // display the selected text
	         if(ui.item.type=="campsite") 
	         {
	         	$('#search_campsite_id').val(ui.item.id); // save selected id to hidden input
	         	$('#search_country_id').val();
	         }
	         else {
	     		$('#search_campsite_id').val(); // save selected id to hidden input
	         	$('#search_country_id').val(ui.item.id);
	         }
         return false;
	     },

	    change: function( event, ui ) {
	        
	    } 
	});

}

String.prototype.nl2br = function()
{
    return this.replace(/\n/g, "<br />");
}


jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + 
                                                $(window).scrollTop()) + "px");
    this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + 
                                                $(window).scrollLeft()) + "px");
    return this;
}

function filterCountry(country = "0",link){
	console.log(link)
	$(".btn").each(function (){
		$(this).removeClass("active");
	});
	$.ajax({
		method: "POST",
		url: GlobalJSBasePath+"ajaxcore/campsites/campsite_country.php",
		data: { country_id: country,baselink:link },
		dataType: "html"
	})
	.done(function( data ) {
		$('#country_content').html(data);
		var url = GlobalJSBasePath+''+link.substr(1);
		var obj = { Title: country, Url: url };
		history.pushState(obj, obj.Title, obj.Url);
	});
	$("#countryBtn_"+country).addClass("active");

}

function show_loader(loader_message)
{
	if(isBlank(loader_message))
	{
		$(".loading_message").html($(".loader_default").html());
	}
	else{
		$(".loading_message").html(loader_message);
	}

	$(".overlay-loader").show();
}

function isBlank( data ) {
	return ( $.trim(data).length == 0 );
}

function hide_loader()
{
	$(".overlay-loader").hide();
}

function bind_loader()
{
	$("form.form_loader_display").submit(function (e) {
		var message_value = $(this).attr("loader_message");
		show_loader(message_value) ;
	});

	$(".loader_display").click(function () {
		var message_value = $(this).attr("loader_message");
		show_loader(message_value) ;
	});

	$(document).ajaxStop(function() {
		if(auto_hide_loader)
		{
			hide_loader() ;
		}

	});
}

function rebind_loader()
{
	$(".loader_display").click(function () {
		var message_value = $(this).attr("loader_message");
		show_loader(message_value) ;
	});
}