/* =================================================================== */
// various link functionality - popups, external, onstate
// original script taken from Jeremy Keith
// dependencies: jQuery
var anchors = {
	a: Object,
	addBehaviors : function() {
		$('a').each( function() {
			var $a = $(this);
			// external links
			if ( ($a.attr('rel')=="external") || $a.hasClass('external') || $a.hasClass('pdf') || $a.hasClass('popupFull') ) {
				$a.click( function(e) {
					e.preventDefault();
					return anchors.openWin(this,"");
				});
			}
			// popup
			if ( $a.hasClass('popup') ) {
				$a.click( function() {
					return anchors.openWin(this,"height=550,width=600,scrollbars=yes");
				});
			}
			// onstate
			if ( $a.attr('href') == location.href ) {
				$a.addClass('onstate');
			}
		});
	},
	openWin : function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	}
};
/* =================================================================== */

/* =================================================================== */
// function to add events crossbrowser if not using a js library
// from: http://www.dustindiaz.com/rock-solid-addevent/
// needs to be higher in the code than any calls to it.
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];
	}
}

// use only with addEvent function
// this is here to clear the event cache to prevent memory leaks
// for more info:  http://novemberborn.net/javascript/event-cache
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);
/* =================================================================== */

/* =================================================================== */
// function to add/remove classes from elements
// written by Christian Heilmann 
// more info here http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
// call like cssjs('add',containerOBJ,classname);
function cssjs(a,o,c1,c2){
	switch (a){
		case 'swap':
			o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
		case 'add':
			if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp("(^|\\s)" + c1 + "(\\s|$)").test(o.className);
		break;
	}
}

/* =================================================================== */

/* =================================================================== */
// add a class of "js" to the body tag, to allow for css that gets
// implemented only when js is available
// dependencies: cssjs(), addEvent()
// could change this to the html tag as well pretty easy
function addJSClass () {
	var oBody = document.getElementsByTagName('body')[0];
	if (!oBody) { return; }
	cssjs('add',oBody,'js');
}

/* =================================================================== */