jQuery.noConflict();

jQuery(document).ready(function ($) {
		
		
		// Clone Funktions Anfang
		jQuery('#tx-powermail-pi1_fieldset_248').addClass("clonable").wrap('<div id="clonecont" />');
		jQuery('form.tx_powermail_pi1_form fieldset.labellangclonefield').addClass("clonable").wrap('<div id="clonecont" />');
		
		
		
		jQuery('fieldset.clonable input, fieldset.clonable select, fieldset.clonable textarea').each(function(){
			var s = $(this).attr("id"); 			
			$(this).attr("rel", s);
			var uid = s.replace('uid', '');
		});	
		
   
		$('fieldset.clonable input[type="text"],fieldset.clonable textarea').live("focus", function(event){
			if(this.value=='--'){this.value=''}
		});
		
		$('fieldset.clonable input[type="text"],fieldset.clonable textarea').live("blur", function(event){
			if(this.value==''){this.value='--'}
		});
		
		$('#uid1538,#uid1605,#uid1582,#uid1594').bind("change", pferdclonefunc);
		
		var pferdclonefuncvar = $('#uid1538,#uid1605,#uid1582,#uid1594').val();
		if (pferdclonefuncvar > 1) {
			pferdclonefunc(pferdclonefuncvar);
		}
		
});

function pferdclonefunc(pferdanzahl){
			
			jQuery('#clonecont').find('div:hidden').each(function(){
				jQuery(this).show();
			});
			
			jQuery('fieldset.clonable input, fieldset.clonable select, fieldset.clonable textarea').each(function(){
				var s = jQuery(this).attr("rel");
				var uid = s.replace('uid', '');
				jQuery(this).attr("id", 'uid'+uid+'_1');
				jQuery(this).attr("name", 'tx_powermail_pi1[uid'+uid+'][1]');
				jQuery(this).removeAttr("onchange");
			});	
			
			if (pferdanzahl > 1) {
				var anzahl = pferdanzahl;
			} else {
				var anzahl = jQuery(this).val();
			}
			
			var anzahlNumber = parseInt(anzahl);
			var yourclass=".clonable";  //The class you have used in your form
			var clonecount = jQuery(yourclass).length;	//how many clones do we already have?
			
			for (i=clonecount;i<anzahlNumber;i++) {
					var clonecount2 = jQuery(yourclass).length;
					var newid = Number(clonecount2) + 1;		//Id of the new clone   
				
					jQuery(yourclass+":first").fieldclone({		//Clone the original elelement
						newid_: newid,						//Id of the new clone, (you can pass your own if you want)
						target_: jQuery("#clonecont"),			//where do we insert the clone? (target element)
						insert_: "append",					//where do we insert the clone? (after/before/append/prepend...)
						limit_: 10							//Maximum Number of Clones
					});
			}
			
			
			jQuery('fieldset.clonable input[type="text"],fieldset.clonable textarea').each(function(){
				if(this.value==''){this.value='--'}
			});
			
			if(clonecount>anzahlNumber) {
				var max_listCount = anzahlNumber;
				
				jQuery("#clonecont").children(".clonable").filter(function(index){ 
					return (index >= max_listCount); 
				}).remove();
	
			}
			
			jQuery('form.tx_powermail_pi1_form :input:visible').each(function(i,e){ jQuery(e).attr('tabindex', i) });
			
			return false;

}


//The Plugin Script
(function($) {

    $.fn.fieldclone = function(options) { 
    
		//==> Options <==//
		var settings = {
			newid_ : 0,
			target_: $(this),
			insert_: "before",
			limit_: 0
		};
        if (options) $.extend(settings, options);           

		if( (settings.newid_ <= (settings.limit_+1)) || (settings.limit_==0) ){	//Check the limit to see if we can clone

			//==> Clone <==//
			var fieldclone = $(this).clone(true);
			var node = $(this)[0].nodeName;
			var classes = $(this).attr("class");

			//==> Increment every input id <==//
			var srcid = 1;
			//$(fieldclone).find(':input').each(function(){
			//	var s = $(this).attr("name"); 			
			//	$(this).attr("name", s.replace(eval('/_'+srcid+'/ig'),'_'+settings.newid_)); 
			//});
			$(fieldclone).find('legend').each(function(){
				var s = $(this).text(); 			
				//$(this).text(s.replace(eval('/_'+srcid+'/ig'),'_'+settings.newid_)); 
				//$(this).addClass();
				$(this).text(s +' Nr '+settings.newid_); 
			});
			
			$(fieldclone).find(':input').each(function(){
				var s = $(this).attr("rel");
				uid = s.replace('uid', '');
				$(this).attr("id", 'uid'+uid+'_'+settings.newid_);
			});
			
			$(fieldclone).find(':input').each(function(){
				var s = $(this).attr("rel");
				uid = s.replace('uid', '');
				$(this).attr("name", 'tx_powermail_pi1[uid'+uid+']['+settings.newid_+']');
			});
			
			//$(fieldclone).find('label').each(function(){
			//	var s = $(this).attr("for"); 			
			//	$(this).attr("for", s +'_'+settings.newid_); 
			//	$(this).append(' '+settings.newid_); 
			//});
			
			$(fieldclone).find('div:hidden').each(function(){
				$(this).show();
			});
			
			
			//==> Locate Target Id <==//
			var targetid = $(settings.target_).attr("id");
			if(targetid.length<=0){
				targetid = "clonetarget";
				$(settings.target_).attr("id",targetid);
			}		

			//==> Insert Clone <==//
			var newhtml = $(fieldclone).html().replace(/\n/gi,"");
			newhtml = '<'+node+' class="'+classes+'">'+newhtml+'</'+node+'>';
			
			eval("var insertCall = $('#"+targetid+"')."+settings.insert_+"(newhtml)"); 
		}
    };
	
		

})(jQuery);    
