
// Global var for whether site is live, or dev
var isLive = (location.toString().search(/https?:\/\/(?:www\.)?oscar\.virginia\.edu/i) != -1);

// Keeps track of events registered with a particular object
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

// Add multiple events to a listener, using the EventCache object
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

//DOM-ready watcher; documentation at http://www.brothercake.com/site/resources/scripts/domready/
function domFunction(f, a)
{
	//initialise the counter
	var n = 0;
	
	//start the timer
	var t = setInterval(function()
	{
		//continue flag indicates whether to continue to the next iteration
		//assume that we are going unless specified otherwise
		var c = true;

		//increase the counter
		n++;
	
		//if DOM methods are supported, and the body element exists
		//(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
		//in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
		{
			//set the continue flag to false
			//because other things being equal, we're not going to continue
			c = false;

			//but ... if the arguments object is there
			if(typeof a == 'object')
			{
				//iterate through the object
				for(var i in a)
				{
					//if its value is "id" and the element with the given ID doesn't exist 
					//or its value is "tag" and the specified collection has no members
					if
					(
						(a[i] == 'id' && document.getElementById(i) == null)
						||
						(a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
					) 
					{ 
						//set the continue flag back to true
						//because a specific element or collection doesn't exist
						c = true; 

						//no need to finish this loop
						break; 
					}
				}
			}

			//if we're not continuing
			//we can call the argument function and clear the timer
			if(!c) { f(); clearInterval(t); }
		}
		
		//if the timer has reached 60 (so timeout after 15 seconds)
		//in practise, I've never seen this take longer than 7 iterations [in kde 3 
		//in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time]
		if(n >= 60)
		{
			//clear the timer
			clearInterval(t);
		}
		
	}, 250);
};

function getObj(id)
{
	var node;
	if (document.getElementById)
		node = document.getElementById(id);
	else if (document.all)
		node = document.all[id];
		
	return node;
}

function TellFriend(ref)
{ 
	var str="toolbar=no,status=no,menubar=no,location=no,scrollbars=yes,resizable=yes,height=320,width=500";
	tellaFriend = window.open(ref,"TellObj",str);
    tellaFriend.opener = top;
}

addEvent(window, "load", function()
{
	/* //Old deprecated code
	getObj("textfieldInput").onfocus = function() 
	{
		if (this.value == "search") 
			this.value = ""; 
	}
	getObj("searchButton").onclick = function()
	{
		return(redirectSearch(this.form));
	}*/
});

function updateStats()
{
	if (isLive)
	{
		// nextStat
		document.write('<script type="text/javascript" language="JavaScript" src="https://dce.nextstat.com/logging_js.php?ac=498"><\/script>');
		
		// StatCounter
		/*sc_project=422529;
		sc_partition=2;
		document.write('<script type="text/javascript" language="javascript" src="http://www.statcounter.com/counter/counter.js"><\/script>');*/
		
		<!-- Google Analytics -->
		document.write('<script type="text/javascript" src="'+(("https:" == document.location.protocol) ? "https://ssl." : "http://www.")+'google-analytics.com/ga.js"><\/script>');
		document.write('<script type="text/javascript">var pageTracker = _gat._getTracker("UA-126863-7"); pageTracker._initData(); pageTracker._trackPageview();<\/script>');
	}
}