(function() {
	var wrap  = document.getElementById('kramar-eo-rows');
	var addBtn = document.getElementById('kramar-eo-add');
	var tpl   = document.getElementById('kramar-eo-row-template');

	if ( ! wrap || ! addBtn || ! tpl ) {
		return;
	}

	function bindRemove( row ) {
		var btn = row.querySelector('.kramar-eo-remove');
		if ( btn ) {
			btn.addEventListener('click', function() {
				row.parentNode.removeChild(row);
			});
		}
	}

	// Bind existing rows.
	var existing = wrap.querySelectorAll('.kramar-eo-row');
	for ( var i = 0; i < existing.length; i++ ) {
		bindRemove( existing[i] );
	}

	addBtn.addEventListener('click', function() {
		var index = Date.now();
		var html  = tpl.innerHTML.replace(/__INDEX__/g, index);
		var tmp   = document.createElement('tbody');
		tmp.innerHTML = html;
		var newRow = tmp.querySelector('.kramar-eo-row');
		if ( newRow ) {
			wrap.appendChild(newRow);
			bindRemove(newRow);
		}
	});
})();
