YAHOO.namespace("com.lgan.Analyzer");

(function(){
    var
      Dom = YAHOO.util.Dom,
      Event = YAHOO.util.Event,
      CustomEvent = YAHOO.util.CustomEvent,
      Config = YAHOO.util.Config,
      Analyzer = YAHOO.com.lgan.Analyzer,
      Logger = YAHOO.com.lgan.Logger || YAHOO
    ;
    
    /**
      Additional property:  elStartParent, the parent of the drag element at drag commencement.  Set to null at the end of endDrag.
    */
    Analyzer.DDProxy = function(id, sGroup, config){
        Analyzer.DDProxy.superclass.constructor.call(this, id, sGroup, config);
        this.elStartParent = null;
        this.elInitNextSibling = null;
    };
    
    /**
      Fires when a variable is dropped on the filter list.  The signature is FLAT, not LIST, so arguments (singular or multiple) are passed and received as an Object.
    */
    Analyzer.DDProxy.DropOnFilter = new CustomEvent("DropOnFilter");
    Analyzer.DDProxy.DropOnFilter.signature = CustomEvent.FLAT;
    
    /**
      Fires when a variable is dropped on any active-variable list, including the filter list.  The signature is FLAT, not LIST, so arguments (singular or multiple) are passed and received as an Object.
    */
    Analyzer.DDProxy.VariableMoved = new CustomEvent("VariableMoved");
    Analyzer.DDProxy.VariableMoved.signature = CustomEvent.FLAT;
    
    /**
      Fires when any variable is dragged, "active" or not.  Expected argument:  The HTML element drag-drop handle.
    */
    Analyzer.DDProxy.VarDragStart = new CustomEvent("VarDragStart");
    Analyzer.DDProxy.VarDragStart.signature = CustomEvent.FLAT;
    
    /**
      Fires when any variable motion completes, whether the variable moved or not or whether the variable was dragged or bumped around by some other script.  Expected argument:  The HTML element drag-drop handle.
    */
    Analyzer.DDProxy.VarMotionComplete = new CustomEvent("VarMotionComplete");
    Analyzer.DDProxy.VarMotionComplete.signature = CustomEvent.FLAT;
    
    /**
      Fires when a variable is dragged from somewhere in the "active" area.
    */
    Analyzer.DDProxy.ActiveVarDragStart = new CustomEvent("ActiveVarDragStart");
    Analyzer.DDProxy.ActiveVarDragStart.signature = CustomEvent.FLAT;
    
    /**
      Fires after a variable dragged from somewhere in the "active" area is dropped.
    */
    Analyzer.DDProxy.ActiveVarDragEnd = new CustomEvent("ActiveVarDragEnd");
    Analyzer.DDProxy.ActiveVarDragEnd.signature = CustomEvent.FLAT;
    
    /**
      Fires when a variable is dragged from somewhere in the "active" area.
    */
    Analyzer.DDProxy.InactiveVarDragStart = new CustomEvent("InactiveVarDragStart");
    Analyzer.DDProxy.InactiveVarDragStart.signature = CustomEvent.FLAT;
    
    /**
      Fires after a variable dragged from somewhere in the "active" area is dropped.
    */
    Analyzer.DDProxy.InactiveVarDragEnd = new CustomEvent("InactiveVarDragEnd");
    Analyzer.DDProxy.InactiveVarDragEnd.signature = CustomEvent.FLAT;
    
    Analyzer.DDProxy.ActivatingDropGroupName = "ActivateDrop";
    Analyzer.DDProxy.DeactivatingDropGroupName = "DeactivateDrop";
    
    YAHOO.extend(
      Analyzer.DDProxy,
      YAHOO.example.DDList,
      {
        /**
          Calls superclass method, then stores a reference to the parent of the drag element.
        */
        startDrag: function(x,y){
            //Logger.log(["Analyzer.DDProxy Properties:", "id:  " + this.id, "groups:  " + YAHOO.lang.JSON.stringify(this.groups)].join("\n"));
            
            Analyzer.DDProxy.VarDragStart.fire(this.getEl());  //Fire this first because it changes a potentially visual CSS class
            
            Analyzer.DDProxy.superclass.startDrag.call(this, x, y);
            
            var thisel = this.getEl();
            this.elStartParent = thisel.parentNode;
            this.elInitNextSibling = thisel.nextSibling;
            
            //This if-else block of code may be deprecated, since trees are not in use in any Analyzers right now.  --AJN 20080611
            if(this.groups[ Analyzer.DDProxy.ActivatingDropGroupName ]){
                Analyzer.DDProxy.ActiveVarDragStart.fire(this);
            } else {
                Analyzer.DDProxy.InactiveVarDragStart.fire(this);
                this.addToGroup(Analyzer.DDProxy.ActivatingDropGroupName);
            }
        },
        
        /**
          Calls superclass method, then removes the reference to the parent of the drag element, since it may no longer be correct at this point.
        */
        endDrag: function(e){
            Analyzer.DDProxy.superclass.endDrag.call(this, e);
            
            if(Dom.hasClass(this.elStartParent, "active")){
                Analyzer.DDProxy.ActiveVarDragEnd.fire(this);
            } else {
                //?
            }
            
            this.elStartParent = null;
            this.elInitNextSibling = null;
            
            Analyzer.DDProxy.VarMotionComplete.fire(this.getEl());
        },
        
        /**
          This event aborts if not dropped on a varbin.
          onDragDrop fires another event for the filter box.
        */
        onDragDrop: function(e, id){
            var objEventInfo = {
              strDDId: this.id,
              eventE: e,
              strDropTargetId: id,
              strStartParentId: this.elStartParent.id
            };
            
            var el = Dom.get(id);
            if(!Dom.hasClass(el, "varbin")){
                //Nop
            } else {
                if(Dom.hasClass(el, "generated")){
                    //Nop
                } else {
                    if(id == "ulDDFilters"){
                        Analyzer.DDProxy.DropOnFilter.fire(objEventInfo);
                    }
                    if(Dom.hasClass(el, "source")){
                        this.anim.duration = 0;
                    }
                    Analyzer.DDProxy.superclass.onDragDrop.call(this, e, id);
                    this.anim.onComplete.subscribe(function() {
                        Analyzer.DDProxy.VariableMoved.fire(objEventInfo);
                    });
                }
            }
        }
      }
    );
})();

