/**
 * @fileoverview
 * 
 * <p>Copyright (c) 2006 Peter Michaux. All rights reserved.<br />
 * Code licensed under the MIT License:<br />
 * http://michaux.ca/svn/dragdrop/trunk/MIT-LICENSE.txt</p>
 *
 * @author Peter Michaux - http://peter.michaux.ca
 * @version 0.1 (August 2006)
 */

/**
 * @object
 *
 * <p>The DragMultiple object is designed to have it's properties dynamically
 * added to an instance of DragManager or a subclass of DragManager. By dynamically
 * adding these properties the Manager will gain mulitiple selection and 
 * multiple drag functionality.</p>
 *
 * <p>To add the mutliple drag functionality, the following DragMultiple
 * properites will overwrite DragManager's properties.<p>
 *
 * <ul>
 *   <li>selected</li>
 *   <li>makeSelection()</li>
 *   <li>handleMouseDownOffDraggable()</li>
 *   <li>isSelected()</li>
 *   <li>select()</li>
 *   <li>unselect()</li>
 * </ul>
 *
 * <p>The selected property is changed from an HTMLElement object to and array
 * of HTMLElement objects. The proxy and interesting moment hooks may need adjusting
 * to accomodate either just the array version of selected or both
 * the array and non-array versions.</p>
 */
DragMultiple = {
  
  selected: [],
  
  makeSelection: function(e, draggable) {
    /* Opera Mac doesn't know about the meta key so use shift key instead */
    var meta = e.altKey;
    if (!this.isSelected(draggable)) {
    	/* Could only unselect the others.
    	 * However, this is a good sledge hammer approach
    	 * to unselect everything and then select the new one. */
    	if (!meta) {
    		this.unselect();
    	}
    	this.select(draggable);
    } else if (meta) {
    	this.unselectOne(draggable);
    }
  },
  
  makeUnselection: function() {},
  
  handleMouseDownOffDraggable: function() {
	  this.unselect();
  },
  
  isSelected: function(draggable) {
		for (var i=0; i<this.selected.length; i++) {
			if (draggable === this.selected[i]) {
				return true;
			}
		}
		return false;
	},
	
	select: function(draggable) {
	  this.onSelect(draggable);
		this.selected.push(draggable);
	},
	
	selectAll: function() {
		this.unselect();
    var draggables = YAHOO.util.Dom.getElementsByClassName("draggable");

		for (var i=0; i<draggables.length; i++) {
				this.select(draggables[i]);				
		}
	},
	
	unselectOne: function(draggable) {
	  this.onUnselect(draggable);
		for (var i=this.selected.length-1; i>=0; i--) {
			if (draggable === this.selected[i]) {
				this.selected.splice(i,1);
			}
		}
	},
	
	unselect: function() {
		for (var i=0; i<this.selected.length; i++){
      this.onUnselect(this.selected[i]);
		}
		this.selected = [];
	}
	
};

