/**
 *
 * Cookie
 */

function setCookie(cname, cvalue, exmins) {
	let exdate = new Date();
	exdate.setTime(exdate.getTime() + (exmins * 60 * 1000));
	let expires = "expires=" + exdate.toUTCString();
	document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
	let cvalue = '';
	let decodedCookie = decodeURIComponent(document.cookie);
	let carr = decodedCookie.split(';');
	carr.forEach(function(citem){
		let rname = citem.trim().split('=')[0];
		let rvalue = citem.trim().split('=')[1];
		if (cname == rname) {
			cvalue = rvalue;
		}
	})
	return cvalue;
}

/**
 *
 * Scroll Animation
 */

$.fn.customScrollAnimate = function() {
	$('html').animate({
		'scrollTop' : $(this).offset().top - parseInt($(this).css('margin-bottom')) / 2 - $('.navbar.sticky-top').outerHeight()
	}, 200);
	return this;
}

// a[href=""], a.disabled
$(document).on('click', 'a[href=""], a.disabled', function() {
	return false;
});

// a[href="#img-modal"]
$(document).on('click', 'a[href="#img-modal"]', function() {
	if ($(this).find('img, figure[src]').length) {
		let src = $(this).find('img, figure[src]').attr('src');
		let title = $(this).find('img, figure[src]').attr('title');
		$('.modal').modal('hide');
		$('body').append($('<div class="modal modal-carousel" id="img-modal" tabindex="-1" style="display:block;"></div>').html([ $('<div class="modal-dialog modal-lg"></div>').html([ $('<div class="modal-content"></div>').html([ $('<button type="button" class="close" data-dismiss="modal">&times;</button>'), $('<img class="img-fluid" src="' + src + '" title="' + (title ? title : '') + '"/>') ]) ]) ]));
		$('#img-modal').modal('show');
		return false;
	}
});
$(document).on('hidden.bs.modal', '#img-modal', function() {
	$('#img-modal').remove();
});

// img[alt], img[title]
$(function() {
	$('img[title][alt=""], img[title]:not([alt])').each(function() {
		$(this).attr('alt', $(this).attr('title'));
	});
	$('img[alt][title=""], img[alt]:not([title])').each(function() {
		$(this).attr('title', $(this).attr('alt'));
	});
});

// [data-toggle="popover"], [data-toggle="tooltip"]
$(function() {
	$('[data-toggle="popover"]').popover();
	$('[data-toggle="tooltip"]').tooltip();
});

// [data-toggle="collapse"][data-parent]
$(document).on('click', '[data-toggle="collapse"][data-parent]', function() {
	$($(this).data('parent')).find('.collapse').not($(this).data('target')).collapse('hide');
});

// [data-toggle="tbody-collapse"]
$(document).on('click', '[data-toggle="tbody-collapse"]', function() {
	$(this).find('tr').toggleClass('active').parents('tbody').next('.collapse').toggleClass('show');
});

// [data-toggle="print"]
$(document).on('click', '[data-toggle="print"]', function() {
	$('iframe[id!="st_gdpr_iframe"]:visible').each(function() {
		$(this).after('<div class="iframe-placeholder card card-body" style="width:' + $(this).width() + 'px;height:' + $(this).height() + 'px;">VIDEO</div>').hide();
	});
	window.print();
	$('iframe:hidden+.iframe-placeholder').each(function() {
		$(this).before('iframe').show().next('.iframe-placeholder').remove();
	});
});

// .tab-pane
$(function() {
	$('.tab-content').each(function() {
		if ($(this).find(location.hash).length) {
			$('[href="' + location.hash + '"]').trigger('click');
			$(window).scrollTop(0);
		} else {
			$('[href="' + '#' + $(this).find('.tab-pane').eq(0).attr('id') + '"]').trigger('click');
		}
	});
});

// #topToBottom, .fixed-top.fade
$(function() {
	let offset = 0;
	$('body>nav, body>header').not('.fixed-top').each(function() {
		offset += $(this).outerHeight();
	});
	$('#topToBottom').addClass('fade show');
	$(window).on('scroll', function() {
		if ($(window).scrollTop() > offset) {
			$('#topToBottom').removeClass('show').hide();
			$('.fixed-top.fade').show().addClass('show');
		} else {
			$('#topToBottom').show().addClass('show');
			$('.fixed-top.fade').removeClass('show').hide();
		}
	});
});

// .col-xs-
$(function() {
	$('[class*="col-xs-"]').each(function() {
		$(this).attr('class', $(this).attr('class').replace('col-xs-', 'col-'));
	});
});

// DataTable
function dtState() {
	let dtFilter = $('#dataTable_filter input').length ? encodeURIComponent($('#dataTable_filter input').val()) : null;
	let dtLength = $('#dataTable').DataTable().page.len();
	let dtIdx = $('#dataTable').DataTable().page();
	history.pushState(
		{
			dtFilter: dtFilter,
			dtLength: dtLength,
			dtIdx: dtIdx
		},
		document.title,
		location.href.split('#')[0] + (dtFilter ? '#dtFilter=' + dtFilter : '') + (dtLength ? '#dtLength=' + dtLength : '') + (dtIdx > 0 ? '#dtIdx=' + dtIdx : '')
	);
}
$(document).on('keyup', '#dataTable_filter input', function(){
	dtState();
});
$(document).on('change', '#dataTable_length select', function(){
	dtState();
});
$(document).on('click', '[data-dt-idx]', function(){
	dtState();
});
$(document).on('init.dt', '#dataTable', function(){
	let hash_params = location.hash.substr(1).split('#');
	hash_params.forEach(function(hash_param){
		let hash_param_name = hash_param.split('=')[0];
		let hash_param_value = hash_param.split('=')[1];
		if (hash_param_name == 'dtFilter') {
			$('#dataTable').DataTable().search(decodeURIComponent(hash_param_value)).draw(false);
		} else if (hash_param_name == 'dtLength') {
			$('#dataTable').DataTable().page.len(hash_param_value).draw(false);
		} else if (hash_param_name == 'dtIdx') {
			$('#dataTable').DataTable().page(parseInt(hash_param_value)).draw(false);
		}
	});
});