 $(document).ready(function(){	 
	dynamicBlocks.init('partList');
});
 
dynamicBlocks = {
	el: null,
	templateEl:null,
	
	currentIndex: 1,
	
	init: function(id) {
		this.el = jQuery("#"+id);
		this.templateEl = jQuery('.dynamicItemTemplate');
		
		index = 0;
		jQuery('#orderParts').find('input[type=hidden][name^=parts]').each(function() {
		   el = jQuery(this);
		   
		   newIndex = el.attr('name').substr(6,1);
		   if(index < newIndex) index = newIndex;
		});
		this.currentIndex = parseInt(index)+1;
	},

	addNew: function() {
		this.el.append(this._getTemplate(this.currentIndex));
		this._addValidation(this.currentIndex);
		
		this.currentIndex++;
		
		this._rebuildSequence();
	},
	
	remove: function(id) {
		this.el.find(".dynamicItem_"+id).remove();
		this._removeValidation(id)
		this._rebuildSequence();
	},
	
	
	getBrothers: function(element) {
		name = jQuery(element).attr('name');
		els = new Array();
		index = 0;
		
		for(i=0; i<=dynamicBlocks.currentIndex; i++) {
		    el = jQuery("input[name="+name.replace(/0/,i)+"],textarea[name="+name.replace(/0/,i)+"],select[name="+name.replace(/0/,i)+"]");
		    if(el.length>0) {
		        els[index++] = document.getElementById(el.attr('id'));
		    }
		}
		
		return els;
	},
	
	
	_rebuildSequence: function() {
		i = 0;
		jQuery(".dynamicList .dynamicItemNumber").each(function() {
			jQuery(this).html(++i);
		})
	},
	
	_getTemplate: function(index) {
		return this.templateEl.html().replace(/{{num}}/g,index).replace(/{{numplus}}/g,index+1);
	},
	
	_getTemplateFields: function () {
		var fields = new Array();
		i = 0;
		
		// getting fields
		dynamicBlocks.templateEl.find("input, select, textarea").each(function() {
		    el = jQuery(this);

		    found = false;
		    for(j=0; j<fields.length; j++) {
		        if(fields[j] == el.attr('name')) found = true;
		    }

		    if(!found)
		        fields[i++] = el.attr('name');
		})
		return fields;
		
	},
	
	_addValidation: function(index) {
		fields = this._getTemplateFields();
		// getting rules & messages for fields & dublicate them for new item
		for(j=0; j<fields.length; j++) {
		    name = fields[j];
		    rules = null;
		    messages = null;

		    zeroField = name.replace('{{num}}', 0);
		    if(dynamicValidator.validator.settings.rules[zeroField] != null) {
		    	dynamicValidator.validator.settings.rules[name.replace('{{num}}', index)] = dynamicValidator.validator.settings.rules[zeroField];
		        dynamicValidator.validator.settings.messages[name.replace('{{num}}', index)]= dynamicValidator.validator.settings.messages[zeroField];
		    }
		    
		}
	},
	
	_removeValidation: function(index) {
		fields = this._getTemplateFields();
		// getting rules & messages for fields & dublicate them for new item
		for(j=0; j<fields.length; j++) {
		    name = fields[j];
		    rules = null;
		    messages = null;
		    
		    dynamicValidator.validator.settings.rules[name.replace('{{num}}', index)] = null;
		    dynamicValidator.validator.settings.messages[name.replace('{{num}}', index)] = null;
		}
	}
}
