var isIE;
var debuglinks;

debuglinks = !true;
debuglinks = location.protocol == "file:";


init_website();

// ---------------------------------------------------------
// preload initialization....
// ---------------------------------------------------------
function init_website() {
	// for local offline testing
	if(location.protocol == "file:") {
		var tmp = location.href.substring(location.
			protocol.length + 3).split("websites/", 2);
		location.vroot = tmp[0] + "websites/" + 
		(location.vhost = tmp[1].split("/",1)[0]);
	};
	
	PseudoLink(location);
	
	// -----------------------------------------------------
	// insert custom stylesheet for MSIE if detected
	// -----------------------------------------------------
	if(!!(isIE = navigator.appName.indexOf("Microsoft") > -1)) {
		document.writeln('<link href="css/ie.css" ' + 
			'rel="stylesheet" type="text/css" />');
	
		/*
		The body width needs to be set based on this formula:
		siteWidth - (paddingLeft + paddingRight) * fontSize;
		This can be handled by the IE stylesheet as long as
		the body padding stays constant.  it can be done by
		script, but it's much easier to set the width based
		on the body.class.  
		
		satstream.us home page needs more room (less padding).
		index.html uses <body class="wide">.  problem solved.
		*/
		
		/*
		// possible script to fix width dynamically (bad)
		
		var style = document.body.currentStyle;
		var header = document.getElementById("header");
		var width = parseInt(header && header.currentStyle && 
			header.currentStyle.width || 770);
		document.body.style.width = (width - 
			(parseInt(style.paddingLeft) +
			parseInt(style.paddingRight)) * 
			parseInt(style.fontSize)) + "px";
		*/
	}
}

// ---------------------------------------------------------
// cross-browser: install multiple event handlers in object
// ---------------------------------------------------------
// function redefines itself the first time it's called
// ---------------------------------------------------------
function addListener(obj, type, fn, capture) {
	return (window.addListener = window.addEventListener
	? function addListener(obj, type, fn, capture) {
		if(typeof obj == "string") capture = fn, 
			fn = type, type = obj, obj = window;
		return obj.addEventListener(type, fn, capture);
	}
	: function addListener(obj, type, fn, capture) {
		if(typeof obj == "string") capture = fn, 
			fn = type, type = obj, obj = window;
		
		if(!(arr = obj[type + "Array"])) {
			var arr = obj[type + "Array"] = [];
		
			// preserve original event handler if present
			if(obj["on" + type]) {
				arr[arr.length++] = obj["on" + type];
			}
		
			// now add the queue-based event handler
			obj["on" + type] = function onevent(e) {
				for(var i = 0, result; i < arr.length; i++) {
					result = arr[i](e);
				}
			
				return result;
			}
		}
		
		// add handler to end of queue
		arr[arr.length++] = fn;
	})(obj, type, fn, capture);
}

// ---------------------------------------------------------
// cross-browser: remove event handler from object
// ---------------------------------------------------------
// function redefines itself the first time it's called
// ---------------------------------------------------------
function removeListener(obj, type, fn, capture) {
	return (window.removeListener = window.removeEventListener
	? function removeListener(obj, type, fn, capture) {
		if(typeof obj == "string") capture = fn, 
			fn = type, type = obj, obj = window;
		return obj.removeEventListener(type, fn, capture);
	}
	: function removeListener(obj, type, fn, capture) {
		if(typeof obj == "string") capture = fn, 
			fn = type, type = obj, obj = window;
		
		var arr = obj[type + "Array"] = obj[type + "Array"] || [];
		for(var i = 0; i < arr.length; i++) {
			if(arr[i] == fn) {
				arr.splice(i, 1);
				break;
			}
		}
	})(obj, type, fn, capture);
}

