/* 
// HISTORY
// ------------------------------------------------------------------
// May 16, 2005: Generalized from the previous AnchorPosition.js to 
//               get the position of any object in more moern browsers,
//               or fall back to finding an anchor for Netscape4
//               support.

Author: Matt Kruse
View documentation, examples, and source code at:
http://www.JavascriptToolbox.com/
http://mattkruse.com/javascript/ObjectPositionTest.php

DESCRIPTION: These functions find the position of an object in a document,
so other elements can be positioned relative to it. Since Netscape4.x 
lacked the ability to get the position of arbitrary objects, you can use
an anchor NAME (not ID) which will still work in modern browsers.

These functions do their best to account for scrollable areas and other
formatting which may exist in a page. However, with complext layouts or
functionality, more page-specific code may need to be written.

COMPATABILITY: Works with Netscape, IE, Safari, Opera, Firefox, etc. 
Some small positioning errors - usually with Window positioning - may 
occur on the Macintosh platform.

FUNCTIONS:
getObjectPosition(objectId[or anchor NAME])
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the object. Position is relative to the PAGE.

getObjectWindowPosition(objectId[or anchor NAME])
  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 getObjectWindowPosition. 
   Otherwise, use getObjectPosition

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.
*/ 

// A flag to decide whether or not to consider specific browser bugs/issues
var fixBrowserQuirks = true;

var ObjectPositionBody = null;
var ObjectPositionTraceString = "";

// A utility function to help trace actions, values, and calculations
function ObjectPositionTrace(str,padding) {
	if (typeof(padding)=="number") {
		for (var i=0; i<padding; i++) { ObjectPositionTraceString += " "; }
	}
	ObjectPositionTraceString += str + "\n";
}

// getObjectPosition(id)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the object or anchor, relative to the page.
function getObjectPosition(id) {
	ObjectPositionTraceString = ""; // initialize the trace string
	
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	coordinates.x=0; coordinates.y=0;

	// Get a ref to the body
	if (ObjectPositionBody==null) {
		if (document.body) {
			ObjectPositionBody = document.body;
		}
		else if (document.getElementsByTagName) {
			ObjectPositionBody =  document.getElementsByTagName('BODY').item(0);
		}
	}
	var o = null;
	if (document.getElementById && (o = document.getElementById(id))!=null) {
		ObjectPositionTrace("DOM has getElementById and ID ["+id+"] exists in document");
		ObjectPositionTrace("Finding position of tag: "+o.tagName);
		coordinates.x = ObjectPosition_getPageOffsetLeft(o);
		coordinates.y = ObjectPosition_getPageOffsetTop(o);
	}
	else if (document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x) {
		// Browser doesn't have getElementById or ID not found
		// Check for anchor with a NAME matching the given ID
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==id) { 
				coordinates.x = document.anchors[i].x;
				coordinates.y = document.anchors[i].y;
				return coordinates;
			}
		}
	}
	else if (documentall && (o=document.all[id])!=null) {
		// Try document.all to find object as a last resort
		coordinates.x = ObjectPosition_getPageOffsetLeft(o);
		coordinates.y = ObjectPosition_getPageOffsetTop(o);
		return coordinates;
	}
	// Couldn't find it anywhere. Return 0,0 so an error doesn't occur
	return coordinates;
}


