

var dragObj = new Object();
var dragSect = new Object();
var currdragobjid = "";
var dragBrowserIE = false;
var dragStartX = 0;
var dragStartY = 0;
var dragEnough = false;

function dragStart(ev,id) {
  var el;
  var x, y;

  var oMenu = new fnGetObj("MyMWMENUSEL");
  oMenu.style.visibility = "hidden";

  dragBrowserIE = (navigator.appVersion.indexOf("MSIE")!=-1);

  currdragobjid = id;
  
  dragObj = new fnGetObj(id);
  dragObj.style.display = 'none';

  dragSect = new fnGetObj("dragSect");
  dragSect.obj.innerHTML = dragObj.obj.innerHTML;
  dragSect.style.visibility = "visible";
  dragStartX = 0;
  dragEnough= false;

  // Capture mousemove and mouseup events on the page.
  if (dragBrowserIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    ev.preventDefault();
  }
  dragGo(ev);
}


function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (dragBrowserIE) {
    x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
  } else {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }


  //adjust for section size
  x-=130;
  y+=5;

  if(dragStartX==0) {
	dragStartX = x;
	dragStartY = y;
  }	
 
  if((Math.abs(dragStartX-x)>10) || (Math.abs(dragStartY-y)>10)) dragEnough=true;


  // Move drag element by the same amount the cursor has moved.
  dragSect.style.left = x;
  dragSect.style.top  = y;
  dragPlaceSection(x,y,true);


  if (dragBrowserIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  } else {
    event.preventDefault();
  }
}


function dragStop(event) {


  var x = dragSect.style.left;
  var y = dragSect.style.top;
  if(dragEnough) {
  	dragPlaceSection(x,y,false);
  } else {
	var dragPreview = new fnGetObj("dragPreview");
	dragPreview.style.display= "none";
	dragObj.style.display = 'block';  	
  }

  // Stop capturing mousemove and mouseup events.
  dragSect.style.visibility = "hidden";
  

  if (dragBrowserIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  } else {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

function dragLoadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = dragReceiveLocChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = dragReceiveLocChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function dragSetLoc(SectionID,DropSectionID,Column,OnTop) {
	
	SectionID = "" + SectionID;
	DropSectionID = "" + DropSectionID;
	SectionID = SectionID.replace(/Item/, ""); 
	DropSectionID = DropSectionID.replace(/Item/, ""); 

	if (OnTop) {
		var strURL="m.w?lp=my&sp=m&slhhnf=t&a=ma&c="+SectionID+"&ds="+DropSectionID+"&i="+Column;

	} else {
		var strURL="m.w?lp=my&sp=m&slhhnf=t&a=mb&c="+SectionID+"&i="+Column;
	}
	
	dragLoadXMLDoc(strURL);
}

function dragReceiveLocChange() {
    
}

function dragPlaceSection(x,y,previewMode) {
	var Col2Left = "";
	var prevSectTop = 0;
	var prevSectID = "";
	var prevSection = null;
        var sLocation = "";
	var beforeSect = null;
	var iCol = 0;
	
	var dragPreview = new fnGetObj("dragPreview");

	// remove "px" and adjust for section size
	x = "" + x;
	x = x.replace(/px/, ""); 
	x = parseInt(x) + 130;
	y = "" + y;
	y = y.replace(/px/, ""); 
	y = parseInt(y) - 5;	

	var cCol = document.getElementById("myMWCOL2");
	var aPosition = fnFindPosition(cCol);
	Col2Left = "" + aPosition[0];
	Col2Left = Col2Left.replace(/px/, ""); 
	Col2Left = parseInt(Col2Left);

	//if to left of second column, we're over first column
	if (x < Col2Left) {
		var cObj = document.getElementById("myMWCOL1");
		iCol = 1;
	} else {
		var cObj = document.getElementById("myMWCOL2");
		iCol = 2;
	}

	//which section are we over
	if (cObj.childNodes.length > 0) {			
		for(var j=0; j<cObj.childNodes.length; j++){
	
			// skip over none drag sections
			if(cObj.childNodes[j].className!='myMWDragBox') continue;

			var aPosition = fnFindPosition(cObj.childNodes[j]);
			sLocation = "" + aPosition[1];
			sLocation = sLocation.replace(/px/, ""); 
			sLocation = parseInt(sLocation);
			if (!isNaN(sLocation)) {
				
				//check if over section
				if ((y > prevSectTop) && (y < ((prevSectTop + sLocation)/2))) {
					if(j==0) prevSection = cObj.childNodes[j];
					beforeSect = prevSection;					
				}else if ((y < sLocation) && (y >= ((prevSectTop + sLocation)/2))) {
					prevSection = cObj.childNodes[j];					
					beforeSect = prevSection;
				}

				//insert section
				if (beforeSect != null) {
					if (previewMode) {
						cObj.insertBefore(dragPreview.obj,beforeSect);
						dragPreview.style.display= "block";
					} else {
						cObj.insertBefore(dragObj.obj,beforeSect);					
						dragObj.style.width= "100%";
	  					dragObj.style.display= "block";
						dragPreview.style.display= "none";
						dragSetLoc(dragObj.obj.id,beforeSect.id,iCol,true)						
					}
					
					return;
				}
				prevSectTop = sLocation;
				prevSection = cObj.childNodes[j];
			}
		}
	}
	if (previewMode) {
		cObj.appendChild(dragPreview.obj);
		dragPreview.style.display= "block";
	} else {
		cObj.appendChild(dragObj.obj);
		dragObj.style.width= "100%";
	  	dragObj.style.display= "inline";
		dragPreview.style.display= "none";
		dragSetLoc(dragObj.obj.id,"0",iCol,false)		
	}
}
