﻿//drag object class
  function dynamicWindow(winId)
  {
    this.isDrag=false;
    this.oldX=0;
    this.oldY=0;
    this.oldX2=0;
    this.oldY2=0;
    this.currentWindow=null; 
    this.id=winId;
    this.dragElements=[winId];
    this.activeElements=[];
    this.onStartDrag=false;
    this.onStopDrag=false;
  }
  
  var dynamicWindowObjectLink;
  
  dynamicWindow.prototype.stopDragHandler=function()
  {
     dynamicWindowObjectLink.isDrag=false;
     if (dynamicWindowObjectLink.onStopDrag) dynamicWindowObjectLink.onStopDrag();
  }
  
  dynamicWindow.prototype.addDraggingElements=function(elArr)
  {
     for (var i=0;i<elArr.length;i++)
        this.dragElements.push(elArr[i]);
  }
  
  dynamicWindow.prototype.startDragHandler=function(e)
  {
      var ev = e||event;
      var el = ev.target||ev.srcElement; 
      for (var i=0;i<dynamicWindowObjectLink.dragElements.length;i++)
      {
         if (el.id==dynamicWindowObjectLink.dragElements[i]||el.parentNode.id==dynamicWindowObjectLink.id)
         {
            dynamicWindowObjectLink.isDrag=true;
            dynamicWindowObjectLink.oldX=ev.clientX;
            dynamicWindowObjectLink.oldY=ev.clientY;
            if (dynamicWindowObjectLink.onStartDrag) dynamicWindowObjectLink.onStartDrag();
            break;
         }
      }
  }
  
  dynamicWindow.prototype.mouseMoveHandler=function(e)
  {
      if (!dynamicWindowObjectLink||!dynamicWindowObjectLink.isDrag) return;
      var ev = e||event;
      var vx=dynamicWindowObjectLink.oldX-ev.clientX;
      var vy=dynamicWindowObjectLink.oldY-ev.clientY;
      dynamicWindowObjectLink.currentWindow.style.left=(dynamicWindowObjectLink.currentWindow.offsetLeft-vx)+'px';
      dynamicWindowObjectLink.currentWindow.style.top=(dynamicWindowObjectLink.currentWindow.offsetTop-vy)+'px';
      dynamicWindowObjectLink.oldX=ev.clientX;
      dynamicWindowObjectLink.oldY=ev.clientY;
      dynamicWindowObjectLink.removeSelection();
  }   
  
  dynamicWindow.prototype.create=function(div,container)
  { 
      if (this.currentWindow!=null) return;
      dynamicWindowObjectLink=this;      
      div.id=this.id;
      if (!container) container=document.body;
      container.appendChild(div);
      this.currentWindow=document.getElementById(this.id);
      this.setDraggable();
  }
  
  dynamicWindow.prototype.setDraggable=function()
  {
      var rootNode = document.body.parentNode? document.body.parentNode: document.body;
      this.safeAttachEvent(rootNode,'mouseup',this.stopDragHandler);
      this.safeAttachEvent(this.currentWindow,'mousedown',this.startDragHandler);
      this.safeAttachEvent(rootNode,'mousemove',this.mouseMoveHandler); 
      for (var i=0; i<this.activeElements.length; i++)
        this.safeAttachEvent(this.activeElements[i],'mousemove',this.mouseMoveHandler); 
  }
  dynamicWindow.prototype.unsetDraggable=function()
  {
     var rootNode = document.body.parentNode? document.body.parentNode: document.body;
     this.safeDetachEvent(rootNode,'mouseup',this.stopDragHandler);
     this.safeDetachEvent(this.currentWindow,'mousedown',this.startDragHandler);
     this.safeDetachEvent(rootNode,'mousemove',this.mouseMoveHandler);
     for (var i=0; i<this.activeElements.length; i++)
        this.safeDetachEvent(this.activeElements[i],'mousemove',this.mouseMoveHandler); 
  } 
  dynamicWindow.prototype.refreshEvents=function()
  {
      this.unsetDraggable();
      this.setDraggable();
  }
  
  dynamicWindow.prototype.destroy=function()
  {
     if (!this.currentWindow) return;
     this.unsetDraggable();
     this.currentWindow.parentNode.removeChild(this.currentWindow);
     this.currentWindow=null;
  }
  
    dynamicWindow.prototype.safeAttachEvent=function(obj, evt, func)
    {
    if (obj.addEventListener) {
        obj.addEventListener (evt, func, false);
        return true;
     } else if (obj.attachEvent) {
        return obj.attachEvent("on"+evt, func);
     }
    }
    
   dynamicWindow.prototype.safeDetachEvent=function(obj, evt, func)
   {
    if (obj.removeEventListener) {
        obj.removeEventListener (evt, func, false);
        return true;
    } else if (obj.detachEvent) {
        return obj.detachEvent("on"+evt, func);
    } 
  }
  
  dynamicWindow.prototype.removeSelection=function()
  {
      if (window.getSelection) { window.getSelection().removeAllRanges(); }
      else if (document.selection && document.selection.clear)
      document.selection.clear();
  }
  
  dynamicWindow.prototype.createDiv_Get=function(left,top,width,height)
  {
      var div=document.createElement('div');
      div.style.width=width+'px';
      div.style.height=height+'px';
      div.style.left=left+'px';
      div.style.top=top+'px';
      div.style.position="absolute";
      return div;
  }
  
  dynamicWindow.prototype.createDiv_Extend=function(div,div1,div2,div3)
  {
      div.appendChild(div1);
      div.appendChild(div2);
      div.appendChild(div3);
      return div;
  }

