 $(document).ready(function(){	 
	dynamicBlocks.init('partList');
});
 
dynamicBlocks = {
	el: null,
	templateEl:null,
	
	currentIndex: 1,
	
	init: function(id) {
		this.el = jQuery("#"+id);
		this.templateEl = jQuery('.dynamicItemTemplate');
		
		this.currentIndex = jQuery('.dynamicItem').length-1;
	},

	addNew: function() {
		this.el.append(this._getTemplate(this.currentIndex++));
		this._rebuildSequence();
	},
	
	remove: function(id) {
		this.el.find(".dynamicItem_"+id).remove();
		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);
		
		
	}
}
