/*******************************************************************************
* ddi.js - Drag & Drop Interface for Internet Explorer
*
* $Source: H:\\CVS\\cvsrepo/Moonedge\040Javascripts/ddi.js,v $
* $Revision: 1.1 $
* $Date: 2005/08/31 13:30:35 $
* $Author: Markus $
*
* Copyright (C) 2005 Moonedge E-Business GmbH, http://www.moonedge.ch
*******************************************************************************/

/*------------------------------------------------------------------------------

* To link a source element to the target just drag it over
* To move a source element to the target press the shift-key while dragging
* To link a source element to the target press the ctrl-key while dragging

For each element (source or target) you can specify an element-type. In addition 
you can define a list of element-types the element accepts. You can only drop a 
source element onto a target element if: 

  - the source accepts the element-type of the target and
  - the target accepts the element-type of the source

If an element has no element-type defined it is allways accepted. If the list of 
accepted types is 'all' or '*' all element-types will be accepted.

Defining a source element:
--------------------------

DDI.source(
  element_id          // id of the element
  [,element_type      // type of the source element
  [,link_parameter    // parameter to pass to the drop action
  [,linktargets_list  // list of allowed target types the element can be linked to (delimited by ;)
  [,movetargets_list  // list of allowed target types the element can be moved to (delimited by ;)
  [,copytargets_list  // list of allowed target types the element can be copied to  (delimited by ;)
  [,highlight_class   // name of a CSS definition to highlight the element while dragging
  ]]]]]]
)

Defining a target element:
--------------------------

DDI.target(
  element_id          // id of the element
  [,element_type      // type of the target element
  [,link_parameter    // parameter to pass to the drop action
  [,linktargets_list  // list of allowed source types the element accepts for linking (delimited by ;)
  [,movetargets_list  // list of allowed source types the element accepts for moveing (delimited by ;)
  [,copytargets_list  // list of allowed source types the element accepts for copying (delimited by ;)
  [,highlight_class   // name of a CSS definition to highlight the element while dropping
  ]]]]]]
)

Example:
--------

DDI.linkURL('link.asp')
DDI.moveURL('move.asp')
DDI.copyURL('copy.asp')

DDI.source('id1','type1','src=id1','type2;type3',,'type3')
DDI.target('id2','type2','tgt=id2','type1')
DDI.target('id3','type3','tgt=id3',,,'all')

- id1 can be linked id2, because id1 can be linked to type2-elements and id2 accepts type1-elements for linking
  The URL called after dropping will be: "link.asp?src=id1&tgt=id2"
- id1 can be copied to id3, because id1 can be copied to type3-elements and id3 accepts any type for copying
  The URL called after dropping will be: "copy.asp?src=id1&tgt=id3"

------------------------------------------------------------------------------*/

