// ===================================================================
// Anchor position.js
// 
//
// ===================================================================


/* 
AnchorPosition.js
Last modified: 2/18/02

DESCRIPTION: These functions find the position of an <A> tag in a document,
so other elements can be positioned relative to it.

COMPATABILITY: Works with Netscape 4.x, 6.x, IE 5.x on Windows. Some small
positioning errors - usually with Window positioning - occur on the 
Macintosh platform.

FUNCTIONS:
getAnchorPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor. Position is relative to the PAGE.

getAnchorWindowPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor, relative to the WHOLE SCREEN.

NOTES:

1) For popping up separate browser windows, use getAnchorWindowPosition. 
   Otherwise, use getAnchorPosition

2) Your anchor tag MUST contain both NAME and ID attributes which are the 
   same. For example:
   <A NAME="test" ID="test"> </A>

3) There must be at least a space between <A> </A> for IE5.5 to see the 
   anchor tag correctly. Do not do <A></A> with no space.
*/ 

//   getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {

	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;

  var win = getWindow();
  var doc = win.document;

  var o=doc.getElementById(anchorname);
  if(o && o.offsetLeft && o.offsetTop){
    x=o.offsetLeft; y=o.offsetTop;
  }

  coordinates.x=x;
	coordinates.y=y;
	
	return coordinates;
}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
	var coordinates=getAnchorPosition(anchorname);
	var x=0;
	var y=0;

	//AD July 20, 2004 added check to see if we are calling function from PopUp or TextFrame.
  var win = getWindow();
  var doc = win.document;

	if (doc.getElementById) {
		if (isNaN(win.screenX)) {
			x=coordinates.x-doc.body.scrollLeft+win.screenLeft;
			y=coordinates.y-doc.body.scrollTop+win.screenTop;
			}
		else {
			x=coordinates.x+win.screenX+(win.outerWidth-win.innerWidth)-win.pageXOffset;
			y=coordinates.y+win.screenY+(win.outerHeight-24-win.innerHeight)-win.pageYOffset;
			}
		}
	else if (doc.all) {
		x=coordinates.x-doc.body.scrollLeft+win.screenLeft;
		y=coordinates.y-doc.body.scrollTop+win.screenTop;
		}
	else if (doc.layers) {
		x=coordinates.x+win.screenX+(win.outerWidth-win.innerWidth)-win.pageXOffset;
		y=coordinates.y+win.screenY+(win.outerHeight-24-win.innerHeight)-win.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function AnchorPosition_getWindowOffsetLeft (el) {
	
	//AD July 20, 2004 added check to see if we are calling function from PopUp or TextFrame.
	var win = getWindow();
  var doc = win.document;

	return AnchorPosition_getPageOffsetLeft(el)-doc.body.scrollLeft;
}	

function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function AnchorPosition_getWindowOffsetTop (el) {
	
	//AD July 20, 2004 added check to see if we are calling function from PopUp or TextFrame.
	return AnchorPosition_getPageOffsetTop(el)-doc.body.scrollTop;
}

function getWindow(){
 var win = this;
 if (isPopUpWindow == false){
    win = window.parent.TextFrame;
  } else {
     for(x in top.frames["MapFrame"].listOfWindows){
        if(top.frames["MapFrame"].listOfWindows[x].name == "customizeLayerWin"){
           win = top.frames["MapFrame"].listOfWindows[x];
           break;
        }
     }
  }
  return win;
}
