//	getPos.js
//	Syzygy Ltd 2001
//	Finds absolute position of a link within a page flow.
//	Requires:	UserAgent 2.0
//	Language:	Javascript 1.2
//	Version:	1.1
//	$Author: tbrown $
//	$Date: 2002/07/15 13:55:15 $
//	$Revision: 1.1.1.1 $

/*
	Bugs:
	- does not recalculate on page resize
	- IE4.0 Win duplicating table padding / spacing / border offsets

*/


// returns coordinates as Pos object (properties: x, y), or null
function getLinkPos(sLinkHash, oDoc) {
	//console("new\n\n")
	if(typeof UA == "undefined") UA = new UserAgent();
	var d = oDoc || document;
	var pos = null;
	var oLink;
	for(var i=0; i<d.links.length; i++) {
		oLink = d.links[i];
		if(oLink.hash == sLinkHash) {
			if(UA.NS4) pos = new Pos(oLink.x, oLink.y);
			if(UA.NS6) {
				pos = new Pos(oLink.offsetLeft, oLink.offsetTop);
				pos.changeBy(d.body.offsetLeft, d.body.offsetTop);
			}
			if(UA.IE) {
				pos = getAbsPos(oLink);
				// arbitrary offsets
				pos.changeBy(1, 1);
				if(UA.Mac && UA.vers == "4.5") pos.changeBy(1, 2);
			}
			break;
		}
	}
	return pos;
}

// for IE, climb the document adding offsets
function getAbsPos(oEle) {
	var pos = new Pos();
	var myEl = oEle;
	while(myEl.parentElement != null) {
		//exception list
		if((myEl.tagName != "TR" && myEl.tagName != "TBODY") || UA.IE4) {
			if(myEl.tagName == "TABLE") {
				// may also need to check style.border property
				if(myEl.border == 0) pos.changeBy(-1, -1);
				else {
					if(UA.Mac) pos.changeBy(-1, -1);
				}
			}
			pos.changeBy(myEl.offsetLeft, myEl.offsetTop);
			//console(myEl.tagName +" "+ myEl.offsetLeft );
		}
		myEl = myEl.parentElement;
	}
	return pos;
}

// position object constructor
function Pos(x, y) {
	this.x = x || 0;
	this.y = y || 0;
}
// changeBy method
Pos.prototype.changeBy = function(x, y) {
	this.x += x;
	this.y += y;
}
Pos.prototype.toString = function() {return this.x + "," +this.y}


//--------------------------------------------------------//
// UserAgent constructor
// Version: 2.1 [17/02/2002]
function UserAgent() {
	this.agent = navigator.userAgent.toLowerCase();
	this.vers = parseFloat(navigator.appVersion);
	this.major = parseInt(navigator.appVersion);
	var a = this.agent;
	
	this.Mac = (a.indexOf("mac")!=-1);
	this.Win = (a.indexOf("win")!=-1);
	this.Aol = (a.indexOf("aol")!=-1);// does not work on Aol/IE3
	this.Nav = (
		(a.indexOf('mozilla')!=-1) 
		&& (a.indexOf('spoofer')==-1) 
		&& (a.indexOf('compatible') == -1)
		// these will detect opera / webtv, 
		//	commenting them out will include them as `Nav`
		// && (a.indexOf('opera')==-1)
		// && (a.indexOf('webtv')==-1)
	);
	this.MSIE = false;
		
	var msver = a.indexOf("msie");
	if (msver != -1) {
		msver = a.substring(msver+5,this.agent.length);
		this.MSIE = true;
		this.vers = parseFloat(msver);
		this.major = parseInt(msver);
	}
	
	// derived from useragent string
	this.NS4 = (this.Nav && this.major == 4);
	this.IE4 = (this.MSIE && this.major == 4);
	this.IE5 = (this.MSIE && this.major == 5);
	this.NS6 = (a.indexOf("netscape6") > -1);
	
	// derived from objects
	this.DOM1 = (document.getElementById)? true:false;
	this.layer = (document.layers)? true:false;
	this.all = (document.all)? true:false;
	
	//	the following are for kept historic reasons
	//	from original LayerObj implementation,
	this.NS = this.layer;
	this.IE = this.all;
}
// EOF