// Base.js
var popUpParent;
function celloDocInit() {
	if (window == window.parent) {
		// this is not being displayed in the frameset, fix it up
		window.location.href = relPath + "index.htm?doc=" + window.location.href;
		return;
	}
	// display the heading of this document in the title bar frame of this frameset
    if ((window.parent) && (window.parent.frames) && ((window.parent.frames).length > 0)) {
        var el = window.parent.frames[0].document.getElementById("heading");
        if (el) {
            el.innerHTML = document.title;
            window.parent.frames[0].document.title = document.title;
        }
    }
    if (window.registerPopUpEventHandler) {
    	// this file might have popups in it
    	registerPopUpEventHandler();
    }
    // if called from a popup, show my iFRame and scroll to the correct document fragment
    detectPopUp();
    if (popUpParent) {
	    showPopUpIFrame();
    }
    if (detectHighlight()) {
		highlight();
    }
}
function detectHighlight() {
    if (highlight) {
		var qs;
		if ((qs = window.location.search) && qs.indexOf("?zoom_highlight=") == 0 && qs.length > 16) {
			return true;
		}
    }
    return false;
}
function detectPopUp() {
	popUpParent = window.parent;
    if (!(popUpParent && popUpParent.popUpIFrame)) {
    	popUpParent = undefined;
    }
}
function showPopUpIFrame() {
    if (popUpParent.fragment) {
    	// we're supposed to go to an anchor inside the document
		window.scrollTo(0, popUpParent.getPageOffsetTop(document.getElementById(popUpParent.fragment)));
    }
	// hide the loading indicator and show the iFrame
	popUpParent.loadingDiv.style.visibility = "hidden";
	popUpParent.loadingDiv.style.position = "absolute";
	popUpParent.popUpIFrame.style.visibility = "inherit";
	var lnks = document.links;
	var lnk;
	for(var i = 0; i < lnks.length; i++) {
		// register an event handler to process links from the popup
		lnk = lnks[i];
		if (!lnk.target) {
			registerPopupAnchorEventListener(popUpParent, lnk);
		}
	}
}
function registerPopupAnchorEventListener(popUpParent, link) {
    if (link.addEventListener) {
        link.addEventListener("click", popUpLinkClicked, false);
    } else {
        if (link.attachEvent) {
            link.attachEvent("onclick", popUpLinkClicked);
        }
    }
}
function popUpLinkClicked(event) {
	var anchor;
	if (!event) {
		event = window.event;
	}
	anchor = event.srcElement;
	if (!anchor) {
		anchor = event.target;
	}
	while (anchor.nodeName.toLowerCase() != "a") {
		anchor = anchor.parentNode;
	}
	if (window.popUpClass && popUpClass(anchor.className)) {
		// this code will be much better if popups are made objects - this is a work around until I re-write it
	    var hash = anchor.href.indexOf("#");
	    if (hash > 0) {
		    popUpParent.fragment = anchor.href.substring(hash + 1);
		    popUpParent.popUpIFrame.src = anchor.href.substring(0, hash);
	    } else {
		    popUpParent.fragment = undefined;
		    popUpParent.popUpIFrame.src = anchor.href;
	    }		
	} else {
		popUpParent.hidePopUp();
		popUpParent.location.href = anchor.href;
	}
	if (event.preventDefault) {
		event.preventDefault();
	} else {
		event.returnValue = false;
	}	
}
// once loading of the document is complete, initialise the cello document functionality:
window.onload = function() {
	celloDocInit();
};


