

$(document).ready(function(){
	// addJSClass(); this is being called in the master page for quicker response
	
	// these are visual - we'll do those first. 
	youtubeLinks();
	initShortcutsSelect();
	// these are functional
	sfHover();
	txtBoxClear.init();
	anchors.doPopups();
});


sfHover = function() {
	$('#navPrimary li').each( function() {
		$(this).hover( function() {
			$(this).addClass('sfhover');
		}, function() {
			$(this).removeClass('sfhover');
		});
	});
}

/* Show/Hide Select box */
function initShortcutsSelect() {
	$('.fauxSelect').wrapInner('<a href="#" class="showSelect" title="Show List"></a>');
	$("a.showSelect").toggle( function(){
		$(this).parent().addClass('up');
		$(".selectList").slideDown("normal");
	},function(){
		$(this).parent().removeClass('up');
		$(".selectList").slideUp("normal");
	});
}


youtubeLinks = function() {
	$('#content a').filter('a[@href*=youtube.com]').addClass('youtube');
}

/* =================================================================== */
// add a class of "js" to the body tag, to allow for css that gets
// implemented only when js is available
// dependencies: jQuery
// could change this to the html tag as well pretty easy
function addJSClass () {
	$('body').addClass('js');
}

/* =================================================================== */


/* =================================================================== */
// use this to automatically clear a text box of it's default value
// if nothing is typed in the box, the script will put the default value back
// useful on sites where search boxes don't have a seperate label
// you can use it for more than one box per page using the txtBoxes array
txtBoxClear = {
	txtBoxes : ['searchField'],
	init: function() {
		for (i=0;i<txtBoxClear.txtBoxes.length;i++) {
			var oCurrentTxtBox = document.getElementById(txtBoxClear.txtBoxes[i]);
			if (!oCurrentTxtBox) { continue; }
			oCurrentTxtBox.defaultVal = oCurrentTxtBox.defaultValue;
			txtBoxClear.clearBox(oCurrentTxtBox);
		}
	},
	clearBox : function(txtBox) {
		txtBox.onfocus = function() {
			if (txtBox.value == txtBox.defaultVal) { txtBox.value = ''; } 
		};
		txtBox.onblur = function() {
			if (txtBox.value == '') { txtBox.value = txtBox.defaultVal; }
		};
	}
};
/* =================================================================== */


/* =================================================================== */
// 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;
	}
};
/* =================================================================== */



/* =================================================================== */
// 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);
/* =================================================================== */





