
$(document).ready(function(){
	search.init();

	addJSClass();
	textSize.checkForCookie();
	textSize.setUpFontSizingButtons();
	sfHover();
	initShortcutsSelect();
	addPaddingToDhtmlMenus();
	
	$("table.contentTable tbody tr:nth-child(odd)").addClass("odd");
});


var textSize = {
	// variables 
	cookieName		: 'jaxFontSize',
	baseFont		: 16, /* base browser font size - generally 16 */
	fontSize1 		: 68.75,
	rangeLow		: 11,
	rangeHigh		: 15,
	disabledClass	: 'disabled',
	
	// check and see if there is an existing cookie, and if so, use it to set the font size
	checkForCookie : function() {
		var oCookie = getCookie(textSize.cookieName);
		if (oCookie) {
			textSize.updateFontSize('',oCookie);
		this.disableButtons(oCookie);
		} else {
			textSize.updateFontSize('',textSize.fontSize1);
		}
	},
	
	disableButtons : function(cookieVal) {
		var iFontInPx = ((cookieVal/100) * textSize.baseFont);
		if (iFontInPx <= textSize.rangeLow) {
			$('#textSmaller').addClass('disabled');
		} else if (iFontInPx >= textSize.rangeHigh) {
			$('#textBigger').addClass('disabled');
		}
	},
	
	setUpFontSizingButtons : function() {
		$('#textSmaller').click( function(e) {
			var iFontSize = textSize.checkCurrentFontSize();
			if (iFontSize <= textSize.rangeLow) {
				return;
			}
			$('#textControls a').removeClass(textSize.disabledClass);
			var iNewFontSize = ((iFontSize-1)/textSize.baseFont) * 100;
			textSize.updateFontSize(e,iNewFontSize);
			if ( (iFontSize-1) == textSize.rangeLow ) {
				$(this).addClass('disabled');	
			}
		});
		$('#textBigger').click( function(e) {
			var iFontSize = textSize.checkCurrentFontSize();
			if (iFontSize >= textSize.rangeHigh) {
				return;
			}
			$('#textControls a').removeClass(textSize.disabledClass);
			var iNewFontSize = ((iFontSize+1)/textSize.baseFont) * 100;
			textSize.updateFontSize(e,iNewFontSize);
			if ( (iFontSize+1) == textSize.rangeHigh ) {
				$(this).addClass('disabled');	
			}
		});
	},
	
	checkCurrentFontSize : function() {
		var iFontSize = parseFloat($('body').css('fontSize'));
		iFontSize = (iFontSize/100) * textSize.baseFont;
		return iFontSize;
	},
	
	updateFontSize : function(e,size) {
		$('body').css('font-size', size+'%');
		textSize.addCookie(size);
	},
	
	addCookie : function(info) {
		setCookie(textSize.cookieName, info);
	}
}



sfHover = function() {
	if (IS_IE) {
		var oNavUL = document.getElementById('navPrimary');
		if (!oNavUL) return;
		var sfEls = oNavUL.getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				cssjs('add',this,' sfhover');
				//this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				cssjs('remove',this,' sfhover');
				//this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

// adds padding to bottom of drop down menus
addPaddingToDhtmlMenus = function() {
	$('#navPrimary ul').find('li:last').each( function() {
		$(this).addClass('last');
	})
}


var search = {
	init : function() {
		$('#searchLink').click( function(event) {
			$(this).toggleClass('active');
			//$('form#search').addClass('active');
			$('form#search').toggle();
		});
		//search.formHover();
	},
	
	formHover : function() {
		$('form#search').hover(
			function() {
				//nothing to see here, move along
			},
			function() {
				$('#searchLink').toggleClass('active');
				$('form#search').toggle();
			}
		);
		
	}
}


/* Show/Hide Select box*/
function initShortcutsSelect() {
	$('.fauxSelect').wrapInner('<a href="#" class="showSelect"></a>');
	$("a.showSelect").toggle( function(){
		$(".selectList").slideDown("normal");
	},function(){
		$(".selectList").slideUp("normal");
	});
}



/* =================================================================== */
// function to add events crossbrowser
// from: http://www.dustindiaz.com/rock-solid-addevent/
// uncomment the EventCache lines if using EventCache function from code lib
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);
/* =================================================================== */


/* =================================================================== */
// 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');
}

//addEvent(window,'load',addJSClass);
/* =================================================================== */


/* =================================================================== */
// various link functionality - popups, external, onstate
// original script taken from Jeremy Keith
// dependencies: cssjs(), addEvent()
var anchors = {
	a : Object,
	doPopups : function() {
		if (!document.getElementsByTagName) { return false; }
		var links = document.getElementsByTagName("a");
		for (var i=0; i < links.length; i++) {
			var anchor = links[i];
			if (anchor.getAttribute('href') && anchor.rel.match('external')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"");
				};
				cssjs('add',anchor,'external');
			}
		    if (cssjs('check',anchor,'popup')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"toolbar=no");
				};
		    }
		    if (cssjs('check',anchor,'popupFull')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"");
				};
		    }
			if (anchor.href == location.href) {
				cssjs('add',anchor,'onstate');
			}
		}
	},
	openWin : function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	}
};
addEvent(window,'load',anchors.doPopups);
/* =================================================================== */

/* =================================================================== */
// 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;
	}
}

/* =================================================================== */




/* =================================================================== */
// get, set, and delete cookies
// ref: http://www.dustindiaz.com/top-ten-javascript/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) { return null; }
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) { end = document.cookie.length; }
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) { 
			document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}
/* =================================================================== */


/* =================================================================== */
//Browser check and preload and functionality of rollover images
var IS_DOM = (document.getElementById) ? true : false;
var IS_IE = (document.all) ? true : false;
var IS_IE50 = (navigator.userAgent.indexOf("IE 5.0") != -1);
var IS_Mac = (navigator.appVersion.indexOf("Mac") != -1);
/* =================================================================== */