var DDI = {
  lURL : '',
  mURL : '',
  cURL : '',
  
  linkURL : function(u) { lURL = u },
  moveURL : function(u) { mURL = u },
  copyURL : function(u) { cURL = u },
  
  inList : function(l,i) { 
    var ll = ';'+l+';'; 
    return ((l == '*') || (i == '') || (l.toLowerCase() == 'all') || (ll.toLowerCase().indexOf(';'+i.toLowerCase()+';') >= 0));
  },
  
  source : function(i,t,para,lt,mt,ct,hl) {
    var o = document.getElementById(i)
    if (o) {
      o.ondragstart = DDI.dragStart;
      o.ondragend = DDI.dragEnd;

		  o.__srcType = typeof t != 'undefined' ? t.toString() : '';
  		o.__srcPara = typeof para != 'undefined' ? para.toString() : '';
	  	o.__srcLTypes = typeof lt != 'undefined' ? lt.toString() : '';
		  o.__srcMTypes = typeof mt != 'undefined' ? mt.toString() : '';
  		o.__srcCTypes = typeof ct != 'undefined' ? ct.toString() : o.__srcMTypes;
	  	o.__srcHighlite = typeof hl != 'undefined' ? hl : o.className;
		  o.__className = o.className
		}  
  },
  
  target : function(i,t,para,lt,mt,ct,hl) {
    var o = document.getElementById(i)
    if (o) {
      o.ondragenter = DDI.dragEnter;
      o.ondragover = DDI.dragOver;
      o.ondragleave = DDI.dragLeave;
      o.ondrop = DDI.dragDrop;
  
  		o.__tgtType = typeof t != 'undefined' ? t.toString() : '';
  		o.__tgtPara = typeof para != 'undefined' ? para.toString() : '';
  		o.__tgtLTypes = typeof lt != 'undefined' ? lt.toString() : '';
  		o.__tgtMTypes = typeof mt != 'undefined' ? mt.toString() : '';
  		o.__tgtCTypes = typeof ct != 'undefined' ? ct.toString() : o.__tgtMTypes;
  		o.__tgtHighlite = typeof hl != 'undefined' ? hl : o.className;
     	o.__className = o.className
    } 	
  },
  
  dragStart : function() { // fires on source
    var m = 0
    window.event.dataTransfer.setData('text',this.id);
    if (this.__srcCTypes != '') m += 1     
    if (this.__srcMTypes != '') m += 2     
    if (this.__srcLTypes != '') m += 4     
    switch(m) {
      case 0: window.event.dataTransfer.effectAllowed = 'none'; break;
      case 1: window.event.dataTransfer.effectAllowed = 'copy'; break;
      case 2: window.event.dataTransfer.effectAllowed = 'move'; break;
      case 3: window.event.dataTransfer.effectAllowed = 'copyMove'; break;
      case 4: window.event.dataTransfer.effectAllowed = 'link'; break;
      case 5: window.event.dataTransfer.effectAllowed = 'copyLink'; break;
      case 6: window.event.dataTransfer.effectAllowed = 'linkMove'; break;
      case 7: window.event.dataTransfer.effectAllowed = 'all'; break;
    }  
    this.className = this.__srcHighlite;
  },

  dragEnd : function() { // fires on source
    this.className = this.__className;
  },

  dragEnter : function() { // fires on target
    window.event.dataTransfer.dropEffect = 'none';
    window.event.returnValue = false;
    this.className = this.__tgtHighlite;
  },

  dragOver : function() { // fires on target
    var p,o
    window.event.returnValue = false;
    window.event.dataTransfer.dropEffect = 'none';
    p = window.event.dataTransfer.getData('text');
    if (p != null && p.indexOf('http://') < 0) {
      o = document.getElementById(p); // get the source element
      if (o && o.__srcType) { // source element is an DDI element
        if (window.event.ctrlKey) {
          if (DDI.inList(this.__tgtCTypes,o.__srcType) && DDI.inList(o.__srcCTypes,this.__tgtType)) 
            window.event.dataTransfer.dropEffect = 'copy';
        } else if (window.event.shiftKey) {
          if (DDI.inList(this.__tgtMTypes,o.__srcType) && DDI.inList(o.__srcMTypes,this.__tgtType)) 
            window.event.dataTransfer.dropEffect = 'move';
        } else {
          if (DDI.inList(this.__tgtLTypes,o.__srcType) && DDI.inList(o.__srcLTypes,this.__tgtType)) 
            window.event.dataTransfer.dropEffect = 'link';
        } 
      }  
    }  
  },

  dragLeave : function() { // fires on target
    this.className = this.__className;
  },

  dragDrop : function() { // fires on target
    var o = document.getElementById(window.event.dataTransfer.getData('text'));
    if (o && o.__srcType) { // source element is an DDI element
      var act = ''
      if (window.event.ctrlKey) {
        if (DDI.inList(this.__tgtCTypes,o.__srcType) && DDI.inList(o.__srcCTypes,this.__tgtType)) 
          act = typeof cURL != 'undefined' ? cURL : '';
      } else if (window.event.shiftKey) {
        if (DDI.inList(this.__tgtMTypes,o.__srcType) && DDI.inList(o.__srcMTypes,this.__tgtType)) 
          act = typeof mURL != 'undefined' ? mURL : '';
      } else {
        if (DDI.inList(this.__tgtLTypes,o.__srcType) && DDI.inList(o.__srcLTypes,this.__tgtType)) 
          act = typeof lURL != 'undefined' ? lURL : '';
      } 
      this.className = this.__className;
      window.event.returnValue = false;

      if (act != '') {
        if (act.indexOf('?') < 0) {
          act = act + '?' + o.__srcPara + '&' + this.__tgtPara
        } else {
          act = act + '&' + o.__srcPara + '&' + this.__tgtPara
        }  
        self.location.href = act
      }  
    }  
  }
}; 

