

var dragActive = false;
var isIE=document.all;
var isNN=!document.all&&document.getElementById;
var highz = 0; //Highest z-level for positionable elements
//var topE,thisE,nowX,nowY,clickX,clickY,offsetx,offsety,drgtype;
var topE,nowX,nowY,clickX,clickY,offsetx,offsety,drgtype;

function mclick(e){

	//alert("mclick");
	
  //thisE=isIE ? event.srcElement : e.target;
  var thisE=isIE ? event.srcElement : e.target;
  
  //HCE=isIE ? document.all.hcentraldiv : document.getElementById("hcentraldiv");  
//alert("A thisE.tagName="+thisE.tagName+", thisE.id="+thisE.id);	  
  //Click to open, get position
  if(thisE.id.substr(0,7)=="openhc_"){
	//offsetx=isIE ? event.clientX : e.clientX;
    //offsety=isIE ? event.clientY : e.clientY;
	
	clickX=isIE ? event.x : window.pageXOffset+e.clientX;
    clickY=isIE ? event.y : window.pageYOffset+e.clientY;
	
	//var xoffs = 40;
	//var yoffs = 30;
	//offsetx=isIE ? event.x : window.pageXOffset+e.clientX;
    //offsety=isIE ? event.y : window.pageYOffset+e.clientY;

//alert("HCE.style.width="+HCE.style.width+", offsetx="+offsetx);

//var l = (offsetx-parseInt(HCE.style.width)-10)+"px";
//var t = (offsety-50)+"px";

//alert("l="+l+", t="+t);

	//HCE.style.left=(offsetx-parseInt(HCE.style.width)-xoffs)+"px";
    //HCE.style.top=(offsety-yoffs)+"px"; 
  }
  //Click on windowbar, activate drag
  else{
	  topE=isIE ? "BODY" : "HTML"; 
	  //while (thisE.id!="hctitletd"&&thisE.tagName!=topE){
		  
		//Kanske g�r att optimera h�r med kortare, smartare id
	  while (thisE.id.substr(0,6)!="hctit_" && thisE.id !="myhfctit" && thisE.id !="aftxttit" && thisE.tagName!=topE){

		thisE=isIE ? thisE.parentElement : thisE.parentNode;
		//alert("B thisE.tagName="+thisE.tagName+", thisE.id="+thisE.id);		
	  }  
	  if (thisE.id.substr(0,6)=="hctit_"){
		  
		drgtype = "hce";
		activeHC = thisE.id.substr(6);
		
		//alert("activeHC="+activeHC);
		
		//alt 1, spara bara activa hid globalt
		var HCE = document.getElementById("hctab_"+activeHC);
		 
		//alt 2, spara hela aktiva diven globalt
		//HCE = document.getElementById("hcdiv_"+activeHC);
		
		offsetx=isIE ? event.clientX : e.clientX;
		offsety=isIE ? event.clientY : e.clientY;
		nowX=parseInt(HCE.style.left);
		nowY=parseInt(HCE.style.top);
	//alert("nowX="+nowX+", nowY="+nowY+", HCE.style.left="+HCE.style.left+", HCE.style.top="+HCE.style.top);
		focusHC(activeHC);
		dragActive=true;
		document.onmousemove=dragE;
	  }
	else if (thisE.id == "myhfctit"){
		
		drgtype = "myhfc";
		
		var myhfc=document.getElementById("lh_myhfc");
		
		offsetx=isIE ? event.clientX : e.clientX;
		offsety=isIE ? event.clientY : e.clientY;
		nowX=parseInt(myhfc.style.left);
		nowY=parseInt(myhfc.style.top);
		
		//Focus myhfc
		highz = highz+1;
		myhfc.style.zIndex	= highz;
		
		dragActive=true;
		document.onmousemove=dragE;
	  }
	 else if (thisE.id == "aftxttit"){

		drgtype = "aftxt";
		
		var txttab = document.getElementById("addhfromtxttab");
		
		offsetx=isIE ? event.clientX : e.clientX;
		offsety=isIE ? event.clientY : e.clientY;
		nowX=parseInt(txttab.style.left);
		nowY=parseInt(txttab.style.top);
		
		//Focus layer
		highz = highz+1;
		txttab.style.zIndex	= highz;
		
		dragActive=true;
		document.onmousemove=dragE;
	  }
  }
}

function dragE(e){
  if (!dragActive) return;
 
	var elem;
	if(drgtype == "hce"){
		elem = document.getElementById("hctab_"+activeHC);
	}else if(drgtype == "myhfc"){
		elem = document.getElementById("lh_myhfc");
	}else if(drgtype == "aftxt"){
		elem = document.getElementById("addhfromtxttab");
	}

  
  elem.style.left=isIE ? (nowX+event.clientX-offsetx)+"px" : (nowX+e.clientX-offsetx)+"px"; 
  elem.style.top=isIE ? (nowY+event.clientY-offsety)+"px" : (nowY+e.clientY-offsety)+"px";
  return false;  
}


document.onmousedown=mclick;
document.onmouseup=Function("dragActive=false");


