/**
 * CROWN COPYRIGHT
 * Glen Collins, OESR, Queensland Treasury, Australia
 * www.oesr.qld.gov.au
 * Do not remove this note.
 * Modified extensively from CodeLifter.com source
 * 
**/


isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;
dradStillRunning=false;
var offsetx = 0;
var offsety = 0;
var nowX = 0;
var nowY = 0;
var panX1 = 0;
var panX2 = 0;
var panY1 = 0;
var panY2 = 0;
panStarted = 0;



function startDrag(e){
  whichDog= mapImageDrag;  
	//alert(e.target);
	//CRAP CRAP CRAP!
	//document.getElementById('mapPanel').style.left = "45px";
	//alert((document.getElementById('mapPanel').style.left));
	//alert(whichDog.id);
  /*while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog){
    hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
  } */
	if ((browserType == 1) || (browserType == 4) && (panStarted == 0)) {
    mapX = getOffsetLeft(mapImageRef);
		mapY = getOffsetTop(mapImageRef);
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
		if((event.x+scrollX)<mapX || (event.y+scrollY)<mapY || (event.x+scrollX)>(IMAGEW + mapX) 
			|| (event.y+scrollY)>(IMAGEH + mapY)) {
			DEBUGME.display3.value = "You didn't click over the map";
			return;
		}
		offsetx=isIE ? event.clientX : e.clientX;
    offsety=isIE ? event.clientY : e.clientY;
    nowX=parseInt(whichDog.style.left);
    nowY=parseInt(whichDog.style.top);
    dradStillRunning=true;
		//DEBUGME.display4.value = "offsetx= " +offsetx+ "  offsety= " +offsety+ "  nowX= " +nowX+ "  nowY= " +nowY;
		//DEBUGME.display5.value = "nowX= " +nowX+ "  event.clientX= " +event.clientX+ "  offsetx= " +offsetx;
		panX1 = offsetx;
		panY1 = offsety;
		DEBUGME.displaystartX.value = panX1;
		DEBUGME.displaystartY.value = panY1;
    document.onmousemove=runWithDrag;
	} else if ((browserType == 2) || (browserType == 3) && (panStarted == 0)) {
		mapX = getOffsetLeft(mapImageRef);
		mapY = getOffsetTop(mapImageRef);
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
		if((e.clientX+scrollX)<mapX || (e.clientY+scrollY)<mapY || (e.clientX+scrollX)>(IMAGEW + mapX) 
			|| (e.clientY+scrollY)>(IMAGEH + mapY)) {
			DEBUGME.display3.value = "You didn't click over the map";
			return;
		}
		offsetx=isIE ? event.clientX : e.clientX;
    offsety=isIE ? event.clientY : e.clientY;
    nowX=parseInt(whichDog.style.left);
    nowY=parseInt(whichDog.style.top);
    dradStillRunning=true;
		//DEBUGME.display4.value = "offsetx= " +offsetx+ "  offsety= " +offsety+ "  nowX= " +nowX+ "  nowY= " +nowY;
		//DEBUGME.display5.value = "nowX= " +nowX+ "  event.clientX= " +event.clientX+ "  offsetx= " +offsetx;
		panX1 = offsetx;
		panY1 = offsety;
		DEBUGME.displaystartX.value = panX1;
		DEBUGME.displaystartY.value = panY1;
		panStarted = 2;
    document.onmousemove=runWithDrag;
	}
}

function runWithDrag(e){
  if (!dradStillRunning) {
		return true;
	} else {
  	whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; 
  	whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
		//self.scrollTo(0,0);
		DEBUGME.displaymouseX.value	= isIE ? event.clientX : e.clientX;
		DEBUGME.displaymouseY.value	= isIE ? event.clientY : e.clientY;
		DEBUGME.display2.value = movecount;
		panX2 = isIE ? event.clientX : e.clientX;
		panY2 = isIE ? event.clientY : e.clientY;
		movecount++;
		panStarted = 1;
  	return false; 
	}
}

function stopDrag() {
	if (panStarted == 1) {
		dradStillRunning=false;
		panX = panX1 - panX2;
		panY = panY1 - panY2;
		panclickX = Math.round(panX + (IMAGEW/2));
		panclickY = Math.round(panY + (IMAGEH/2));
		DEBUGME.displayboxX1.value  = "PanX1 = " + panX1;
		DEBUGME.displayboxY1.value  = "PanY1 = " + panY1;
		DEBUGME.displayboxX2.value  = "PanX2 = " + panX2;
		DEBUGME.displayboxY2.value  = "PanY2 = " + panY2;
		newURL = REQUEST_HANDLER_SERVLET+ "?mapinfobean=recenter&appname=\n" + APP_NAME + "&mapimage.x=" + panclickX + "&mapimage.y=" + panclickY;
		//alert(newURL);
		if (submitaction){
			top.changecover('cover2a',true);
			top.changecover('cover3a',true);
			document.location.href = newURL;
		} else {
			DEBUGME.display3.value = newURL;
			//alert(newURL);
			resetInfo();
		}
		panStarted = 0;
	} else if (panStarted == 2) {
		dradStillRunning=false;
		alert('You must click down and drag the map and then release your mouse to pan to a new point.');
		panStarted = 0;
	} else {return}
		movecount = 0;
}

