<?php
add_filter( 'wp_enqueue_scripts', 'my_global_helper_functions' );
function my_global_helper_functions() {
	?>
		<script>
		// START JAVASCRIPT
		var businessHoursObj = {
			'start': {
				'hours': 07,
				'minutes': 0,
				'seconds': 0,
			},
			'end': {
				'hours': 18,
				'minutes': 0,
				'seconds': 0,
			},
		};
			
		var fastsnipeObj = {
			1: {
				'day_end': 7,
				'color':  '#f8dd4d',
				'price': 2250,
				'label': 'Premium', 
			},
			2: {
				'day_end': 14,
				'color': 'hotpink',
				'price': 1800,
				'label': 'Premium',
			},
			3: {
				'day_end': 21,
				'color': '#fd9f02',
				'price': 1100,
				'label': 'Premium',
			},
			4: {
				'day_end': 35,
				'color': '#87cefa',
				'price': 500,
				'label': 'Premium',
			},
			5: {
				'day_end': 57,
				'color': 'darkseagreen',
				'price': 0,
				'label': 'Free',
			},
		};
			
		function Confirm(title, msg, $true, $false, $link, $open) {
			var $content = "<div class='dialog-ovelay'>" +
				"<div class='dialog'><header>" +
				" <h3> " + title + " </h3> " +
				"<i class='fa fa-close'></i>" +
				"</header>" +
				"<div class='dialog-msg'>" +
				" <p> " + msg + " </p> " +
				"</div>" +
				"<footer>" +
				"<div class='controls'>" +
				" <button class='button doAction' id='true-button'>" + $true + "</button> " +
				" <button class='button cancelAction' id='false-button'>" + $false + "</button> " +
				"</div>" +
				"</footer>" +
				"</div>" +
				"</div>";
			jQuery('body').prepend($content);
			jQuery('.doAction').click(function() {
				if ($open === '_self') {
					window.location.href = $link;
				}
				else if ($open === '_blank') {
					window.open($link, '_blank');
				}			
				jQuery(this).parents('.dialog-ovelay').fadeOut(400, function() {
					jQuery(this).remove();
				});
			});
			jQuery('.cancelAction, .fa-close').click(function() {
				jQuery(this).parents('.dialog-ovelay').fadeOut(400, function() {
					jQuery(this).remove();
				});
			});
		}
			
		function inject_pikaday_legend(xpath, fswitch) {
			//let pika_single = getElementByXpath("//div[contains(@class, 'pika-single') and not(contains(@class, 'is-hidden'))]");	
			let pika_single = getElementByXpath(xpath);
			
			if( fswitch == 'price' || fswitch == 'label' ) {
				let append_html = '<div id="table-pikaday-div"><table id="table-pikaday-legend"> <tr> <td><div class="color-div"></div></td> <td><div class="color-div"></div></td> <td><div class="color-div"></div></td> <td><div class="color-div"></div></td> <td><div class="color-div"></div></td> </tr></table></div>';				
				jQuery(pika_single).append(append_html);
				for (var key in fastsnipeObj) {
					if (fastsnipeObj.hasOwnProperty(key)) {
						let selector = 'table-pikaday-legend td:nth-child(' + key + ')';
						let price = fastsnipeObj[key]['price'];
						let color = fastsnipeObj[key]['color'];
						let label = fastsnipeObj[key]['label'];

						if( fswitch == 'price' ) {
							var innerHTML = '<div class="color-div" style="background: ' + color + '"></div><div class="period-price"><img src="http://zinomedia.de/terminsnipe/wp-content/uploads/sites/6/2020/03/630865.svg" alt="fastcoin"></div><span>' + price + '</span>';
						}
						else if( fswitch == 'label' ) {
							var innerHTML = '<div class="color-div" style="background: ' + color + '"></div><span>' + label + '</span>';
						}
						jQuery('#' + selector)[0].innerHTML = innerHTML;
					}
				}
			}
			else if( fswitch == 'start_end' ) {
				//let append_html = '<div id="table-pikaday-div"><table id="table-pikaday-legend-start-end" <tr> <td><div class="color-div"></div></td> <td><div class="color-div"></div></td> </tr></table></div>';
				let append_html = '<div id="table-pikaday-div"><table id="table-pikaday-legend-start-end" style="width: 120px; margin: auto"> <tr> <td><div class="color-div"></div></td> </tr></table></div>';
				jQuery(pika_single).append(append_html);
				//let selector = 'table-pikaday-legend td:nth-child(' + key + ')';
				jQuery('#table-pikaday-legend-start-end td:nth-child(1)')[0].innerHTML = "<div class=\"color-div\" style=\"background: lightgrey\"></div><span>Start-Datum</span>";
				//jQuery('#table-pikaday-legend-start-end td:nth-child(2)')[0].innerHTML = "<div class=\"color-div\" style=\"background: #33aaff\"></div><span>End-Datum</span>";				
			}		
		}	
		
		function colorize_pikaday_spans() {
			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();				
				
				for (var key in fastsnipeObj) {
					if (fastsnipeObj.hasOwnProperty(key)) {						
						let day_end = fastsnipeObj[key]['day_end'] - 1;
						if (i <= day_end) {
							var color = fastsnipeObj[key]['color'];
							break;
						}					
					}
				}				
				pika_change_bg_color(day, month, year, color);
			}
		}

		function pika_change_bg_color(day, month, year, color, force = 0) {
			console.log('day: ' + day + ' | month: ' + month + ' | year: ' + year + ' | color: ' + color);
			month--;
			let xpath = "//div[contains(@class, 'pika-single') and not(contains(@class, 'is-hidden'))]/div/table/tbody/tr/td//button[contains(@class, 'pika-button') and @data-pika-month='" + month + "' and @data-pika-day='" + day + "' and @data-pika-year='" + year + "']";
			let el = getElementByXpath(xpath);
			let className = jQuery(el)[0].parentElement.className;
			if( force ) {
				jQuery(el).css({"background": color, "opacity": "1"});
			}
			else if (className != 'is-disabled' && className != 'is-disabled is-today' && !force) {
				jQuery(el).css('background', color);				
			}
		}

		function getElementByXpath(path) {
			return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
		}
		
		function fastsnipe_highest_end_day() {
			let highestKey = Object.keys(fastsnipeObj).reduce(function(a, b){ return fastsnipeObj[a] > fastsnipeObj[b] ? a : b });
			return fastsnipeObj[highestKey]['day_end'];
		}
			
		function pikaday_maxDate() {
			let add_days_end = fastsnipe_highest_end_day()-1;
			let today = new Date();
			let end_date = new Date();
			end_date.setDate(today.getDate()+add_days_end);
			return end_date;
		}
			
		function wait_for_element(selector, fnName, dateObject) {
			if (jQuery('#' + selector).length) {
				console.log('ready');
				window[fnName](selector, dateObject);
			} else {
				setTimeout(function() {
					wait_for_element(selector, fnName, dateObject);
				}, 100);
			}
		}
			
		function makeItRain() {
			confetti.start(1200, 50, 150);
		}
			
		function removeA(array, item) {
			var index = array.indexOf(item);
			if (index !== -1) array.splice(index, 1);
			
			return array;
		}
			
		function trim_string(string, length) {
			if( string.length > length )
				string = string.substring(0, length-3) + '...';
			
			return string;
		}
		// END JAVASCRIPT
		</script>
	<?php
}