// JavaScript Document
var cb = {
	addEvent: function(elm, evType, fn, useCapture) {
	// cross browser event handling
	if (elm.addEventListener) {
	elm.addEventListener(evType, fn, useCapture);
	return true;
	} else if (elm.attachEvent) {
	var r = elm.attachEvent('on' + evType, fn);
	return r;
	} else {
	elm['on' + evType] = fn;
	}
},


stopLink: function(e) {
	//prevent the default, which is the link opening in the current window
	if (window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}	
	if (e && e.stopPropagation && e.preventDefault) {
		e.stopPropagation();
		e.preventDefault();
	}
},
	/* as above but for Safari */
	cancelClick: function(e) {
		if (document.getElementById) {
			return false;	
		}
		return true;
	},
	
init: function() {
		if (!document.getElementsByTagName || !document.getElementById) return;
		/*var textfield = document.getElementById('searchwords');
		cb.addEvent(textfield, 'focus', cb.clearText, false);
		cb.addEvent(textfield, 'blur', cb.clearText, false);*/		
		
		/* listen for anchors with rel="external" attribute */
		var anchor_list = document.getElementsByTagName("a");
		for (var i=0; i<anchor_list.length; i++) { // loop through and find all anchor tags 
			var anchor = anchor_list[i];
			// below: check the a tag has an href attribute AND the rel="external" attribute 
			if (anchor.getAttribute("href")) {
				
				if (anchor.getAttribute("rel") == "external") {
					cb.addEvent(anchor, 'click', cb.openBlankWindow, false);			
					cb.addEvent(anchor, 'click', cb.stopLink, false);
					
				}

				if (anchor.className && (' ' + anchor.className + ' ').indexOf(' popup ') != -1) {
					cb.addEvent(anchor, 'click', cb.popup, false);
					cb.addEvent(anchor, 'click', cb.stopLink, false);
					anchor.onclick = cb.cancelClick;//for Safari only
				}
				if (anchor.className && (' ' + anchor.className + ' ').indexOf(' pdf ') != -1) {
					cb.addEvent(anchor, 'click', cb.openBlankWindow, false);
					cb.addEvent(anchor, 'click', cb.stopLink, false);
					anchor.onclick = cb.cancelClick;//for Safari only
				}			
			}
				cb.addEvent(anchor, 'mouseover', cb.linkTitle, false);
			
		}


},
popup: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
	if (el.nodeType==3) {
		el = el.parentNode;	// Safari fix	
	}	
	var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=525,height=475,resizable=yes, status=yes, scrollbars=yes';
		
	window.open(el.href, '', _POPUP_FEATURES);
},

/* function to clear the search box when clicked */
clearText: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
	
	
	if (el.value != '') {
		el.value = '';
	}
	else {
		el.value = 'search...';
	}
},

linkTitle: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
	
	if (el.rel == "external") {
	el.title = "external site; opens in new browser window";
	}
	if (el.rel == "pdf") {
	el.title = "link will open a pdf in new browser window";
	}	

}, 

/* function to open a blank window when an external hyperlink is clicked */
openBlankWindow: function(e) {
	var el = window.event ? window.event.srcElement : e ? e.target : null;
	
	window.open(el.href);
},

anchor_list: [] /* for IE??*/
};

cb.addEvent(window, 'load', cb.init, false);