
// Site specific JS

$(document).ready(function(){
	addJSClass();
	cbhViewAsText.loadPage();
	cbhFonts.checkForCookie();
	cbhFonts.setUpFontSizingButtons();
	sfHover();
	alternatingTableRows();
	backToTopLink();
});


var cbhFonts = {
	// variables 
	cookieName	: 'cbhFontSize',
	fontSize1 	: 68.75,
	fontSize2	: 75,
	fontSize3	: 81.25,
	fontSize4	: 93.75,
	
	// check and see if there is an existing cookie, and if so, use it to set the font size
	checkForCookie : function() {
		var oCookie = getCookie(cbhFonts.cookieName);
		if (oCookie) {
			cbhFonts.updateFontSize('',oCookie);
		}
	},
	
	setUpFontSizingButtons : function() {
		$('#text-normal').click( function(e) {
			cbhFonts.updateFontSize(e,cbhFonts.fontSize1);
		});
		$('#text-big').click( function(e) {
			cbhFonts.updateFontSize(e,cbhFonts.fontSize2);
		});
		$('#text-biggest').click( function(e) {
			cbhFonts.updateFontSize(e,cbhFonts.fontSize3);
		});
	},
	
	updateFontSize : function(e,size) {
		$('body').css('font-size', size+'%');
		cbhFonts.addCookie(size);
	},
	
	addCookie : function(info) {
		setCookie(cbhFonts.cookieName, info, 90, '/');
	}
}



var cbhViewAsText = {
	// variables 
	cookieName	: 'cbhStyle',
	
	loadPage : function() {
		this.addViewAsTextLink();
		var oCookie = this.checkForCookie();
		var link = $('#viewAsText')[0];	
		if (oCookie) {
			if (oCookie === 'text') {
				cbhViewAsText.switchStyleText(link);
			} else {
				cbhViewAsText.switchStyleCss(link);
			}
		}
	},
	
	
	// check and see if there is an existing cookie, and if so, use it to set the css
	checkForCookie : function() {
		var oCookie = getCookie(cbhViewAsText.cookieName);
		if (oCookie) {
			return oCookie;
		}
		return false;
	},
	
	addViewAsTextLink : function() {
		$('#pageControls')
			.prepend('<a id="viewAsText" rel="Text Only" href="#pageControls">View as text</a>&nbsp;|&nbsp;')
			.find('a#viewAsText')
			.click( function() {
				cbhViewAsText.linkClick(this);
				return false;
		})
	},
	
	
	linkClick : function(el) {
		var oCookie = this.checkForCookie();
		if (oCookie) {
			if (oCookie === 'text') {
				cbhViewAsText.switchStyleCss(el);
			} else {
				cbhViewAsText.switchStyleText(el);
			}
		} else {
			cbhViewAsText.switchStyleText(el);	
		}
	},
	
	
	switchStyleText : function(link){
		var styleName = link.getAttribute("rel");
        $('link[@rel*=style][@title]').each(function(i)
        {
                this.disabled = true;
                if (this.getAttribute('title') == styleName) this.disabled = false;
        });
		$('style[@type=text/css]').attr('media','aural');
		cbhViewAsText.addCookie('text');
		$(link).text('View with styles');
	},
	
	
	switchStyleCss : function(link){
        $('link[@rel*=style][@title]').each(function(i)
        {
                this.disabled = true;
        });
		$('style[@type=text/css]').attr('media','screen');
		cbhViewAsText.addCookie('css');
		$(link).text('View as text');
	},
	
	addCookie : function(info) {
		setCookie(cbhViewAsText.cookieName, info, 90, '/');
	}
}






sfHover = function() {
	if (IS_IE) {
		var oNavUL = document.getElementById('nav-primary');
		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"), "");
			}
		}
	}
}

alternatingTableRows = function() {
    $('.contentTable tr').removeClass('alternate');
    $('.contentTable tr:even').addClass('alternate');
}


backToTopLink = function() {
	if ($('body').height() > 1000) {
		$('#backToTop').show();
	}
}







// Common JS below


/* =================================================================== */
//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);
/* =================================================================== */

/* =================================================================== */
// use cLog instead of alert or console.log - it will log to the console
// when available and will alert otherwise
function cLog(str) {
	if (typeof console == 'undefined') {
		alert(str);
	} else {
		console.log(str);
	}
}
/* =================================================================== */

/* =================================================================== */
// 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');
}

/* =================================================================== */




/* =================================================================== */
// 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,'popupFull')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"");
				};
		    }
		    if (cssjs('check',anchor,'popupFlash')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"width=415, height=290,toolbar=no, resizable=yes, scrollbars=yes");
				};
		    }
		    if (cssjs('check',anchor,'popup')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"width=630, height=700,toolbar=no, resizable=yes, scrollbars=yes");
				};
		    }
		    if (cssjs('check',anchor,'file')) {
				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";
	}
}
/* =================================================================== */

