jQuery.fn.multiple = function(passedArgsObj) {
	// Default arguments
	var args = {
		addText: 'Toevoegen',
		removeText: 'Verwijderen'
	}
	
	// Overwrite defaults
	for (var i in passedArgsObj) {
		args[i] = passedArgsObj[i];
	}
	
	return this.each(function() {
		var container = jQuery(this);
		var children = container.children();
		
		if (children.length > 1) {
			var firstChild = jQuery(children[0]);
			var current = 0;
			
			// Hide fields
			children.hide();
			
			// Show first field
			firstChild.show();
			
			// Add buttons
			container.append('<div class="buttons"><input type="button" value="' + args.removeText + '" class="remove"> <input type="button" value="' + args.addText + '" class="add"></div>');
			var buttonsDiv = container.children('div.buttons');
			var addButton = buttonsDiv.children('input.add');
			var removeButton = buttonsDiv.children('input.remove');
			addButton.click(function(){
				// Add child
				addChild = jQuery(children[current+1]);
				addChild.show();
				
				current++;
				
				if (current == children.length-1) {
					// Hide add button after last one was added
					addButton.hide();
				}
				// Show remove button after first is added
				removeButton.show();
			});
			// Hide remove button
			removeButton.hide();
			removeButton.click(function() {
				// Remove last visible child
				removeChild = jQuery(children[current]);
				removeChild.hide();
				
				// Empty field values in removed child
				removeChild.find(':input').each(function() {
					var tag = this.tagName.toLowerCase();
					var type = this.type.toLowerCase();
					if (tag == 'textarea' || tag == 'input' && (type == 'text' || type == 'password' || type == 'file')) {
						this.value = this.defaultValue;
					}
					else if (tag == 'input' && (type == 'checkbox' || type == 'radio')) {
						this.checked = this.defaultChecked;
					}
					else if (tag == 'select') {
						jQuery(this.options).each(function() {
							this.selected = this.defaultSelected;
						});
					}
				});
				
				current--;
				
				if (current == 0) {
					// Hide remove button when only one child is visible
					removeButton.hide();
				}
				if (current < children.length-1) {
					// Display add button when not all children are visible
					addButton.show();
				}
			});
		}
	});
}

