jQuery(function($) {
	'use strict';

	var
		cntSelector    = '.form-repeat-container',
		rowSelector    = '.form-repeat-row',
		delSelector    = '.form-repeat-delete',
		addSelector    = '.form-repeat-append',
		topSelector    = '.form-repeat-top',
		upSelector     = '.form-repeat-up',
		downSelector   = '.form-repeat-down',
		bottomSelector = '.form-repeat-bottom',
		newgroup       = 1000;

	var renumberRows = function(container) {
		var
			rows     = $(rowSelector, container),
			i        = 0,
			len      = rows.length,

			row, j;

		for (; i < len; ++i) {
			row    = $(rows[i]);

			row.removeClass('even').removeClass('odd');
			row.addClass(i%2==0 ? 'even' : 'odd');
		}

		container.removeClass('empty').removeClass('nonempty').removeClass('single');
		container.addClass(len === 0 ? 'empty' : (len === 1 ? 'single' : 'nonempty'));
	};

	// create add/delete links
	
	var addRowButtons = function(row) {
		row.append('<a href="#" class="form-repeat-top form-repeat-button"></a>');
		row.append('<a href="#" class="form-repeat-up form-repeat-button"></a>');
		row.append('<a href="#" class="form-repeat-down form-repeat-button"></a>');
		row.append('<a href="#" class="form-repeat-bottom form-repeat-button"></a>');
		row.append('<a href="#" class="form-repeat-delete form-repeat-button"></a>');
		row.append('<a href="#" class="form-repeat-append form-repeat-button"></a>');
	}

	var containers = $(cntSelector);

	for (var i = 0, len = containers.length; i < len; ++i) {
		var cont = $(containers[i]);
		var rows = $(rowSelector, cont);

		cont.addClass(rows.length === 0 ? 'empty' : 'nonempty');

		for (var j = 0, jlen = rows.length; j < jlen; ++j) {
			var row = $(rows[j]);
			addRowButtons(row);
		}

		renumberRows(cont);
	}

	// remove an existing item

	$(cntSelector).delegate(delSelector, 'click', function() {
		var self = $(this), row = self.closest(rowSelector), cont = row.closest(cntSelector);

			row.remove();
			renumberRows(cont);

		return false;
	});

	// add a new row

	$(cntSelector).delegate(addSelector, 'click', function() {
		var self = $(this), row = self.closest(rowSelector), cont = row.closest(cntSelector);
		$.ajax({
			'url': 'index.php?page=devutilsrepeat',
			'data': {'type':  cont.data('metatype'), 'name': cont.data('metaname'), 'group': newgroup++},
			'dataType': 'json',
			'success': function(data) {
				if (data.markup == undefined) return false;
				var newrow = $(data.markup);
				row.after(newrow);
				addRowButtons(newrow);
				renumberRows(cont);
				$(newrow).trigger('rowAdded');
			}
		});

		return false;
	});

	// move row to the bottom

	$(cntSelector).delegate(bottomSelector, 'click', function() {
		var row = $(this).closest(rowSelector), cont = row.closest(cntSelector);

		cont.append(row);
		renumberRows(cont);

		return false;
	});

	// move row to the top

	$(cntSelector).delegate(topSelector, 'click', function() {
		var row = $(this).closest(rowSelector), cont = row.closest(cntSelector);

		cont.prepend(row);
		renumberRows(cont);

		return false;
	});

	// move row down

	$(cntSelector).delegate(downSelector, 'click', function() {
		var row = $(this).closest(rowSelector),	cont = row.closest(cntSelector);

		row.next().after(row);
		renumberRows(cont);

		return false;
	});

	// move row up

	$(cntSelector).delegate(upSelector, 'click', function() {
		var row = $(this).closest(rowSelector), cont = row.closest(cntSelector);

		row.prev().before(row);
		renumberRows(cont);

		return false;
	});
});