// ---------------------------------------------------------
// install global setStatus() and clearStatus()
// closures with privately scoped variables
// ---------------------------------------------------------
(function() {
	var tid;
	var dms = 400;
	
	// ---------------------------------------------------------
	// set status bar message, return true for mouseover handler
	// NOTE: link.text no good in IE, use link.innerHTML instead
	// ---------------------------------------------------------
	function setStatus(msg, def) {
		if(tid) tid = clearTimeout(tid);
		var str = (msg && (msg.title || msg.text 
			|| msg.innerHTML)) || msg || "";
		if(msg && msg.external) {
			str += (str ? " " : "") + "[External Website]" + 
				(msg.domain ? " [" + msg.domain + "]" : "");
		}
		
		if(window.debuglinks && msg && str && msg.index != null) {
			str = "[" + msg.index + "] " + 
				str + " " + (msg.href || "");
		}
		
		if(def) window.defaultStatus = str;
		window.status = str;
		return true;
	}
	
	// ---------------------------------------------------------
	// clear status bar message after delay (0 to clear now)
	// ---------------------------------------------------------
	function clearStatus(ms, def) {
		if(ms < 0) ms = dms = -ms;
		else if(ms === 0) {
			window.status = "";
			tid = clearTimeout(tid);
			if(def) window.defaultStatus = "";
		} else {
			ms = ms || dms;
			tid = setTimeout((def 
				? "window.defaultStatus = " : "")
				+ "window.status = '';", ms);
		}
		return true;
	}

	window.setStatus = window.setStatus || setStatus;
	window.clearStatus = window.clearStatus || clearStatus;
})();

//----------------------------------------------------------
// Call as constructor or as a function, init if arg passed
// constructor: get new link object, init if passed url/obj
// function: inits passed object or returns new PseudoLink
//----------------------------------------------------------
// function replaced with object constructor on initial call
// ---------------------------------------------------------
function PseudoLink(url) {
	function PseudoLink(url) {
		if(this.constructor == arguments.callee) {
			(obj = this).url = String(url || "");
		} else {
			var obj = url;
		}
		
		if(!(obj == null || typeof obj == "undefined")) {
			arguments.callee.init(obj);
		}
		
		return obj;
	}
	
	var fn = PseudoLink;
	var proto = fn.prototype;
	proto.constructor = fn;
	proto.url = "";
	
	proto.toString = 
	function toString() {
		return this.url;
	}
	
	// ---------------------------------------------------------
	// add custom properties to link object: 
	// protocol, domain, path, dir, file
	// NOTE: setting protocol or pathname of location loads url
	// ---------------------------------------------------------
	fn.init = function init(link) {
		if(!link) link = location;
		else if(typeof link != "object") {
			return new PseudoLink(link);
		}

		var url = String(link) || location.href || "";
	//	link.url = link.url || url;
		if(!link.protocol) link.protocol = url
			.substring(0, url.indexOf(":") + 1);
		url = url.substring(link.protocol.length);

		link.path = (url = url.substring(
			(p = url.indexOf("///") + 1) ? p + 2
			: (p = url.indexOf("//") + 1) ? p + 1 : 0))
			.split("#", 1)[0].split("?", 1)[0];

		if(link.protocol == "file:" && location.vroot) {
			link.path = link.path.split(location
				.vroot, 2)[1] || link.path;
		}
		
		if(!(link.pathname || link.pathname ===""))
			link.pathname = url.substring(url.indexOf("/"));
		
		var host = link.domain = link
			.hostname || (url.indexOf("/") > -1
			? url.substring(0, url.indexOf("/")) 
			: url.indexOf("@") ? url.substring(url.
			indexOf("@") + 1) : url).split(":", 1)[0];
		
		if(host == link.protocol)
			host = link.domain = "";
		
		link.roothost = link.roothost ||
			(p = host.indexOf(".")) != host.lastIndexOf(".")
			? host.substring(p + 1) : host;
		
		// NOTE: IE defines r/o "dir" property for links (argh!)
		
		link.file = url.substring((p = url.lastIndexOf("/")) + 1);
		link.fdir = url.substring(url.indexOf("/"), p);
		
		return link;
	}

	window.PseudoLink = PseudoLink;
	
	return this == window ? PseudoLink(url) : new PseudoLink(url);
}

// ---------------------------------------------------------
// extract filename from url w/o anchor or query
// ---------------------------------------------------------
function getfilename(link) {
	var url = String(link = link || location);

	return link.file || (link.file = 
		(url = link.path || (link.path = 
		(url = url || (link = location).href)
		? url.split("#", 1)[0].split("?", 1)[0] : ""))
		.substring(url.lastIndexOf("/") + 1));
}

