function dragdrop(id)
{
   this.master = document.getElementById(id);
   this.modules = new Array();
   this.mapNum = 0;
   
   this.ajax = createAjaxObject();       
   
   this.salt = '';
   
   this.imgUp = 'http://'+document.domain+'/assets/images/drag/w-gore.gif';
   this.imgDown = 'http://'+document.domain+'/assets/images/drag/w-dol.gif';
   this.imgEmpty = 'http://'+document.domain+'/assets/images/drag/brak.gif';
   
   this.dragged = new Array();
   this.draggedFade = new Array();
   this.draggedOrder = new Array();
   this.reOrder = new Array();
   this.draggedClass = new Array();
   
   this.startDrag = false;
   this.blockDrag = false;
   
   this.dragNode = null;
   this.emptyBox = null;
   this.emptyPlace = null;     
   
   this.button = -1;
   
   this.destLeft = 0;
   this.destTop = 0;
   this.srcLeft = 0;
   this.srcTop = 0;
   this.curLeft = 0;
   this.curTop = 0;
   
   this.mouseX = 0;
   this.mouseY = 0;
   
   this.startX = 0;
   this.startY = 0;
   
   this.mouse_x = 0;
   this.mouse_y = 0;
   
   this.putPlace = '';
   this.putTarget = null;
   this.movedNode = null;
   
   this.autoScrollActive = true;
   this.autoScrollSpeed = 10;
   
   this.timeoutTime = 70;
   this.timeout = null;
   
   this.doconselectstart = document.onselectstart;
   this.doconmousedown = document.onmousedown;
   this.docondragstart = document.ondragstart;
   
   this.mustReplace = false;
    
   this.topPos = function(inputObj)
   {        
    var returnValue = inputObj.offsetTop;
    while((inputObj = inputObj.offsetParent) != null)
    {
      if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
    }
    return returnValue;
   };
    
   this.leftPos = function(inputObj)
   {
      var returnValue = inputObj.offsetLeft;
      while((inputObj = inputObj.offsetParent) != null){
          if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
      }
      return returnValue;
   };
  this.getNodebyBounds = function(x,y,notNode)
  {
    var counts = this.dragged.length;
    for (var i=0;i<counts;i++)
    {
      var node = document.getElementById('wi-'+this.dragged[i]);
      if (node)
      {
        var top = this.topPos(node);
        var left = this.leftPos(node);
        if (x >= left && y>=top && x <= node.offsetWidth + left && y<=node.offsetHeight+top && node != notNode) return node;
      } 
    }
    return null;
  }; 
  this.getBoxNode = function(node)
  {
    var isInside = 0;
    var parent = node;
    if (node.className && node.className == 'wi') return node;
    
    while(parent && parent.nodeName.toLowerCase() != 'body' && node != this.master)
    {
      parent = parent.parentNode;
      if (parent.className && parent.className == 'wi') return parent;
    }
    
    return null;
  }; 
  this.mouseDown = function(e) 
  {  
     this.owner.button=-1;
     if (navigator.appName=="Netscape") this.owner.button=e.which;
     else this.owner.button=event.button;
     
     return false;
  };
  
  this.move = function(e)
  {
    if(document.all) e = event;
    var target = (e.srcElement)?e.srcElement:e.target;
    
    //var button = this.owner.mouseDown(e);
    
    //document.getElementById('__tool').innerHTML = this.owner.button;
    
    if ((this.owner.button == 2 || this.owner.button == 3) && this.owner.startDrag)
    {
      this.owner.stopDragging();
      return false;
    }
    
    /*
    if (!this.owner.startDrag)
    { 
    var curNode = this.owner.getDragNode(target);
    
    var counts = this.owner.draggedFade.length;
    
    //document.getElementById('__tool').innerHTML = target.className + ' ' + ((curNode)?curNode.id:curNode);
    
    for (var i=0;i<counts;i++)
    {
      var node = document.getElementById('wi-'+this.owner.draggedFade[i]);
      
      if (node)
      {
         if (curNode && curNode == node) this.owner.setfade(curNode,0);
         else this.owner.setfade(node,1);
      }
    }
    }
    */
    
    
    this.owner.mouse_x = e.clientX + (document.body.scrollLeft || document.body.parentNode.scrollLeft);
    this.owner.mouse_y = e.clientY + (document.body.scrollTop || document.body.parentNode.scrollTop);
    
    if (this.owner.mouseX == 0) this.owner.mouseX = this.owner.mouse_x;
    if (this.owner.mouseY == 0) this.owner.mouseY = this.owner.mouse_y;
    
    var dX = this.owner.mouse_x - this.owner.mouseX;
    var dY = this.owner.mouse_y - this.owner.mouseY;
    
    this.owner.mouseX = this.owner.mouse_x;
    this.owner.mouseY = this.owner.mouse_y;
    
    if (this.owner.dragNode && (Math.abs((this.owner.curLeft + dX) - this.owner.mouseX) > this.owner.startX))
    {
      this.owner.curLeft =  this.owner.mouseX - this.owner.startX;
      dX = 0;
    }
    if (this.owner.dragNode && (Math.abs((this.owner.curTop + dY) - this.owner.mouseY) > this.owner.startY))
    {
      this.owner.curTop =  this.owner.mouseY - this.owner.startY;
      dY = 0;
    }
    
    
     this.owner.curLeft = this.owner.curLeft + dX;
     this.owner.curTop = this.owner.curTop + dY;
    
    if (this.owner.startDrag && this.owner.dragNode && !this.owner.blockDrag) 
    {
      
      if (this.owner.autoScrollActive)
      {
       if (document.body.parentNode) 
       {
         if (document.body.parentNode.clientHeight - e.clientY < 50) document.body.parentNode.scrollTop += this.owner.autoScrollSpeed;         
         if (e.clientY < 50) document.body.parentNode.scrollTop -= this.owner.autoScrollSpeed;
       }
       else
       {
         if (document.body.clientHeight - e.clientY < 50) document.body.scrollTop += this.owner.autoScrollSpeed;         
         if (e.clientY < 50) document.body.scrollTop -= this.owner.autoScrollSpeed;
       } 
      }
      
      var nodePosTop = this.owner.topPos(this.owner.dragNode);
    
      this.owner.putTarget = this.owner.getNodebyBounds(this.owner.mouse_x,nodePosTop,this.owner.dragNode);
      this.owner.putPlace = '';
      
      
      
      var addtext = nodePosTop;
      
      if (this.owner.putTarget)
      {
       var nodeH = this.owner.putTarget.offsetHeight;
       
       var targetPosTop = this.owner.topPos(this.owner.putTarget);
       
       var diff = parseInt((nodeH/2) + (targetPosTop - this.owner.dragNode.offsetHeight));
       
       
       if (nodePosTop > diff ) this.owner.putPlace = 'before';
       if ((nodePosTop > targetPosTop && nodePosTop < targetPosTop + nodeH/2)) this.owner.putPlace = 'after';
       
       addtext += ' '+this.owner.putPlace + ' '+diff + ' '+targetPosTop + ' ' + nodeH ;   
      } 
      
      if (this.owner.putTarget && this.owner.putTarget.parentNode == this.owner.emptyBox.parentNode)
      { 
       this.owner.changePlace();
      }
       
      this.owner.dragNode.style.left = this.owner.curLeft + 'px';        
      this.owner.dragNode.style.top = this.owner.curTop + 'px';        
      
      var target = (e.srcElement)?e.srcElement:e.target; 
    }
  };
  this.changePlace = function()
  {
   switch(this.putPlace)
     {
       case 'before' : this.putTarget.parentNode.insertBefore(this.emptyBox,this.putTarget);break;
       case 'after'  :
       if (this.putTarget.nextSibling) 
       {
        this.putTarget.parentNode.insertBefore(this.emptyBox,this.putTarget.nextSibling);
        break;
       } 
       else
       {
        this.putTarget.parentNode.appendChild(this.emptyBox);
        break;
       } 
       break;
     } 
     
     if (this.putPlace == 'before' || this.putPlace == 'after') this.movedNode = this.putTarget;   
     
     this.putTarget = null;
     this.putPlace = '';
     
  };
  this.dragging = function(e)
  {
  
     
    //document.getElementById('__tool').innerHTML = this.owner.startDrag + ' ' + this.owner.blockDrag;
    if (this.owner.startDrag && this.owner.blockDrag) return true;
    
    if(document.all) e = event;     
    
    this.owner.mouseDown(e);
    
    if ((this.owner.button == 2 || this.owner.button == 3) && this.owner.startDrag)
    {
      this.owner.stopDragging();
      return false;
    }
    
    
    //if (document.selection) document.selection.empty();
    if (window.getSelection && window.getSelection() != '') window.getSelection().removeAllRanges();
    else if (document.selection && document.selection.createRange().text) document.selection.empty();
    
    this.owner.mouse_x = e.clientX + (document.body.scrollLeft || document.body.parentNode.scrollLeft);
    this.owner.mouse_y = e.clientY + (document.body.scrollTop || document.body.parentNode.scrollTop);
    
    
    this.owner.startX = Math.abs(this.owner.mouse_x - this.owner.leftPos(this));
    this.owner.startY = Math.abs(this.owner.mouse_y - this.owner.topPos(this));
        
    var target = (e.srcElement)?e.srcElement:e.target;
    
    if (!target.className || (this.owner.draggedClass.indexOf(target.className) == -1))
    {
      return true;  
    }
    
    node = this;
    owner = this.owner;
    
     document.onselectstart = function() {return false;};
     document.onmousedown = this.owner.mouseDown;
     document.ondragstart = function() {return false;}; 
    
    var top = this.owner.topPos(node);
    var left = this.owner.leftPos(node);
    
    this.owner.curLeft = left;
    this.owner.curTop = top;
    
    this.owner.emptyPlace = document.createElement('div');
    
    this.owner.emptyPlace.owner = owner;
    this.owner.emptyPlace.onmousemove = this.owner.move;
    this.owner.emptyBox = document.createElement('div');
    
    
    this.owner.emptyBox.style.width = node.offsetWidth - 4 +'px';
    this.owner.emptyBox.style.background = '#f1f1f1';
    
    if (navigator.appVersion.substr(22,3)=="6.0" || navigator.appVersion.substr(22,3)=="7.0") 
    {
      this.owner.emptyBox.style.height = node.offsetHeight - 14 +'px';
    }
    else
    {
      this.owner.emptyBox.style.height = node.offsetHeight - 4 +'px';
    }
    this.owner.emptyBox.style.marginBottom = '10px';
    
    this.owner.emptyBox.className = 'empty wi';
    
    
    this.owner.emptyBox.style.border = '2px dashed #e1e1e1';
        
    node.parentNode.insertBefore(this.owner.emptyBox,node);
    node.parentNode.insertBefore(this.owner.emptyPlace,node);
        
    node.style.position = 'absolute';
    node.style.zIndex = '10000';
    node.style.top = top +'px';
    node.style.left = left + 'px';
    node.style.textAlign = 'left';
    
    this.owner.setfade(node,1);   
    
    this.owner.dragNode = node;
    
    document.body.appendChild(this.owner.dragNode);
    
    document.body.style.mozSelection = 'none';
    
    this.owner.startDrag = true;
  };
  this.stepStopDragging = function()
  {
    var step = 15;
    
    var diffA = Math.round(Math.abs(this.owner.destLeft - this.owner.srcLeft) / 5);
    var diffB = Math.round(Math.abs(this.owner.destTop - this.owner.srcTop) / 5);
    
    if (diffA < step) diffA = step;
    if (diffB < step) diffB = step;
    
    
    if (Math.abs(this.owner.destLeft - this.owner.srcLeft) > step)
    {
     if (this.owner.destLeft > this.owner.srcLeft) this.owner.srcLeft += diffA;
     else this.owner.srcLeft -= diffA;
    }
    else
    {
      this.owner.srcLeft = this.owner.destLeft;
    }
    
    if (Math.abs(this.owner.destTop - this.owner.srcTop) > step)
    {
     if (this.owner.destTop > this.owner.srcTop) this.owner.srcTop += diffB;
     else this.owner.srcTop -= diffB;
    }
    else
    {
      this.owner.srcTop = this.owner.destTop;
    }
    
    this.owner.dragNode.style.top = this.owner.srcTop + 'px';        
    this.owner.dragNode.style.left = this.owner.srcLeft + 'px';     
    
    if (this.owner.srcTop != this.owner.destTop || this.owner.srcLeft != this.owner.destLeft)
    {
      this.owner.timeout = setTimeout(this.owner.stepStopDragging,this.owner.timeoutTime);
    }
    else
    {
      
     
      clearTimeout(this.owner.timeout);
      this.owner.stop();
      
      
      
      document.onselectstart = this.owner.doconselectstart;
      document.onmousedown = this.owner.doconmousedown;
      document.ondragstart = this.owner.docondragstart;
      
    }
  };
  this.isBlockNode = function(testnode)
  {
     var block = false;
     
     if (!testnode) return false;
     
     var node = testnode.nextSibling;
     
     while(node)
     {
      if (node.className && node.className == 'wi' && !node.getAttribute('drag')) 
       {
         node = node.nextSibling;
         while(node)
         {
           if (node && node.className && node.className == 'wi' && node.getAttribute('drag'))  
           {
             block = true;
             break;
           }
           node = node.nextSibling;
         }
           
       if (block) break;
      }
     if (node) node = node.nextSibling;
     else break;
     }
     
    return block; 
  };
  this.stop = function()
  {
    if (this.emptyBox && this.dragNode)
    {
     if (this.movedNode)
     {
       var block = this.isBlockNode(this.movedNode);
       
       if (!block)
       {
         block = this.isBlockNode(this.emptyPlace); 
       }
       
       if (block)
       {
         
         
         this.emptyPlace.parentNode.insertBefore(this.movedNode,this.emptyPlace);
         this.movedNode = null;
       }
       
     }
     
     block = false;
     
     //if (this.draggedFade.indexOf(parseInt(this.dragNode.id.replace(/wi[\-]/,''))) == -1) 
     //{
       this.setfade(this.dragNode,0);   
     //}
     
     this.emptyBox.parentNode.insertBefore(this.dragNode,this.emptyBox);
     
     this.emptyBox.parentNode.removeChild(this.emptyBox);
          
     
     this.emptyPlace.parentNode.removeChild(this.emptyPlace);
     
     this.dragNode.style.position = 'static';
     
     
     
     this.dragNode = null;
     this.emptyBox = null;
     this.emptyPlace = null;
     this.movedNode = null;
     
     this.mouseX = 0;
     this.mouseY = 0;
     
    var newOrder = new Array(); 
    var modules = new Array(); 
    
    var counts = this.modules.length;
    
    for (var i=0;i<counts;i++)
    {
      newOrder[i] = this.getOrder(this.modules[i]);
      modules[i] = this.modules[i].id.replace(/wi[\-]/g,'');
    }
    
    
    this.createCookie('gadget',this.salt,365);
    
    
    var owner = this;
    var today = new Date();     
    var query = "http://"+document.domain+"/ajax/gadget.php";        
    var func =  function(xmlResponse){owner.getResponse(owner,xmlResponse)};                   
        
        sendQueryTXT(owner.ajax,'POST',query+"?t="+today.getTime(),func,'modules='+modules+'&order='+newOrder); 
    }
  };
  this.getResponse = function(owner,xmlResponse)
  {
    this.blockDrag = false;
    this.startDrag = false;
  };
  this.stopDragging = function()
  {
    
    
    if (!this.owner) this.owner = this;
    
    if (this.owner.blockDrag) return false;
        
    if (this.owner.emptyBox && this.owner.dragNode)
    {
      this.owner.blockDrag = true;  
      
      
      this.owner.destLeft = this.owner.leftPos(this.owner.emptyBox);
      this.owner.destTop = this.owner.topPos(this.owner.emptyBox);
    
      this.owner.srcLeft = this.owner.leftPos(this.owner.dragNode);
      this.owner.srcTop = this.owner.topPos(this.owner.dragNode);
    
      window.owner = this.owner;
    
      this.owner.timeout = setTimeout(this.owner.stepStopDragging,this.owner.timeoutTime);
    }
  };
  this.getOrder = function(node)
  {
    var count = node.parentNode.childNodes.length;
    var order = 0;
    var n = 0;
    for(var i=0;i<count;i++)  
    {
      if (node.parentNode.childNodes[i].className && node.parentNode.childNodes[i].className == 'wi')
      {
       if (node.parentNode.childNodes[i] == node) 
       {
         order = n;
         break;
       }
       n++;
      }
    }
    
   return order; 
  };
  this.createCookie = function(name,value,days) 
  {
    if (days) 
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = ";expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+";path=/";
  };
 this.readCookie = function(name) 
 {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
 };
 this.eraseCookie = function(name) 
 {
  createCookie(name,"",-1);
 };
  this.replaceNode = function(node1,node2)
  {
    var emptyPlace = document.createElement('div');  
    
    node1.parentNode.insertBefore(emptyPlace,node1);     
    node2.parentNode.insertBefore(node1,node2);     
    emptyPlace.parentNode.insertBefore(node2,emptyPlace);     
    
    emptyPlace.parentNode.removeChild(emptyPlace);
  };
  this.getNodeAtOrder = function(order,node)
  {
    var count = node.parentNode.childNodes.length;
    var n = 0;
    for(var i=0;i<count;i++)  
    {
      if (node.parentNode.childNodes[i].className && node.parentNode.childNodes[i].className == 'wi')
      {
       if (n == order) return node.parentNode.childNodes[i];
       n++;
      }
    }
    
   return null; 
  };
  this.setfade = function(node,type)
  {
    if (!type && node)
    {
      if (!document.all)
      {
       if (node.style.opacity != 1) node.style.opacity = 1;
      }
      else
      { 
       if (node.firstChild.style.filter != 'alpha(opacity=100)') node.firstChild.style.filter = 'alpha(opacity=100)'; 
      } 
    }
    else if (type && node)
    {
      if (!document.all)
      {
       if (node.style.opacity != 0.6) node.style.opacity = 0.6;
      }
      else
      { 
       if (node.firstChild.style.filter != 'alpha(opacity=60)') node.firstChild.style.filter = 'alpha(opacity=60)'; 
      } 
    }
  };
  this.init = function()
  {       
    this.owner = this;
    document.owner = this;
    document.onmousemove = this.move; 
    document.onmouseup = this.stopDragging; 
    
    
    var counts = this.dragged.length;
    for (var i=0;i<counts;i++)
    {
      var node = document.getElementById('wi-'+this.dragged[i]);
      
      
      if (node)
      {
        node.setAttribute('drag',1);
        
        node.owner = this;
        
        /*
        if (this.draggedFade.indexOf(this.dragged[i]) != -1)
        {
          this.setfade(node,1);
        }
        */
         
        node.onmousedown = this.dragging;
        node.onmousemove = this.move; 
        this.modules[this.modules.length] = node;
        
        var divs = node.getElementsByTagName('div');
        for (var n=0;n<divs.length;n++) if (divs[n].className && this.draggedClass.indexOf(divs[n].className) >=0) divs[n].style.cursor = 'move';  
          
        var divs = node.getElementsByTagName('img');
        for (var n=0;n<divs.length;n++) if (divs[n].className && this.draggedClass.indexOf(divs[n].className) >=0) divs[n].style.cursor = 'move';  
        
        var divs = node.getElementsByTagName('a');
        for (var n=0;n<divs.length;n++) if (divs[n].className && this.draggedClass.indexOf(divs[n].className) >=0) divs[n].style.cursor = 'move';  
        
        var divs = node.getElementsByTagName('area');
        for (var n=0;n<divs.length;n++) 
        {
          divs[n].owner = this;
          divs[n].onclick = function(){this.owner.drag(this)}; 
          divs[n].onfocus = function(){this.blur()};
          divs[n].onmouseout = function(){this.owner.blurBtn(this)};
          divs[n].onmouseover = function(){this.owner.hoverBtn(this)};
          
          //"document.getElementById(\'imgmap'.$boxID.'\').src=\''.$mosConfig_live_site.'assets/images/drag/brak.gif\'" onmouseover="document.getElementById(\'imgmap'.$boxID.'\').src=\''.$mosConfig_live_site.'assets/images/drag/w-dol.gif\'"
        } 
        /*
        if (this.draggedOrder[i] != this.getOrder(node))
        {
          this.replaceNode(node,this.getNodeAtOrder(this.draggedOrder[i],node)); 
        }
        */
        
      } 
    }
  };
  this.blurBtn = function(node)
  {
    var img = document.getElementById(node.getAttribute('image'));
    
        img.src = this.imgEmpty;
    
  };
  this.hoverBtn = function(node)
  {
    var img = document.getElementById(node.getAttribute('image'));
    if (node.getAttribute('tooltip') == 'up' && this.getPrevNode(this.getDragNode(node)))
    {
      img.src = this.imgUp;
    } 
    if (node.getAttribute('tooltip') == 'down' && this.getNextNode(this.getDragNode(node)))
    {
      img.src = this.imgDown;
    } 
  };
  this.getNextNode = function(node)
  {
    var n = node;
    while(n = n.nextSibling)
    {
      if (n.className && n.className == 'wi' && n.getAttribute('drag')) return n;
      if (n.className && n.className == 'wi' && !n.getAttribute('drag')) this.mustReplace = true;
    }
    return null;
  };
  this.getPrevNode = function(node)
  {
    var n = node;
    while(n = n.previousSibling)
    {
      if (n.className && n.className == 'wi' && n.getAttribute('drag')) return n;
      if (n.className && n.className == 'wi' && !n.getAttribute('drag')) this.mustReplace = true;
    }
    return null;
  };
  this.dragDown = function(node)
  {
    
    if (node)
    {
      place = this.getNextNode(node);
      
      if (place)
      {
        if (place.nextSibling)  
        {
          if (this.mustReplace)
          {
            this.replaceNode(node,place);
          }
          else node.parentNode.insertBefore(node,place.nextSibling);
        }
        else
        {
          node.parentNode.appendChild(node);
        }
      }
    }
    return false;
  };
  this.dragUp = function(node)
  {
    if (node)
    {
      place = this.getPrevNode(node);
      
      if (place)
      {
        if (this.mustReplace)
        {
          this.replaceNode(node,place);
        }
        else node.parentNode.insertBefore(node,place);
      }
    }
    return false;
  };
  this.getDragNode = function(node)
  {
    var find=null;
    var n = node;
    while(n = n.parentNode)
    {
      if (n.className == 'wi') return n;
      
    }
    return find;
  };
  this.drag = function(node)
  {
    if (this.startDrag) return false;  
    
    this.startDrag = true;
    
    var img = document.getElementById(node.getAttribute('image'));
        img.src = this.imgEmpty;
    
    var way = node.getAttribute('tooltip');
    
    this.mustReplace = false;
    
    if (way == 'down')
    {
      this.dragDown(this.getDragNode(node));
    }
    if (way == 'up')
    {
      this.dragUp(this.getDragNode(node));
    }
    
    node.onfocus = function(){this.blur()};
    
    var newOrder = new Array(); 
    var modules = new Array(); 
    
    var counts = this.modules.length;
    
    for (var i=0;i<counts;i++)
    {
      newOrder[i] = this.getOrder(this.modules[i]);
      modules[i] = this.modules[i].id.replace(/wi[\-]/g,'');
    }
    
    
    this.createCookie('gadget',this.salt,365);
    
    
    var owner = this;
    var today = new Date();     
    var query = "http://"+document.domain+"/ajax/gadget.php";        
    var func =  function(xmlResponse){owner.getResponse(owner,xmlResponse)};                   
        
        sendQueryTXT(owner.ajax,'POST',query+"?t="+today.getTime(),func,'modules='+modules+'&order='+newOrder); 
    
    return true;
  }
};
Array.prototype.indexOf = function(v)
{
       for(var i = this.length; i-- && this[i] !== v;);
       return i;
};