// New and imporved repeater class - based on Prototype
var Repeater = Class.create();

Repeater.prototype = {
	repeatClass: 'repeat',
	removeClass: 'removeable',
	
	initialize: function() {
		this.buildRepeaters();
		this.buildRemovers();
	},
	
	buildRepeaters: function() {
		// Find all the elements with their class set to the value of repeatClass
		var repeaters = document.getElementsByClassName(this.repeatClass);
		// Adjust the DOM of the repeaters
		for(i = 0; i < repeaters.length; i++) {
			var element = repeaters[i];
			var addLink = document.createElement('a');
			addLink.setAttribute('title', 'Repeats the preceding field or field group.');
			addLink.setAttribute('class', 'duplicateLink');
			addLink.setAttribute('href', 'javascript:void(null)');
			addLink.innerHTML = "<span>Add a row</span>";
			// Bind the 'onclick' event to the new element
			addLink.onclick = this.repeat.bindAsEventListener(this);
			element.appendChild(addLink);
		}
	},
	
	buildRemovers: function() {
		// Find all the elements with their class set to the value of removeClass
		var removers = $$('.'+this.removeClass);
		// Adjust the DOM of the removers
		for(i = 0; i < removers.length; i++) {
			var element = removers[i];
			if(element.select('.removeLink') == 0) {
				var removeLink = document.createElement('a');
				removeLink.setAttribute('title', 'Removes the preceding field or field group.');
				removeLink.setAttribute('class', 'removeLink');
				removeLink.setAttribute('href', 'javascript:void(null)');
				removeLink.innerHTML = "<span>Remove</span>";
				// Bind the 'onclick' event to the new element
				removeLink.onclick = this.remove.bindAsEventListener(this);
				element.appendChild(removeLink);
			}
		}
	},
	
	repeat: function(sender) {
		// Need to traverse up the tree twice
		var repeatObj = Event.element(sender).parentNode.parentNode;
		
		// Clone the repeatObject, then remove the add link
		var newObj = repeatObj.cloneNode(true);
		
		//var repeaterTriggers = document.getElementsByClassName('duplicateLink', newObj);
		var repeaterTriggers = newObj.select('a.duplicateLink');
		for(i = 0; i < repeaterTriggers.length; i++) {
			newObj.removeChild(repeaterTriggers[i]);
		}
		
		newObj.className = this.removeClass;
		
		// Get the next id number in the sequence
		//var nextIndex = document.getElementsByClassName('remove', repeatObj.parentNode).length + 1;
		var nextIndex = repeatObj.parentNode.select('.'+this.removeClass).length + 1;
		// Append the number in nextIndex to the name and id of all children elements
		var children = newObj.childNodes;
		for(i = 0; i < children.length; i++) {
			var element = children[i];
			
			if(element.id != null && element.id != '') {
				element.id += "-" + nextIndex;
			}
			
			if(element.name != null && element.name != '') {
				// This is where things get a little different to the original wforms.
				// If the nam ends in [], it leaves it, because php will convert that to an array...
				if(element.name.length < 2 || element.name.substring(element.name.length - 2) != "[]") {
					element.name += "-" + nextIndex;
				}
			}
			
			// Clear the value of any inputs
			element.value = "";
		}
		
		//make sure we add it just after the existing node
		if (repeatObj.nextSibling){
			repeatObj.parentNode.insertBefore(newObj,repeatObj.nextSibling)
		}
		else {
			repeatObj.parentNode.appendChild(newObj);
		}
		this.buildRemovers();
	},
	
	remove: function(sender) {
		// Need to traverse up the tree thrice then remove the senders grandparent... Trust me.
		Event.element(sender).parentNode.parentNode.parentNode.removeChild(Event.element(sender).parentNode.parentNode);
	}
}

function onLoadRepeater() {
	var repeater = new Repeater();
}

// Bind myself to onload
Event.observe(window, 'load', onLoadRepeater, false);
	