<?php
add_filter( 'wp_enqueue_scripts', 'my_pikaday_legend' );
function my_pikaday_legend() {
	if(page_id() === 3497) {
		
		?>
		<script>
		// START JAVASCRIPT	
		document.addEventListener("DOMContentLoaded", function(event) {			
			var picker = new Pikaday(
			{	
				field: document.getElementById('datepicker'),
				bound: false,
				container: document.getElementById('pikaday-container'),				
				disableWeekends: true,
				numberOfMonths: 3,
				firstDay: 1,
				minDate: new Date(new Date().setDate(new Date().getDate())),
				maxDate: pikaday_maxDate(),				
				
				i18n: {
					previousMonth: 'Vormonat',
					nextMonth: 'Nächster Monat',
					months: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
					weekdays: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
					weekdaysShort: ['So.', 'Mo.', 'Di.', 'Mi.', 'Do.', 'Fr.', 'Sa.']
				},				
			});			
			
			inject_pikaday_legend('//div[@id="pikaday-legende"]', 'price');
			colorize_pikaday_spans();			
			//set_pikaday_spans_legend();			
		});
		
		function get_start_end_span(nextDay, startDay, endDay) {				
			let start = new Date(nextDay.getTime());
			let end = new Date(nextDay.getTime());						
			start.setDate(start.getDate() - startDay);
			end.setDate(end.getDate() - endDay);						
			let start_span = start.getUTCDate() + '.' + (start.getUTCMonth() + 1);
			let end_span = end.getUTCDate() + '.' + (end.getUTCMonth() + 1);

			return [start_span, end_span];
		}
			
		function set_pikaday_spans_legend() {
			let start_span_one, end_span_one, start_span_two, end_span_two, start_span_three, end_span_three, start_span_four, end_span_four, start_span_five, end_span_five;	
			
			let highestKey = Object.keys(fastsnipeObj).reduce(function(a, b){ return fastsnipeObj[a] > fastsnipeObj[b] ? a : b });
			let highestDay = fastsnipeObj[highestKey]['day_end'] - 1;
			for (i = 0; i <= highestDay; i++) {			
				let nextDay = new Date();
				nextDay.setDate(nextDay.getDate() + i);
				let month = nextDay.getUTCMonth() + 1; //months from 1-12
				let day = nextDay.getUTCDate();
				let year = nextDay.getUTCFullYear();
				
					
				if (i >= fastsnipeObj[1]['day_end'] && i <= fastsnipeObj[2]['day_end']-1) { // category 1
					if (!start_span_one) {
						[start_span_one, end_span_one] = get_start_end_span(nextDay, 7, 1);
						jQuery('#start_one')[0].textContent = start_span_one;
						jQuery('#end_one')[0].textContent = end_span_one;
						jQuery('#category-1')[0].textContent = fastsnipeObj[1]['price'];
						jQuery('.color-category-one').css('background-color', fastsnipeObj[1]['color']);
					}
				}
				else if (i >= fastsnipeObj[2]['day_end'] && i <= fastsnipeObj[3]['day_end']-1) { // category 2
					if (!start_span_two) {
						[start_span_two, end_span_two] = get_start_end_span(nextDay, 7, 1);
						jQuery('#start_two')[0].textContent = start_span_two;
						jQuery('#end_two')[0].textContent = end_span_two;
						jQuery('#category-2')[0].textContent = fastsnipeObj[2]['price'];
						jQuery('.color-category-two').css('background-color', fastsnipeObj[2]['color']);
					}
				}
				else if (i >= fastsnipeObj[3]['day_end'] && i <= fastsnipeObj[4]['day_end']-1) { // category 3
					if (!start_span_three) {						
						[start_span_three, end_span_three] = get_start_end_span(nextDay, 7, 1);
						jQuery('#start_three')[0].textContent = start_span_three;
						jQuery('#end_three')[0].textContent = end_span_three;
						jQuery('#category-3')[0].textContent = fastsnipeObj[3]['price'];
						jQuery('.color-category-three').css('background-color', fastsnipeObj[3]['color']);
					}
				}
				else if (i >= fastsnipeObj[4]['day_end'] && i <= fastsnipeObj[5]['day_end']-1) { // category 4
					if (!start_span_four) {
						[start_span_four, end_span_four] = get_start_end_span(nextDay, 14, 1);
						jQuery('#start_four')[0].textContent = start_span_four;
						jQuery('#end_four')[0].textContent = end_span_four;
						jQuery('#category-4')[0].textContent = fastsnipeObj[4]['price'];
						jQuery('.color-category-four').css('background-color', fastsnipeObj[4]['color']);
					}
					
					if (i == fastsnipeObj[5]['day_end']-1) {
						if (!start_span_five) {
							[start_span_five, end_span_five] = get_start_end_span(nextDay, 21, 0);
							jQuery('#start_five')[0].textContent = start_span_five;
							jQuery('#end_five')[0].textContent = end_span_five;
							jQuery('#category-5')[0].textContent = fastsnipeObj[5]['price'];
							jQuery('.color-category-five').css('background-color', fastsnipeObj[5]['color']);
						}
						
					}
				}
				
			}			
		}
			
		// END JAVASCRIPT
		</script>
		<?php
	}
}