/*
Class: Drag.Group and Drag.Group.Item

Author: Nathan White http://www.nwhite.net/
License:
	MIT-style license.

Arguments:
	options - an object. See options Below.

Options:
	active : activates group dragging, if off reverts to standard drag behavior
	store : the name the drag option is stored under on the element
	filter : function that takes an element to check for drag grouping
	drag : Drag.Move options object

*/

Drag.Group = new Class({

	Implements : [Options],

	options : {
		'active' : true,
		'store' : 'drag-group-item',
		'filter' : $lambda(true),
		'drag' : {}
	},

	elements : [],

	initialize : function(options){
		this.setOptions(options);
	},

	add : function(el,options){
		var drag = new Drag.Group.Item(el, this, $merge(this.options.drag,options))
		el.store(this.options.store, drag );
		this.elements.push(el);
		return drag;
	},

	find : function(el) {
		var pos = null;
		
		this.elements.each(function(elem, idx) {
			if (elem.getAttribute('id') == el.getAttribute('id'))
				pos = idx;
		});
		
		return pos;
	},
	
	remove : function(el){
		var idx = this.find(el);
		if (idx) 
			this.elements.splice(idx, 1);
	},

	start : function(el,event){
		if(!this.options.active || !this.options.filter(el))
			el.retrieve(this.options.store).start(event,true);
		else {
			this.elements.filter(this.options.filter).each(function(match){
				if (match) {
					try {
						match.retrieve(this.options.store).start(event,true);
					}
					catch (ex) {
						console.log(match);
						console.log(ex);
					}
				}
			},this);
		}
	}
});

Drag.Group.Item = new Class({

	Extends : Drag.Move,

	initialize : function(el,group,options){
		this.group = group;
		this.parent(el,options);
	},

	start : function(event,alerted){
		if(alerted) this.parent(event);
		else this.group.start(this.element,event);
	}
});
