(function($){  
$.fn.cloner = function( options ) {
	
	var defaults = {  
		cloneFieldsetClass	:	'cloner',
		existingItemClass	:	'collateral_block_exists',
		removeClass			:	'remove',
		existsRemoveClass	:	'exists_remove',
		addLink				:	'add',
		deleteStateText		:	'Click "Save Changes" to delete or',
		deleteStateLink		:	'Keep Me'
	}; 
	 
	var options = $.extend( defaults, options );

	var obj					=	this;
	this.formObj			=	$('.'+options.cloneFieldsetClass).parent()			//	the form object
	this.cloneSet			=	$('.'+options.cloneFieldsetClass);					//	the fieldset to be cloned
	this.removeLink			=	$('.'+options.removeClass);							//	remove links
	this.existsRemoveBox	=	$('dl.'+options.existsRemoveClass);					//	existing items' remove checkboxes
	this.existsRemoveLink	=	$('p.'+options.existsRemoveClass+" a");				//	existing items' remove link
	this.addLink			=	this.formObj.find('.' + options.addLink+ ' a');		//	add new item link
	this.banana				=	this.formObj.find('.banana');
	this.idCounter			=	1;													//	counter for ids and names
			
	return this.each(function() {  

		//	when you click on 'add new'
		obj.addLink.click( function()
		{
			obj.cloneSet.clone(true).insertBefore(obj.cloneSet).removeClass(options.cloneFieldsetClass).slideDown( function()
			{
				$(this).find('input, textarea').each( function()
				{
					$(this).attr( 'id', $(this).attr('id') + obj.idCounter );
				});
			
				$(this).find('label').each( function()
				{
					$(this).attr( 'for', $(this).attr('for') + obj.idCounter );
				});
			
				obj.idCounter ++;
			});
			
			obj.bananaCheck();
			
			return false;
		});


		//	when you click on 'remove' for a cloned item
		obj.removeLink.click( function()
		{
			$(this).parent().fadeOut( function()
			{
				$(this).remove();
				obj.bananaCheck();
			});
			
			return false;
		});


		//	when you click on 'remove' for an existing item
		obj.existsRemoveLink.click(function()
		{
			var checkbox	=	$(this).parent().parent().find('.'+options.existsRemoveClass+' input');
		
			if( checkbox.attr('checked'))
			{
				$(this).parent().find('.keep').remove();
				$(this).html('Remove');
				checkbox.removeAttr('checked');
			}
			else{
				$(this).html(options.deleteStateLink);
				$(this).parent().prepend('<span class="keep">'+options.deleteStateText+'</span> ');
			
				checkbox.attr('checked', 'checked');
			}
			
			obj.showStatus( checkbox );
			
			return false;
		});


		//	checks to see if the banana should be shown or hidden
		obj.bananaCheck	=	function()
		{
			if( obj.formObj.find('fieldset:visible').length < 1 )
			{
				obj.banana.slideDown();
			}
			else
			{
				obj.banana.slideUp();
			}
		}

		//	changes the class of existing items
		obj.showStatus = function( checkbox )
		{
			if( checkbox.parent().find('input:checked').length > 0 )
			{
				checkbox.parent().parent().parent().addClass('delete_me');
			}
			else
			{
				checkbox.parent().parent().parent().removeClass('delete_me');
			}
		}

	});  
};  
})(jQuery);
