fabrikForm = new Class( {
	
	initialize: function( ) {
		this.elements = $H({});
		this.delGroupJS = $H({});
		this.duplicateGroupJS = $H({});
		this.duplicatedGroups = {};
		this.clickDeleteGroup = this.deleteGroup.bindAsEventListener(this);
		this.clickDuplicateGroup = this.duplicateGroup.bindAsEventListener(this);
		//put here as in IE in admin watchgroups doesnt work on domready event
		window.addEvent('load', function(){
			this.watchGroupButtons();
		}.bind(this));
	},
		
	addElement: function( oEl ){
		if(oEl.element){
			elId = oEl.element.id.replace('[]', '');
			this.elements.set(elId, oEl);
		}
	},

	dispatchEvent: function( elementType, elementId, action, js){
		var el = this.elements.get(elementId);
		if(el){
			action = action.replace('on', '');
			el.addNewEvent( action, js );
		}
	},
		
	action: function( task, element ){
		var oEl = this.elements.find( function( oEl ){
			return (oEl.element.id == element);
		})
		eval('oEl.' + task + '()');
	},

	watchGroupButtons: function(){
		this.unwatchGroupButtons();
		$$('.deleteGroup').each(function(item, index){
			item.addEvent('click', this.clickDeleteGroup);
		}, this);
		$$('.addGroup').each(function(item, index){
			item.addEvent('click', this.clickDuplicateGroup);
		}, this);
	},

	unwatchGroupButtons: function(){
		$$('.deleteGroup').each(function(g){
		  g.removeEvent('click', this.clickDeleteGroup);
		}, this);
		$$('.addGroup').each(function(g){
		  g.removeEvent('click', this.clickDuplicateGroup);
		}, this);
	},

	addGroupJS: function(groupId, event, js){
		if(event == 'delete'){
			this.delGroupJS.set(groupId, js);
		}else{
			this.duplicateGroupJS.set(groupId, js);
		}
	},

	deleteGroup: function(e){
		var event = new Event(e);
		var b = event.target;
		var i = b.id.replace('delGroup_', '');
		var subgroups = $$('#group' + i + ' .fb_sub_group');
		var endSubGroup = subgroups.pop();
		var js = this.delGroupJS.get(i);
		var myFx = new Fx.Style(endSubGroup, 'opacity', {duration:300,
			onComplete : function(){
				if(subgroups.length != 0){
					endSubGroup.remove();
				}
				eval(js);
			}});
		myFx.start(1, 0);
		event.stop();
	},

	/* duplicates the groups sub group and places it at the end of the group */ 

	duplicateGroup: function(e){
		var event = new Event(e);
		var b = event.target;
		var i = b.id.replace('addGroup_', '');
		var js = this.duplicateGroupJS.get(i);
		var group = $('group'+i);
		var subgroups = $$('#group' + i + ' .fb_sub_group');
		var c = subgroups.length;		
		if(group.style.display == 'none'){
			group.style.display = 'block';	
		}else{
			var subgroup = $('subgroup'+i+'_0');
			var clone = null;
			var found = false;
			for (var j in ofabrik.duplicatedGroups)
			{
			 if(j == i){
				found = true;
				}
			}
			if(!found){
				this.duplicatedGroups[i] = clone;
				clone = subgroup.cloneNode(true);
			}else{
				if(!subgroup){
					clone = this.duplicatedGroups[i];
				}else{
					clone = subgroup.cloneNode(true);
				}
			}

			group.appendChild(clone);
			clone.id = 'subgroup'+i+'_' + c;
			var children = $(clone.id).getElements('[rel^=fabrik]') 

			//remove values and increment ids
			children.each(function(cNode){
				if(cNode.nodeType == 1 && cNode.id != ''){
					var oldId = cNode.id;
					if(cNode.id.indexOf('[]') != -1){
						cNode.id = cNode.id.replace('[]', '') + c + '[]';
					}else{
						cNode.id = cNode.id + c;
					}
					if(cNode.name){
						r = eval('/\[' + '0' + ']/');
						cNode.name = cNode.name.replace(r, '1');
					}
					cNode.value = '';
				}
			});
			c = c + 1;
			
			var myFx = new Fx.Style(clone, 'opacity', {duration: 500}).set(0);
			myFx.start(1);
			
		}
		eval(js);
		this.watchGroupButtons();
		event.stop();
	}
});
	
ofabrik = new fabrikForm();
