
<?php
// jquery coookie
$GLOBALS['TL_JAVASCRIPT'][] = 'files/cto_layout/scripts/cookie/jquery.cookie.js|static';

// template variables
$strSession = $this->session_name;
$intPrivacy = $this->privacy;
?>

<div id="privacy_optin_<?= $this->id; ?>" class="mod_privacy_optin">
	<div class="privacy_overlay"></div>
	<div class="privacy_popup">
		<div class="privacy_inside">
			<div class="privacy_content">
				<div class="cookie_settings">
					<!-- info text after ajax -->
					<div id="ajax_info_<?= $this->id; ?>" class="ajax_info">{{iflng::en}}Settings saved{{iflng}}{{iflng::de}}Einstellungen gespeichert{{iflng}}</div>
					<?php if ($this->headline): ?>
						<div class="headline">
						 <<?= $this->hl ?>><?= $this->headline ?></<?= $this->hl ?>>
						</div>
					<?php endif; ?>
					<p class="cookie_info">{{iflng::en}}Select an option to continue{{iflng}}{{iflng::de}}Bitte treffen Sie eine Auswahl um fortzufahren{{iflng}}</p>
					<form id="<?= $strSession.'_'.$this->id; ?>" name="<?= $strSession; ?>" method="post">
						<input type="hidden" name="FORM_SUBMIT" value="<?= $strSession; ?>">
						<input type="hidden" name="REQUEST_TOKEN" value="<?= REQUEST_TOKEN; ?>">
						<div class="radio_container cookies tab">
							<div class="palette">
								<?php if($this->label_1): ?>
								<div class="item<?php if($intPrivacy == 1): ?> active<?php endif; ?>">
									<div class="widget widget-radio">
										<input type="radio" class="radio" name="privacy" id="cookiebar_privacy_1" value="1">
										<div class="widget-label">
											<label for="cookiebar_privacy_1"><?= $this->label_1; ?>
											<span class="cookie_option_info"><?= $this->description_1; ?></span>
											</label>
										</div>
									</div>
								</div>
								<?php endif; ?>
								<?php if($this->label_2): ?>
								<div class="item<?php if($intPrivacy == 2): ?> active<?php endif; ?>">
									<div class="widget widget-radio">
										<input type="radio" class="radio" name="privacy" id="cookiebar_privacy_2" value="2">
										<div class="widget-label">
											<label for="cookiebar_privacy_2"><?= $this->label_2; ?>
											<span class="cookie_option_info"><?= $this->description_2; ?></span>
											</label>
										</div>
									</div>
								</div>
								<?php endif; ?>
								<?php if($this->label_3): ?>
								<div class="item<?php if($intPrivacy == 3): ?> active<?php endif; ?>">
									<div class="widget widget-radio">
										<input type="radio" class="radio" name="privacy" id="cookiebar_privacy_3" value="3">
										<div class="widget-label">
											<label for="cookiebar_privacy_3"><?= $this->label_3; ?>
											<span class="cookie_option_info"><?= $this->description_3; ?></span>
											</label>
										</div>
									</div>
								</div>
								<?php endif; ?>
							</div>
						</div>
						<div class="ce_hyperlink">
							<input type="submit" name="save_settings" class="submit readonly" disabled value="{{iflng::en}}Save settings to continue{{iflng}}{{iflng::de}}Einstellungen speichern & fortfahren{{iflng}}">
						</div>
					</form>
					<div class="cookie_privacy_links">
						<?= $this->link_1; ?>
						<?= $this->link_2; ?>
						<a href="#" class="cookie_help_link">{{iflng::en}}Further information{{iflng}}{{iflng::de}}Weitere Informationen</a>{{iflng}}
					</div>
				</div>
				<div class="cookie_help">
					<div><?= $this->html; ?></div>
					<div class="cookie_help_link">{{iflng::en}}Close{{iflng}}{{iflng::de}}Schließen{{iflng}}</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script>
/* <![CDATA[ */
jQuery(document).ready(function()
{
	var privacy = localStorage.getItem('<?= $strSession; ?>');
	var privacy_cookie = document.cookie;

	if(privacy == undefined || privacy == '' || privacy == null || privacy_cookie.indexOf('<?= $strSession; ?>') < 0)
	{
		jQuery('#privacy_optin_<?= $this->id; ?>').addClass('open');
		// set privacy level to -1
		privacy = -1;
	}

	// remove token
	if(window.location.search.indexOf('clear_privacy_settings') >= 0)
	{
		// clear local storage
		localStorage.removeItem('<?= $strSession; ?>');
		// empty cookie
		//document.cookie = '<?= $strSession; ?>=;';
		jQuery.removeCookie('<?= $strSession; ?>',{path:'/'});
		// fire event
		jQuery(document).trigger('Eclipse.clear_privacy_settings',{});
		// log
		console.log('Privacy settings cleared');
		// reload without parameter
		location.href = window.location.origin+window.location.pathname;
	}

	// set a body class
	jQuery('body').addClass('privacy_setting_'+privacy);

	// hide the info
	jQuery('#ajax_info_<?= $this->id; ?>').hide();

	var form = jQuery('#<?= $strSession.'_'.$this->id; ?>');

	// remove readonly from submit when user changes its selection
	form.find('.widget').change(function(e)
	{
		form.find('input[name="save_settings"]').removeClass('readonly');
		form.find('input[name="save_settings"]').prop('disabled', false);
	});

	form.find('input[name="save_settings"]').click(function(e)
	{
		e.preventDefault();

		// still readonly
		if(jQuery(this).hasClass('readonly'))
		{
			return false;
		}

		// show ajax info
		jQuery('#ajax_info_<?= $this->id; ?>').fadeIn(50);

		var data = form.serializeArray();
		var privacy = parseInt(data[2]['value']);

		// set local storage
		localStorage.setItem('<?= $strSession; ?>',privacy);
		// set cookie for php
		//document.cookie = '<?= $strSession; ?>='+privacy;
		jQuery.cookie('<?= $strSession; ?>',privacy,{expires:<?= $this->privacy_cookie_expires; ?>,path:'/'});
		// set a body class
		jQuery('body').addClass('privacy_setting_'+privacy);
		// remove negative body class
		jQuery('body').removeClass('privacy_setting_-1');
		setTimeout(function()
		{
			// fire JS event
			jQuery(document).trigger('Eclipse.user_privacy',{'level':privacy});

			// fire form as usal to catch it via php
			//form.submit();
		}, 500);

	});

	// help info
	jQuery('#privacy_optin_<?= $this->id; ?> .cookie_help_link').click(function()
	{
		jQuery('#privacy_optin_<?= $this->id; ?> .privacy_popup').toggleClass('view_help');
	});

	// help - scrollToTop
	jQuery('#privacy_optin_<?= $this->id; ?> .cookie_help_link').click(function()
	{
		jQuery("html, body").animate({ scrollTop: 0 });
		return false;
	});

});

// listen to Eclipse.user_privacy Event
jQuery(document).on('Eclipse.user_privacy',function(event,params)
{
	if(params.level >= 0)
	{
		jQuery('#privacy_optin_<?= $this->id; ?>').removeClass('open');
		// send ajax for log file
		jQuery.ajax(
		{
			url:location.href,
			data:{'user_privacy_settings':params.level,'tstamp':Math.round(new Date().getTime()/1000)}
		});
	}
});

</script>
