﻿
/*INIT JS */
if (window.Event)
    document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;
dragobj=null;
selectedList=null;
grabx=0;
graby=0;
elex=0;
eley=0;
orix=0;
oriy=0;
mousex=0
mousey=0;
/*INIT JS */
function getMouseXY(e)
{ 
  if (!e) e = window.event; 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { 
      mousex = e.pageX;
      mousey = e.pageY;
    }
    else if (e.clientX || e.clientY)
    { 
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
  }
}
    
function $(v){
    return document.getElementById(v);
}
            
/////////////////////////////////////////////////////////////// 
/////////////////   DRAG LIST  - DO NOT EDIT       //////////// 
///////////////////////////////////////////////////////////////   
function DragList(listObj){
    this.List = listObj;
    this.Items=this.List.getElementsByTagName("li");
    this.Length = this.List.getElementsByTagName("li").length;
    this.selectedItem=null;
    this.Heights =new Array();
    
    
    this.initHeights=function(){
        for(var i=0;i<this.Length;i++){
            this.Heights[i]=this.Items[i].offsetTop;       
        }
    }
    this.changeOnePlaceUp=function(liObj){
       var index=this.indexOf(liObj);
       if(index!=-1 && index<this.Length-1){
        oSwap=this.Items[index+1];
        this.swapNodes(liObj,oSwap);
        this.initHeights();
       }
    }
    this.changeOnePlaceDown=function(liObj){
       var index=this.indexOf(liObj);
       if(index!=-1 && index>0){
        oSwap=this.Items[index-1];
        this.swapNodes(liObj,oSwap);
        this.initHeights();
       }
    }
        
    this.indexOf=function(liObj){
        for(var i=0;i<this.Length;i++){
            if(this.Items[i]==liObj)
                return i;
        }
        return -1;
    }
    this.swapNodes=function(item1,item2) {
        if(item1==item2)
            return;
            
        var itemtmp = item1.cloneNode(1);
        var parent = item1.parentNode;
        item2 = parent.replaceChild(itemtmp,item2);
        parent.replaceChild(item2,item1);
        parent.replaceChild(item1,itemtmp);
        itemtmp = null;
    }
    
    this.CreateDargView=function(selObj){
        var itemtmp = document.createElement("li");
        with (itemtmp.style){
            border="dotted 1px navy";
            position="absolute";
            left= selObj.offsetLeft.toString()+"px";
            top= selObj.offsetTop.toString()+"px";                    
            
            height=selObj.offsetHeight;
            width=selObj.offsetWidth;
        }
      
        this.List.appendChild(itemtmp);
        return itemtmp;
    }
    
    this.setListOrderAjax =function(){
        var str="";
        for(i=0;i<this.Length;i++)
            str+=(i==0)?this.Items[i].id:","+this.Items[i].id;
        url = "/Ajax/Users_UpdateUserPageDragOrder.asp?ULName="+this.List.id+"&DragOrder="+str;
	    setObjXML()
	    objXML.open("GET", url ,false); 
	    objXML.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	    objXML.send(null);
	    objXML=null;
    }
    
    this.startDarg=function(selObj){
            this.initHeights();
            this.selectedItem = selObj
            selectedList=this;
            var shaddow = this.CreateDargView(selObj);
            dragobj= shaddow;
            document.onmousedown = falseFunc;
            dragobj.style.zIndex = 10; 
            orix = dragobj.offsetLeft;
            oriy = dragobj.offsetTop;
            grabx = mousex;
            graby = mousey;
           
            document.onmousemove = drag;  
            document.onmouseup=function(e){
                             if (dragobj){
                                dragobj.style.zIndex = 0;
                                dragobj.parentNode.removeChild(dragobj);
                                selectedList.setListOrderAjax();
                                dragobj = null;
                                selectedList=null;
                             }
                             //document.onmousemove = null;
                             document.onmouseup = null;
                             document.onmousedown = null;               
                          }
            
    }
    function drag(e){
          getMouseXY(e);
          if (dragobj)
          {
            elex = orix + (mousex-grabx);
            eley = oriy + (mousey-graby);
            dragobj.style.position = "absolute";
            //dragobj.style.left = (elex).toString(10) + 'px';
            dragobj.style.top  = (eley).toString(10) + 'px';
           
            var Sindex=selectedList.indexOf(selectedList.selectedItem);
            
            if (Sindex>0)
                if(selectedList.Heights[Sindex-1]>dragobj.offsetTop)
                    selectedList.changeOnePlaceDown(selectedList.selectedItem);  
       
            if (Sindex<selectedList.Length-1)
                if(selectedList.Heights[Sindex+1]<dragobj.offsetTop)
                    selectedList.changeOnePlaceUp(selectedList.selectedItem); 
           
          }
          
          return false; 
   }
}
         
  
 function falseFunc(){
    return false;
 }
        
 

 