<?php
	$cookieName = 'fixed-note-' . $this->id;
	$cookieValue = $this->tstamp;
?>
<!-- indexer::stop -->
<div class="fixed-note <?php echo $this->class ?>"<?php echo $this->cssID ?><?php if ($this->icon): ?> data-icon="&#x<?php echo $this->icon ?>;"<?php endif ?>>

	<div class="fixed-note-column">
		<?php echo $this->text ?>
	</div>
	<?php if ($this->linkLabel): ?>
		<div class="fixed-note-column">
			<a href="<?php echo $this->linkUrl ?>" class="fixed-note-link"<?php if ($this->newWindow): ?> target="_blank"<?php endif ?>>
				<?php echo $this->linkLabel ?>
			</a>
		</div>
	<?php endif ?>
	<div class="fixed-note-column">
		<button class="fixed-note-button"><?php echo $this->buttonLabel ?></button>
	</div>

</div>

<?php if ($this->allowReopen): ?>
	<a class="fixed-note-closed" href="{{env::request}}#"><span>Info</span></a>
<?php endif ?>
<!-- indexer::continue -->

<script>
(function() {

	var getCookie = function() {
		var value = document.cookie.match('(?:^|;)\\s*<?php echo $cookieName ?>=([^;]*)');
		return (value) ? decodeURIComponent(value[1]) : null;
	};

	var setCookie = function() {
		document.cookie = '<?php echo $cookieName ?>=<?php echo $cookieValue ?>; path=/; expires=' + (new Date(Date.now() + <?php echo number_format(($this->cookieDays ?: 30) * 24 * 60 * 60 * 1000, 0, '', '') ?>)).toGMTString();
	};

	var removeCookie = function() {
		document.cookie = '<?php echo $cookieName ?>=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT';
	};

	var resize = function() {
		var style = window.getComputedStyle(note);
		if (style.position === 'fixed' && style.display !== 'none') {
			document.documentElement.style.paddingBottom = note.offsetHeight + 'px';
		}
		else {
			document.documentElement.style.paddingBottom = '';
		}
	};

	var close = function() {
		note.style.display = 'none';
		<?php if ($this->allowReopen): ?>
			noteClosed.style.display = '';
		<?php endif ?>
		resize();
	};

	var open = function() {
		note.style.display = '';
		<?php if ($this->allowReopen): ?>
			noteClosed.style.display = 'none';
		<?php endif ?>
		resize();
	};

	var note = document.querySelectorAll('.fixed-note');
	note = note[note.length - 1];
	var button = note.querySelector('.fixed-note-button');

	<?php if ($this->allowReopen): ?>
		var noteClosed = document.querySelectorAll('.fixed-note-closed');
		noteClosed = noteClosed[noteClosed.length - 1];

		noteClosed.addEventListener('click', function(event) {
			open();
			removeCookie();
			event.preventDefault();
		});
	<?php endif ?>

	button.addEventListener('click', function() {
		close();
		setCookie();
	});

	if (getCookie() === '<?php echo $cookieValue ?>') {
		close();
	}
	else {
		open();
	}

	window.addEventListener('resize', resize);

	<?php if ($this->allowReopen): ?>
		document.body.insertBefore(noteClosed, document.body.firstChild);
	<?php endif ?>
	document.body.insertBefore(note, document.body.firstChild);

})();
</script>