// getObjectWindowPosition(id)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the object or anchor, relative to the window
function getObjectWindowPosition(id) {
	var coordinates=getObjectPosition(id);
	if (document.getElementById) {
		if (isNaN(window.screenX)) {
			coordinates.x = coordinates.x-document.body.scrollLeft+window.screenLeft;
			coordinates.y = coordinates.y-document.body.scrollTop+window.screenTop;
			}
		else {
			x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
			}
		}
	else if (document.all) {
		x=coordinates.x-document.body.scrollLeft+window.screenLeft;
		y=coordinates.y-document.body.scrollTop+window.screenTop;
		}
	else if (document.layers) {
		x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

function ObjectPosition_getPageOffsetLeft (el) {
	ObjectPositionTrace("");
	ObjectPositionTrace("Finding left position");
	ObjectPositionTrace("---------------------");
	return ObjectPosition_getPageOffsetTopLeft(el,"Left");
}
function ObjectPosition_getPageOffsetTop (el) {
	ObjectPositionTrace("");
	ObjectPositionTrace("Finding top position");
	ObjectPositionTrace("---------------------");
	return ObjectPosition_getPageOffsetTopLeft(el,"Top");
}
function ObjectPosition_getPageOffsetTopLeft(el,type) {
	if (el.offsetParent) {
		ObjectPositionTrace("Element's offsetParent is "+el.offsetParent.tagName);
	}
	if (el.parentNode) {
		ObjectPositionTrace("Element's parentNode is "+el.parentNode.tagName);
	}
	if (el.offsetParent && el.parentNode && el.offsetParent==ObjectPositionBody && el.parentNode!=ObjectPositionBody) {
		ObjectPositionTrace("offsetParent is the BODY, so we will walk up the parentNode chain");
		// Some browsers make offsetParent always equal to the body
		// We need to walk up the nodes to check for element scrolling
		var considerScrolls = true;
		if (fixBrowserQuirks && window.opera) { considerScrolls = false; } // Opera doesn't need to consider the scroll setting of elements, since it already does so in it's offsetLeft and offsetTop values
		var offset=el["offset"+type];
		ObjectPositionTrace("Offset from BODY="+offset);
		while((el=el.parentNode) != null) { 
			ObjectPositionTrace("parentNode="+el.tagName);
			if (el==ObjectPositionBody) { 
				// Some browsers (ie in stands-compliance mode) have offset from the BODY to the HTML (documentElement) tag, so we need to consider that
				if (typeof(el["offset"+type])=="number") {
					ObjectPositionTrace("BODY has an offset of "+el["offset"+type]+" so we'll consider it");
					offset += el["offset"+type];
				}
				// Stop considering scrolling, since we're at the body itself, and we want coordinates relative to the body
				considerScrolls = false;
			}
			if (considerScrolls && el!=ObjectPositionBody && el["scroll"+type]) {
				ObjectPositionTrace("Scroll amount of this object="+el["scroll"+type]);
				offset -= el["scroll"+type]; 
				ObjectPositionTrace("Total offset="+offset);
			}
		}
		ObjectPositionTrace("Total offset="+offset);
		return offset;
	}
	else {
		ObjectPositionTrace("Walking up the offsetParent chain");
		var offset=el["offset"+type];
		ObjectPositionTrace("Offset from parent ("+((el.offsetParent==null)?"null":el.offsetParent.tagName)+") ="+el["offset"+type]);
		var padding=0;
		var considerScrolls = true;
		if (fixBrowserQuirks && window.opera) { considerScrolls = false; } // Opera doesn't need to consider the scroll setting of elements, since it already does so in it's offsetLeft and offsetTop values
		while((el=el.offsetParent) != null) { 
			padding+=2;
			offset += el["offset"+type]; 
			ObjectPositionTrace(el.tagName+"'s Offset from parent ("+((el.offsetParent==null)?"null":el.offsetParent.tagName)+") ="+el["offset"+type],padding);
			ObjectPositionTrace("Total offset="+offset,padding);
			if (considerScrolls && el!=ObjectPositionBody && el["scroll"+type]) { // Don't count the body scroll!
				ObjectPositionTrace("Scroll amount of this "+el.tagName+"="+el["scroll"+type],padding);
				offset -= el["scroll"+type]; 
				ObjectPositionTrace("Total offset="+offset,padding);
			}
		}
		return offset;
	}
}

function box() {
	return document.getElementById('box');
}
function kruseposition() {
	var coordinates;
	if (arguments.length==1) { 
		coordinates = getObjectPosition(arguments[0]);
	}
	else {
		coordinates = getObjectPosition(arguments[0],arguments[1]);
	}
//alert(coordinates.x+","+coordinates.y);
	//box().style.left = coordinates.x+"px";
	//box().style.top  = coordinates.y+"px";
	//document.forms[0].debugview.value = ObjectPositionTraceString;

	additionalOffset=0;
	
	if (mac && ie5) 
		{
		bmt = eval(document.body.currentStyle.marginTop.replace("px", ""));
		bpt = eval(document.body.currentStyle.paddingTop.replace("px", ""));
		bml = eval(document.body.currentStyle.marginLeft.replace("px", ""));
		bpl = eval(document.body.currentStyle.paddingLeft.replace("px", ""));
		//additionalOffset = (which=="x")  ? (bml + bpl) : (bmt + bpt);
		coordinates.x += bml + bpl;
		coordinates.y += bmt + bpt;
		}
		
	return coordinates;
}