/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

jQuery(function($) {
	$('.rbac-article-tree').delegate('.chk', 'click', function(e) {
		if ($(this).is('.inherit')) return false;

		if (e.target.type !== 'checkbox') {
			var checkbox = $('input', this);
			checkbox.prop('checked', !checkbox.prop('checked'));
			checkbox.trigger('change');
		}
	});

	$('.rbac-article-tree').delegate('.inheriter', 'click', function(e) {
		var
			row            = $(this).closest('.sly-select-checkbox-list'),
			boxes          = row.find('input:checkbox'),
			numBoxes       = boxes.length,
			childContainer = row.next('ul'),
			i;

		for (i = 0; i < numBoxes; ++i) {
			var box = $(boxes[i]), clsName = box.attr('class');
			childContainer.find('input.' + clsName).prop('checked', box.prop('checked'));
		}

		return false;
	});

	$('.rbac-article-tree').delegate('.inheriter', 'click', function(e) {
		if (e.target.type !== 'checkbox') {
			var checkbox = $('input', this);
			checkbox.prop('checked', !checkbox.prop('checked'));
			checkbox.trigger('change');
		}
	});

	$('.rbac-article-tree').delegate('.inheriter', 'mouseenter', function(e) {
		$(this).closest('.sly-select-checkbox-list').next('ul').addClass('highlight');
	});

	$('.rbac-article-tree').delegate('.inheriter', 'mouseleave', function(e) {
		$(this).closest('.sly-select-checkbox-list').next('ul').removeClass('highlight');
	});
});
