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

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

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

			row, inputs, j, jlen, input, name, origName, newName, newID;

		for (; i < len; ++i) {
			row    = $(rows[i]);
			inputs = $('input,textarea,select', row);
			jlen   = inputs.length;

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

			for (j = 0; j < jlen; ++j) {
				input    = $(inputs[j]);
				name     = input.attr('name');
				origName = input.data('origname');

				if (typeof origName !== 'undefined') {
					newName  = contName+'['+i+']['+origName+']';
					newID    = contName+'_'+i+'_'+origName;

					input.attr('name', newName).attr('id', newID);
				}
			}
		}

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

	var setAltText = function(container) {
		$('input[alt], textarea[alt]', container).each(function() {
			var self = $(this);

			if (!self.val().length) {
				self.val(self.attr('alt')).css('color', '#ccc');
			}

			self.focus(function() {
				if (self.val() == self.attr('alt')) self.val('').css('color', 'inherit');
			});

			self.blur(function() {
				if (!self.val().length) self.val(self.attr('alt')).css('color', '#ccc');
			});
		});
	}

	// create add/delete links

	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]);
			row.append('<a href="#" class="form-multiple-top form-multiple-button"></a>');
			row.append('<a href="#" class="form-multiple-up form-multiple-button"></a>');
			row.append('<a href="#" class="form-multiple-down form-multiple-button"></a>');
			row.append('<a href="#" class="form-multiple-bottom form-multiple-button"></a>');
			row.append('<a href="#" class="form-multiple-delete form-multiple-button"></a>');
			row.append('<a href="#" class="form-multiple-append form-multiple-button"></a>');
		}

		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), newrow = row.clone();

		$('input,textarea,select', newrow).val('');

		row.after(newrow);
		renumberRows(cont);
		setAltText(cont);

		$(newrow).trigger('rowAdded');

		return false;
	});

	// show alt text

	setAltText(cntSelector);

	// 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;
	});
});