/*
// ---------------------------------------------------------
// extract absolute directory from url w/o anchor or query
// ---------------------------------------------------------
function getfiledir(link) {
	var url = String(link = link || location);
	return link.fdir || (link.fdir = (url = link.path || (link
		.path = (url = (url || (link = location).href)).substring((
		(url.indexOf("///")+ 1) || (url.indexOf("//"))) + 2)))
		.substring(url.indexOf("/"), url.lastIndexOf("/")));
}
*/

// ---------------------------------------------------------
// set element class name if not already set or if priority
// modify class name if type being added is multi-value type
// ---------------------------------------------------------
function setClass(obj, tname, priority) {
	var cname = (obj = obj || {}).className;
	var wasIt = cname == "disabled" || tname == "external";
	var multi = tname == "disabled" || tname == "external" || wasIt;
	var hasIt = multi && cname.indexOf(tname) != -1;
	if(!cname) {
		obj.className = tname;
	} else if(multi && !hasIt) {
		obj.className = multi && wasIt 
			? cname + "-" + tname
			: tname + " -" + cname;
	} else if(priority) {
		obj.className = tname;
	}
	
	return obj.className;
}

// ---------------------------------------------------------
// add default handlers for unhandled links
// ---------------------------------------------------------
function init_links(e) {
	function setStatus(msg, def) {
		return (window.setStatus ? setStatus = window
			.setStatus : function() {})(msg, def);
	}
	
	function clearStatus(ms, def) {
		return (window.clearStatus ? clearStatus = window
			.clearStatus : function() {})(ms, def);
	}
	
	function onmouseover(e) {
		return setStatus(this);
	}
	
	function onmouseout(e) {
		return clearStatus();
	}
	
	function onclick(e) {
		return !this.disabled;
	}

	var host = location.roothost;
	var vhost = location.vhost;
	
	var links = document.links;
	
	for(var i = 0, a, s; i < links.length; i++) {
		a = links[i];
		a.index = i;
		PseudoLink(a);
		
		if((a.extn = s = a.file.substring(a.file
			.lastIndexOf(".") + 1)) == "pdf") {
			setClass(a, "pdf");
		}
		
		if(a.file.charAt(0) == "-") {
			setClass(a, "disabled");
		}
		
		a.disabled = a.disabled || a.file == "#" ||
			a.href == location.href + "#" ||
			a.className.indexOf("disabled") != -1;

		if(a.protocol == "mailto:") {
			setClass(a, "mailto");
			a.mailto = s = a.href.substring(7);
		}
		
		if(!!(a.external = a.external || 
		//	((s = a.rootdomain)
			((s = a.roothost)
			!= host && s != vhost))) {
			setClass(a, "external");
		}
		
		if(!a.title && a.text) {
			a.title = a.text
				.split("\n").join("")
				.split("\t").join("");
		}
		
		a.onmouseover = a.onmouseover || onmouseover;
		a.onmouseout = a.onmouseout || onmouseout;
		a.onclick = a.onclick || onclick;
	}
	
	init_anchors(e);
}

// ---------------------------------------------------------
// add node and index properties to anchor objects
// node: top unordered list container, indent when "active"
// ---------------------------------------------------------
// implemented for satscan.us projects.html and service.html
// ---------------------------------------------------------
function init_anchors(e) {
	var anchors = document.anchors;
	for(var i = 0, a; i < anchors.length; i++) {
		a = anchors[i];
		a.index = a.index || i;
		if((a = a.parentNode) && (a = a.parentNode) && 
			(a = a.parentNode) && a.tagName == "UL") {
			anchors[i].node = a;
		}
	}
	
	hilite_anchor();
}

// ---------------------------------------------------------
// if a specific named anchor was specified in url requested
// highlight it by setting its className to "active", clear
// className of other anchors if already set to "active"
// ---------------------------------------------------------
// implemented for satscan.us projects.html and service.html
// ---------------------------------------------------------
function hilite_anchor(link) {
	var anchors = document.anchors;
	var hash = (link = link || location).hash && link.hash.substring(1);
	if(hash) for(var i = 0, a; i < anchors.length; i++) {
		a = anchors[i];
		if(a.name == hash) {
			a.className = a.className || "active";
			if(a.node) a.node.className = 
				a.node.className || "active";
		} else if(a.className == "active") {
			a.className = "";
			if(a.node && a.node.className == "active") {
				a.node.className = "";
			}
		}
	}
}

// window.onload = onloaded;

addListener(window, "load", init_links);