var curImg = null;
var lastLink = null;
var curH = 0;
var curW = 0;

function clickImg(link, width, height) {
    var txt = document.getElementById("imgtxt");

    if (curImg != null) {
	curImg.style.position = "static";
	curImg.style.width = curW;
	curImg.style.height = curH;
	lastLink.parentNode.style.zIndex = 4;
	txt.style.visibility = "hidden";
    } 

    if (link == lastLink) {
	lastLink = null;
	curImg = null;
	return false;
    }

    lastLink = link;
    lastLink.parentNode.style.zIndex = 5;

    curImg = link.getElementsByTagName("img").item(0);
    curW = curImg.style.width;
    curH = curImg.style.height;
    
    var dp = new docCoord(link.parentNode);
    var vp = new viewPort();

    curImg.style.width = width;
    curImg.style.height = height;
    curImg.style.position = "absolute";
    curImg.style.right = "0px";
    curImg.style.top = "0px";
 
    // Bump image coords up if its off the bottom of the screen
    var np = new docCoord(curImg);
    var top = vp.y + vp.height - np.height - np.y;
    // If top is negative, it indicate how far we extend off the
    // bottom of the viewport
    if (top < 0) curImg.style.top = top + "px";

    var ip = new docCoord(curImg);
    txt.style.top = (ip.y - txt.offsetHeight) + "px";
    txt.style.left = ip.x + "px";
    txt.style.visibility = "visible";

    return false;
}

var openTangent;

function viewPort() {
    if (document.documentElement 
	&& document.documentElement.scrollTop) {
	this.x = document.documentElement.scrollLeft;
	this.y = document.documentElement.scrollTop;
	this.width = document.documentElement.clientWidth;
	this.height = document.documentElement.clientHeight;
    } else {
	this.x = document.body.scrollLeft;
	this.y = document.body.scrollTop;
	this.width = document.body.clientWidth;
	this.height = document.body.clientHeight;
    } 
}

function docCoord(node) {
    this.x = 0;
    this.y = 0;
    this.height = node.offsetHeight;
    this.width = node.offsetWidth;

    while(node.offsetParent != null) {
	this.x += node.offsetLeft;
	this.y += node.offsetTop;
	node = node.offsetParent;
    }
}

function doTangent(node, src) {
    if (openTangent) openTangent.focus();
    openTangent = window.open(src, 
			      "tangent", 
			      "width=700,height=500,scrollbars,resizable"); 
//     var pf = frames["tangent_content"];
//     var pelm = document.getElementById("tangent");
//     openTangent = pelm;

//     var vp = new viewPort();
//     pelm.style.top = (vp.y + (vp.height * 0.1)) + "px";
//     var pcl = document.getElementById("pagecloak");

//     pcl.style.visibility = "visible";
//     pelm.style.visibility = "visible";

//     pf.location.href = src;
    return false;
}

function closeTangent() {
    if (openTangent == null) return;
    openTangent.style.visibility="hidden";
    openTangent = null;
    var pcl = document.getElementById("pagecloak");
    pcl.style.visibility="hidden";
}

function prepareCloak() {
    var vp = new viewPort();
    var pcl = document.getElementById("pagecloak");
    var bodyElm = document.getElementsByTagName("BODY").item(0);
    var pelm = document.getElementById("tangent");

//     alert("vp = (" + vp.width + ", " + vp.height 
// 	  + ") bodyElm = (" 
// 	  + bodyElm.offsetWidth + ", "  + bodyElm.offsetHeight
// 	  + ")");

    pelm.style.left = "20px";
    var tHeight = (vp.height * 0.8);
    pelm.style.height = tHeight + "px";
    //pelm.getElementsByTagName("IFRAME").item(0).height = (tHeight - 20);
    pcl.style.height = bodyElm.offsetHeight + "px";
    pcl.style.width = Math.max(vp.width,bodyElm.offsetWidth) + "px";
}

