var c = {
	calendar : {
		config : {
			calendar_container : undefined,
			date_list_with_event : [],
			module_url : window.location.hostname === 'amuz.wroc.pl' ? '/kalendarium-3984' : window.location.hostname === 'en.amuz.wroc.pl' ? '/calendar-3984' : '',
			start_date : undefined
		},
		init : function() {
			c.calendar.config.calendar_container = $('#calendar_view_container');
			if (c.calendar.config.calendar_container.length === 1) {
				c.calendar.config.calendar_container.find('div')
					.datepicker(
						{
							language : LANG,
							format : 'yyyy-mm-dd'
						}
					)
					.on(
						'changeDate',
						function(e) {
							var redirect_url = c.calendar.config.module_url + '/?date=' + [e.date.getFullYear(), c.utility.add_leading_zero(e.date.getMonth()+1, 2), c.utility.add_leading_zero(e.date.getDate(), 2)].join('-')
							document.location.href = redirect_url;
						}
					)
					.on(
						'changeMonth',
						function(e) {
							c.calendar.utility.mark_all_day_with_event();
							c.calendar.utility.mark_all_day_with_past_event();
						}
					)
					.on(
						'changeYear',
						function(e) {
							c.calendar.utility.mark_all_day_with_event();
							c.calendar.utility.mark_all_day_with_past_event();
						}
					)
					.on(
						'changeDecade',
						function(e) {
							c.calendar.utility.mark_all_day_with_event();
							c.calendar.utility.mark_all_day_with_past_event();
						}
					)
					.on(
						'changeCentury',
						function(e) {
							c.calendar.utility.mark_all_day_with_event();
							c.calendar.utility.mark_all_day_with_past_event();
						}
					)
				;
				c.calendar.utility.mark_all_day_with_event();
				c.calendar.utility.mark_all_day_with_past_event();
				$('#calendar_view_container > div').datepicker('update', c.calendar.config.start_date);
			}
		},
		utility : {
			mark_all_day_with_event : function() {
				$.each(c.calendar.config.date_list_with_event, function(k, v) {
					c.calendar.config.calendar_container.find('.day[data-date="'+v+'"]:not(.active)').addClass('background-color-tertiary');
				});

				setTimeout(function() {
					$.each(c.calendar.config.date_list_with_event, function(k, v) {
						c.calendar.config.calendar_container.find('.day[data-date="'+v+'"]:not(.active)').addClass('background-color-tertiary');
					});
				}, 10);
			},
			mark_all_day_with_past_event : function() {
				$.each(c.calendar.config.date_list_with_past_event, function(k, v) {
					c.calendar.config.calendar_container.find('.day[data-date="'+v+'"]:not(.active)').addClass('background-color-gray');
				});

				setTimeout(function() {
					$.each(c.calendar.config.date_list_with_past_event, function(k, v) {
						c.calendar.config.calendar_container.find('.day[data-date="'+v+'"]:not(.active)').addClass('background-color-gray');
					});
				}, 10);
			}
		}
	},
	disabled_person_view : {
		init : function() {
			$('body').on('click', '.enable-high-contrast-btn', function(e) {
				e.preventDefault();
				$(this).removeClass('enable-high-contrast-btn');
				$(this).addClass('enable-standard-contrast-btn');
				$(this).find('img').attr('src', '/img/icon-standard-version.png');
				$(this).attr('title', 'widok standardowy');
				c.disabled_person_view.enable_black_and_yellow();
				$.cookie('website_view_version', 'high-contrast', { path: '/' });
			});

			$('body').on('click', '.enable-standard-contrast-btn', function(e) {
				e.preventDefault();
				$(this).addClass('enable-high-contrast-btn');
				$(this).removeClass('enable-standard-contrast-btn');
				$(this).find('img').attr('src', '/img/icon-black-and-yellow-version.png');
				$(this).attr('title', 'widok z wysokim kontrastem');
				c.disabled_person_view.enable_standard();
				$.cookie('website_view_version', 'standard', { path: '/' });
			});

			if ($.cookie('website_view_version') !== undefined && $.cookie('website_view_version') === 'high-contrast') {
				$('.enable-high-contrast-btn').click();
			}
		},
		enable_black_and_yellow : function() {
			$('body').addClass('black-and-yellow');
			$('.header-logo img.logo-default').attr('src', '/img/logo-poziome-godlo-black-and-yellow.png');
			$('.header-logo img.logo-small').attr('src', '/img/logo-poziome-godlo-only-2-black-and-yellow.png');
		},
		enable_standard : function() {
			$('body').removeAttr('class');
			$('.header-logo img.logo-default').attr('src', '/img/logo-poziome-godlo.png');
			$('.header-logo img.logo-small').attr('src', '/img/logo-poziome-godlo-only-2.png');
		}
	},
	init_cookie : function() {
		var cookie_right_id = 'cookie-2018-09-30'

		if ($.cookie(cookie_right_id) === undefined || $.cookie(cookie_right_id) !== 'accepted') {
			$('#cookie_container').fadeIn(100);
		}

		$('#cookie_container a#cookie_accept_btn').click(function(e) {
			e.preventDefault();
			$.cookie(cookie_right_id, 'accepted', { path: '/' });
			$('#cookie_container').hide();
		});
	},
	init_blinking : function() {
		setInterval(function() {
			$('.blinking').animate(
				{
					opacity: 0.4
				},
				500,
				function() {
					$(this).animate(
						{
							opacity: 1
						},
						500,
						function() {
						}
					);
				}
			);
		}, 1100);
	},
	init_external_link : function() {
		$.expr[':'].external = function(obj){
			return !obj.href.match(/^mailto\:/)
				   && (obj.hostname != location.hostname)
				   && (obj.hostname != 'dynamic-content.amuz.wroc.pl')
				   && (obj.hostname != 'static-content.amuz.wroc.pl')
				   && (obj.hostname != 'amuz.wroc.pl')
				   && (obj.hostname != 'en.amuz.wroc.pl')
				   && !obj.href.match(/^javascript\:/)
				   && !obj.href.match(/^$/)
		};
		$('a:external').attr('target', '_blank');
	},
	init_homepage_owl_carousel_height() {
		const owl = $('#owl-news-carousel');
		owl.on('initialized.owl.carousel', function(event) {
			let maxHeight = 0;
			const posts = $('#owl-news-carousel .post--content');
			$.each(posts, function(index, item) {
				const itemOuterHeight = $(item).outerHeight();
				if (maxHeight < itemOuterHeight) {
					maxHeight = itemOuterHeight;
				}
			});
			posts.height(Math.ceil(maxHeight));
		})
	},
	filters: {
		config: {
			path: '',
		},
		init: () => {
			const container = $('#available_filters_container');

			if (container.length === 0) return;

			c.filters.config.path = available_filters_initial_path;

			function setFilterPath(step) {
				const maxSelectNumber = 3;
				let pathSegments = [];
				for (let i = 0; i < maxSelectNumber; i++) {
					const select = container.find(`select.level-${i}`);
					if (i > step || select.val().length === 0) { continue };
					if (select.length === 1) {
						pathSegments.push(select.val());
					}
				}

				c.filters.config.path = pathSegments.join(` / `);
			}

			function render() {
				container.html(``);
				const filterPathElements = c.filters.config.path.split(' / ');

				const select = $('<select />', {class:`level-0`, 'data-step': 0});
				select.append('<option value="">Filtruj ...</option>');
				for (const filter in available_filters) {
					let selected = '';
					// alert(filterPathElements[0]);
					if (filter === filterPathElements[0]) {
						selected = ` selected="selected"`;
					}
					select.append(`<option value="${filter}" ${selected}>${filter}</option>`);
				}

				container.append(select);

				if (filterPathElements.length >= 1 && filterPathElements[0].length > 0) {
					let counter = 1;
					for (const filterPathElement of filterPathElements) {
						let select;
						let currentLevelFilters = [];
						switch (counter) {
							case 1:
								currentLevelFilters = available_filters[filterPathElements[counter - 1]];
								break;
							case 2:
								currentLevelFilters = available_filters[filterPathElements[counter - 2]][filterPathElements[counter - 1]];
								break;
						}

						if ($.isEmptyObject(currentLevelFilters)) {
							continue;
						}

						select = $('<select />', {class:`level-${counter}`, 'data-step': counter});
						select.append('<option value="">Filtruj ...</option>');
						for (const filter in currentLevelFilters) {
							let selected = '';
							if (filter === filterPathElements[counter]) {
								selected = ` selected="selected"`;
							}
							select.append(`<option value="${filter}" ${selected}>${filter}</option>`);
						}

						container.append(select);
						counter++;
					}
				}

				container.append('<button class="btn btn-xs btn-primary ml-2">Filtruj</button>');
			}

			container.on('change', 'select', function(e) {
				const step = parseInt($(this).data('step'));
				setFilterPath(step);
				render();
			});

			container.on('click', 'button', function(e) {
				e.preventDefault();
				const currentPath = window.location.pathname;
				const currentPathParts = currentPath.split(`/`);
				const finalLocation = `https://${window.location.host}/${currentPathParts[1]}`;
				if (c.filters.config.path.length === 0) {
					document.location.href = `${finalLocation}`;
					return;
				}
				document.location.href = `${finalLocation}/?filter=${available_filters_base} / ${c.filters.config.path}`;
			})

			render();
		}
	},
	utility : {
		add_leading_zero : function (v, l) {
			v = String(v);
			while (v.length < l) {
				v = '0' + v;
			}

			return v;
		}
	},

};
$(document).ready(function() {
	c.init_blinking();
	c.init_external_link();
	c.calendar.init();
	c.disabled_person_view.init();
	c.init_cookie();
	c.filters.init();
	c.init_homepage_owl_carousel_height();
});
