"use strict";

$(function() {

    $("[data-copy]").click(function() {
		var el = $(this.dataset.copy);
		if( !el.length ) return; 
		el.get(0).select();
		var copied = document.execCommand('copy');
		var context = copied ? "Copied text: " : "Could not copy text: ";
		alert( context + el.val() );
	});
	
	$("[data-reflect]").on('input', function() {
		var reflect = $( this.dataset.reflect ).get(0);
		if( !reflect ) return;
		reflect.value = this.value;
	});
	
	function validset(value, alternative) {
		if( value == undefined ) value = alternative;
		else {
			value = value.trim();
			if( value == '' || !value ) value = alternative;
		};
		return value;
	}
	
    $('[data-table]').each(function() {
		var tableset = this.dataset;
		var length = parseInt(tableset.pagelength);
		if( isNaN(length) || length < 10 ) length = 25;
		var order_values = [ Number(validset(tableset.orderby, 0)), validset(tableset.direction, 'asc') ];
		var option = {
			pageLength: length,
			order: [order_values]
		};
		if( this.dataset.table == 'advance' ) {
			option.dom = 'Bfrtip';
			option.buttons = ['excel', 'pdf', 'print'];
		};
		$(this).DataTable(option);
    });
	
});

