// Utility JavaScript Document

var util = {
	
	W3CDOM : document.getElementById && document.getElementsByTagName && document.getElementsByName && document.createElement && document.createTextNode,
	
	
	init : function() {
		
		// get external popup links
		var extLinks = util.getElementsByClass('newindow');
		var totalExtLinks = extLinks.length;
		
		for(var i = 0; i < totalExtLinks; i++) {
			
			extLinks[i].onclick = util.newWindow;
			extLinks[i].setAttribute('title', 'This link opens in a new window');
			
		}
		
	},
	
	// utility function for adding events
  	addEvent : function(obj, type, func) {
    	if (obj.addEventListener) {obj.addEventListener(type, func, false);}
    		else if (obj.attachEvent) {
      			obj["e" + type + func] = func;
      			obj[type + func] = function() {obj["e" + type + func] (window.event);}
      			obj.attachEvent("on" + type, obj[type + func]);
    		}
    	else {obj["on" + type] = func;}
  	},
	
	// utility for removing events
	removeEvent :  function(obj,evt,fn) {
		if (obj.removeEventListener)
			obj.removeEventListener(evt,fn,false);
		else if (obj.detachEvent)
			obj.detachEvent('on'+evt,fn);
	},
	
	// utility function for stopping structural markup default behavior; disables link hrefs in this case
	stopDefault : function(e) {
		 if (!e) {e = window.event;}
		 if (!e.preventDefault) {
			 e.preventDefault = function() { this.returnValue = false; }
		 }
		 e.preventDefault();
		 return false;
	},
	
	// getElementsByClass function created by Dustin Diaz
   	getElementsByClass : function(searchClass,node,tag) {

		// this array will hold the nodes that have the desired class
		var classElements = [];

		// if we did not pass the node parameter, assume document
		if (node == null) {node = document;}

		// if we did not pass the tag parameter, grab every node
		if (tag == null) {tag = '*';}

		// gather all the element nodes to look through; by default is everything in document
		var els = node.getElementsByTagName(tag);

		// to improve loop performance, determine the length ahead of time
		var elsLen = els.length;

		// establish the pattern to search for within className
		var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");

		// look through all the class properties to see if there is a match
		// the j variable is the counter variable that increments each time a
		// match is found and becomes the next item in classElements
		for (var i = 0, j = 0; i < elsLen; i++) {
			if (pattern.test(els[i].className)) {
			   classElements[j] = els[i];
			   j++;
			}
		}

		// send back the array of elements to whatever variable called this function
		return classElements;

	},
	
	// opens traditional popup window
	newWindow : function() {
    	window.open(this.href,"_blank","toolbar=1,location=1,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=800,height=600");
    	return false;
  	}
}

// object detection and initializing functionality
if (util.W3CDOM) {
   util.addEvent(window, 'load', util.init);
}