/******************
    User custom JS
    ---------------

   Put JS-functions for your template here.
   If possible use a closure, or add them to the general Template Object "Template"
*/
/******************************************
 * Begin DISC manipulation script
 ******************************************/
function shuffleArray(array) {
		for (var i = array.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
		return array;
	}
	
	$(document).on('ready pjax:complete',function() {
		// Identify this question
		var thisQuestion = $('.array-flexible-column:eq(0)'); 
	 
		var elements = $('tr.answers-list', thisQuestion).detach().toArray();
		shuffleArray(elements);
		$('tr.answers-list', thisQuestion).remove();
		$.each(elements, function(i, el) {
			$('table.subquestion-list tbody:eq(0)', thisQuestion).append(el);
		});
	});

	$(document).on('ready pjax:complete',function() {    
	 
		// Identify the questions -- FOR DISC
		var thisQuestion = $('.array-flexible-column:eq(0)');
		
		function handleRadios() {
			$('input:radio', thisQuestion).prop('disabled', false);
			$('.disabled-item', thisQuestion).removeClass('disabled-item');
			$('input:radio:checked', thisQuestion).each(function(i) {
				var thisRow = $(this).closest('tr.answers-list');
				var otherRadios = $('input:radio', thisRow).not(this);
				$(otherRadios).prop('disabled', true);
				$(otherRadios).closest('td').addClass('disabled-item');
			});
		}

		// Listener on the radios
		$('input:radio', thisQuestion).on('click', function(e) {
			handleRadios();
		});

		// Initial settings
		handleRadios();
	});
	
 

