docRoot = document.location.href.indexOf("index.htm") >= 0 ? "." : "..";
if( document.location.href.indexOf("html/progs/") >= 0 ) docRoot = "../..";
InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
Netscape = !InternetExplorer;
Windows = navigator.appVersion.indexOf("Win") != -1;
MacOS = !Windows;
Safari = false;
if(InternetExplorer) {
	var str = navigator.userAgent
	Version = str.split("MSIE ")[1];
	Version = Version.split(".")[0];
} else {
	if( navigator.appVersion.indexOf("Safari") != -1 ) {
		Safari = true;
		Version = "6";
	} else if( navigator.vendorSub != null ) {
		Version = navigator.vendorSub.split(".")[0];
	} else {
		Version = navigator.appVersion.split(".")[0];
	}
}
var extraStyles;
if(Safari) {
	extraStyles = "safari.css";
} else if(Windows && InternetExplorer) {
	extraStyles = "explorer_win.css";
} else if(MacOS && InternetExplorer) {
	extraStyles = "explorer_mac.css";
} else if(MacOS && Netscape) {
	extraStyles = "netscape_mac.css";
} else {
	extraStyles = "netscape_win.css";
}
if(extraStyles) {
	document.write('<link href="',docRoot,'/css/',extraStyles,'" rel="stylesheet" type="text/css">');
}

function initPage(elem) {
	window.onresize = function() {resizeAll()};
	setLinks();
}

function initReactions() {
	var reactionsLink = getElement("reactions");
	var filename = getFileName();
	var progId = filename.split("-")[1];
	reactionsLink.href += "#"+progId;
}
	
function resizeAll(forPrint) {
	var container = getElement("container");
	var content = getElement("content");
	var copyright = getElement("copyright");
	var homeMain = getElement("homeMain");
	container.style.left = getContainerX()+"px";
	if( content != undefined ) {
		content.style.height = getContentHeight()+"px";
		content.style.width = getContentWidth()+"px";
	}
	if( copyright != undefined ) {
		homeMain.style.height = getHomeMainHeight()+"px";
		copyright.style.left = getContainerX()+"px";
	}
}

function setInitialPos() {
	document.write('<style>#container {left:'+getContainerX()+'px}');
	document.write('#content {height:'+getContentHeight()+'px; width:'+getContentWidth()+'px}</style>');
}

containerWidth = 700;
contentTop = 180;
contentX = 147;
copyrightH = 18;
minWidth = 20;
netscapeAdjustX = 31;
netscapeAdjustY = 21;

function getContainerX() {
	var winWidth = document.all?document.body.clientWidth:window.innerWidth;
	var containerX = Math.max(Math.round((winWidth-containerWidth)/2), 10);
	return containerX;
}

// used on home page only
function getHomeMainHeight() {
	var winH = document.all?document.body.clientHeight:window.innerHeight;
	return Math.round(winH-copyrightH);
}

function getContentHeight() {
	var winH = document.all?document.body.clientHeight:window.innerHeight;
	return Math.round(winH-contentTop-(Netscape ? netscapeAdjustY : 0));
}

function getContentWidth() {
	var winW = document.all?document.body.clientWidth:window.innerWidth;
	return Math.max(Math.round(winW-getContainerX()-contentX-(Netscape ? netscapeAdjustX : 0)), minWidth);
}

function setLinks() {

// Find out what the current main section is from the pa
	var filename = getFileName();
	var section = filename.split("-")[0];
	if( section == "" ) section = "index";
	var mainLink = getElement("main_"+section);
	if( mainLink ) mainLink.innerHTML = '<span class="activelink">'+mainLink.innerHTML+'</span>';
}

function getFileName() {
	var url = document.location.href.split("?")[0];
	var filename = url.substr(url.lastIndexOf("/")+1);
	filename = filename.split(".")[0];
	return filename;
}

function goBack() {
	history.back();
}

// This may not be needed - remove if so
function getArgs(someUrl) {
	var myArgs = new Object();
	var myPos = someUrl.indexOf("?");
	var myStr = someUrl.substr(myPos + 1);
	if( myStr.indexOf("=") != -1 ) {
		var argPairs = myStr.split("&");
		for( var a = 0; a < argPairs.length; a++ ) {
			var part = argPairs[a].split("=");
			var val = part[1];
			myArgs[part[0]] = val == "" || val == null || val == undefined ? "" : unescape(val);
		}
	}
	return myArgs;
}

function initRolloverBut(linkId, imgId, overImg) {
	linkObj = getElement(linkId);
	linkObj.imgObj = getImage(imgId);
	linkObj.outObj = new Image();
	linkObj.outObj.src = linkObj.imgObj.src;
	linkObj.overObj = preloadImage(overImg);
	linkObj.onmouseover = function() {
		this.imgObj.src = this.overObj.src;
	}
	linkObj.onmouseout = function() {
		this.imgObj.src = this.outObj.src;
	}
}

function getElement(id) {
	var d = document;
	var e;
	if (d.getElementById) {
		e = d.getElementById(id);
	} else if (d.all) {
		e = d.all[id];
	} else if (d.layers) {
		e = findLayer(d, id);
	}
	return e;
}

function getImage(id) {
	return document.images[id];
}

function findLayer(d, id) {
	var e = d.layers[id];
	if (!e) {
		for (var n in d.layers) {
			if (d.layers[n].document) {
				e = findLayer(d.layers[n].document, id);
			}
		}
	}
	return e;
}

function setTopLeft(e, t, l) {
	if (e.style) {
		if (t) e.style.top = t + "px";
		if (l) e.style.left = l + "px";
	} else {
		if (t) e.top = t;
		if (l) e.left = l;
	}
}
function setDepth(e, d) {
	if (e.style) {
		e.style.zIndex = d;
	} else {
		e.zIndex = d;
	}
}
function setVisible(e, v) {
	if (e.style) {
		e.style.visibility = v ? "visible" : "hidden";
	} else {
		e.visibility = v ? "visible" : "hidden";
	}
}

function setClip(e, l, t, r, b) {
	if (e.style) {
		l = l ? l+"px" : "auto";
		t = t ? t+"px" : "auto";
		r = r ? r+"px" : "auto";
		b = b ? b+"px" : "auto";
		e.style.clip = "rect("+t+" "+r+" "+b+" "+l+")";
	} else {
		if (l) e.clip.left = l;
		if (t) e.clip.top = t;
		if (r) e.clip.right = r;
		if (b) e.clip.bottom = b;
	}
}
function setBgColor(e, c) {
	if (e.style) {
		e.style.backgroundColor = c;
	} else {
		e.bgColor = c;
	}
}
function preloadImage(srcUrl) { 
   var imageObj = new Image();
   imageObj.src = srcUrl;
   return imageObj;
}

