$(function ($) {
	var c_lang = 'zh-TW';
	var gdpr = {
		c_name: (c_lang == '' || c_lang == undefined) ? 'fb_GDPR':'fb_GDPR_zh-TW',
		c_val: '',
		version: '1',
		strHtml: $.trim($('.Pos_f.lCookie').html()),
		s_content: '',
		e_content: '',
		hashCode: function () {
			var h = 0;
			var s = this.s_content + this.e_content;
			if (s.length > 0) {
				for (let i = 0; i < s.length; i++) {
					h += (s.charCodeAt(i) * 6);
				}
			}
		   return String(h);
		},
	};
	if (gdpr.strHtml != '' && gdpr.strHtml != undefined) {
		var len = gdpr.strHtml.length;
		var f = Math.floor(len / 2);
		gdpr.s_content = gdpr.strHtml.substr(0, f);
		gdpr.e_content = gdpr.strHtml.substr(len - 100, 100);
		gdpr.c_val = 'code_' + gdpr.hashCode() + '_v' + gdpr.version;
		let isShow = true;
		var getValueFromCookie = getCookie(gdpr.c_name);
		var isSame = getValueFromCookie === gdpr.c_val;
		//console.log('flag='+isSame);
		if (isSame) {
			isShow = false;
		}
		displayBar(isShow);
	}
	$('.mCookieBtn').on('click', function () {
		hideCookieWrap();
		setCookie(gdpr.c_name, gdpr.c_val, 180);	// cookie with 180 days
	});
	function displayBar(isShow) {
		let $fixed = $('.fixed-block');
		if ($fixed.length == 0) {
			$fixed = $('<div></div>');
			$fixed.addClass('fixed-block')
			$fixed.css('width', '100%');
			$fixed.css('position', 'fixed');
			$fixed.css('bottom', '0');
			$fixed.css('left', '0');
			$fixed.css('z-index', '60');
			$('body').append($fixed);
		}
		$fixed.append($('.Pos_f.lCookie'));
		if (isShow) {
		} else {
			hideCookieWrap();
		}
	}
	function hideCookieWrap()  {
		$.each($('.fixed-block').children(), function () {
			if ($(this).hasClass('lCookie')) {
				$(this).hide();
			}
		})
	}
	function setCookie(c_name, value, exdays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
		document.cookie = c_name + "=" + c_value + "; path=/;" + " SameSite=Strict; secure;";
	}
	function getCookie(c_name) {
		var i, x, y, EachCookies = document.cookie.split(";");
		for (i = 0; i < EachCookies.length; i++) {
			x = EachCookies[i].substr(0, EachCookies[i].indexOf("="));
			y = EachCookies[i].substr(EachCookies[i].indexOf("=") + 1);
			x = x.replace(/^\s+|\s+$/g, "");
			if (x == c_name) {
				return unescape(y);
			}
		}
	}
});