jQuery(function($) {
	
	// add JavaScript class for styling
	$('body').addClass('hasJS');
	
	// add classes to odd table rows
	$('table tbody tr:nth-child(odd)').addClass('odd');
	
	// input box in the header to have disapearing title
	$('input.title-switch').each(function(){
		if ($(this).attr('title') != ""){
			// if the title exists
			if($(this).val() == "") {
				// if on load the value is blank
				$(this).val($(this).attr('title'));
			}	
			$(this).blur(function(){
				if($(this).val() == "") {
					$(this).val($(this).attr('title'));
				}
			});	
			$(this).click(function(){
				if($(this).val() == $(this).attr('title')) {
					$(this).val("");
				}
			})
		}
	});
	
	//	Make the targets of internal links toggleable.
	$('a.toggler').each(function() {
		//	Get the id of the target of each internal link.
		var selector = $(this).attr('href');
		if ($(selector)) {
			$(selector).toggle();
			//	When an internal link is clicked, toggle the display of the target.
			$(this).click(function() {
				var selector = $(this).attr('href');
				$(selector).css({'overflow':'hidden'});
				$(selector).slideToggle('fast');
				$(this).toggleClass('displaying');
				return false;
			});
		}
	});
	
	// Set up the contrast switcher
	$('#controls .high').click(function() {
		$('body').addClass('highContrast');
		$.cookie('contrast', 'high', {
			'path': '/',
			'expires': 365 // days
		});
		return false;
	});
	$('#controls .standard').click(function() {
		$('body').removeClass('highContrast');
		$.cookie('contrast', 'standard', {
			'path': '/',
			'expires': 365 // days
		});
		return false;
	});
	// Set initial contrast based on cookie
	if ($.cookie('contrast') == 'high') {
		$('body').addClass('highContrast');
	}
	
});

jQuery(window).load(function() {
	// Crazyweird fix lets us style abbr using CSS in IE - do NOT run onDomReady, must be onload
	document.createElement('abbr');
});

jQuery(function($){
	
	// addSizes was written by Natalie Downe 
	// http://natbat.net/2008/Aug/27/addSizes/
	
	// Copyright (c) 2008, Natalie Downe under the BSD license
	// http://www.opensource.org/licenses/bsd-license.php
	
	$('a[href$=".pdf"], a[href$=".doc"], a[href$=".mp3"], a[href$=".zip"], a[href$=".ogg"], a[href$=".m4u"]').each(function(){
		// looking at the href of the link, if it contains pdf, doc, zip, mp3, ogg, m4u, jpg, png, swf
		var link = $(this);
		var bits = this.href.split('.');
		var type = bits[bits.length -1];
		
		var url= "http://json-head.appspot.com/?url="+encodeURIComponent (this.href)+"&callback=?";
	
		// then call the json thing and insert the size back into the link text
		 $.getJSON(url, function(json){
			if(json.ok && json.headers['Content-Length']) {
				var length = parseInt(json.headers['Content-Length'], 10);
				
				// divide the length into its largest unit
				var units = [
					[1024 * 1024 * 1024, 'GB'],
					[1024 * 1024, 'MB'],
					[1024, 'KB'],
					[1, 'bytes']
				];
				
				for(var i = 0; i < units.length; i++){
					
					var unitSize = units[i][0];
					var unitText = units[i][1];
					
					if (length >= unitSize) {
						length = length / unitSize;
						// 1 decimal place
						length = Math.ceil(length * 10) / 10;
						var lengthUnits = unitText;
						break;
					}
				}
				
				// insert the text directly after the link and add a class to the link
				// note: if you want to insert the size into the link rather than after it change the following 'after' to 'append'
				link.after(' (' + type + ' ' + length + ' ' + lengthUnits + ')');
				link.addClass(type);
			}
		});
	});
});

/**
	open a new window
	@param url the url to open in the new window
	@param wName name of the new window
	@param para object literal of the paramaters for the new window, ex. {width:400,height:550,scrollbars:1}
	@return false
*/
function openWin(url,wName,para)
{
	if(typeof(arguments[2]) == "object")
		var values = _parameters(arguments[2]);
	window.open(url,wName,values);
	return false;
}

/**
	method to take object literal parameters for opening a new window
	@param attributes
	@return values in correct format, comma separated
*/
function _parameters(attributes)
{
	var values = [];
	for(attribute in attributes)
	{
		values.push(attribute + "=" + attributes[attribute].toString());
	}
	return values.join(",");
}
/*----------------------*/

/**
	create a new cookie
*/
function createCookie(name,value,days)
{
	if(days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
/*----------------------*/

/**
	read value of a cookie
	@param name the name of the cookie to read a value from
*/
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i < ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return false;
}
/*----------------------*/

/**
	erase a cookie
	@param name the name of the cookie to erase
*/
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
/*----------------------*/

var sizeCount = 0;
/**
	change font size on the page
	@param dir 1 (increase) or -1 (decrease)
*/
function fontSizer(dir)
{
	if(dir == "minus" && sizeCount == 0)
		return false;
	else if(dir == "minus")
		sizeCount--;
	if(dir == "plus" && sizeCount == 3)
		return false;
	else if(dir == "plus")
		sizeCount++;
	
	var page = document.getElementById("page");
	var children = page.getElementsByTagName('*') || document.all;
	
	for(var i = 0; i < children.length; i++)
	{
		childName = children[i].nodeName.toLowerCase();
		if(children[i].nodeType == 1 && (childName == "p" || childName == "li" || childName == "td" || childName == "h2" || childName == "h3" || childName == "h4"))
		{
			// console.log(childName);
			// get the current font size
			if(children[i].currentStyle)	// IE
				var fontSize = children[i].currentStyle["fontSize"];
			else if(window.getComputedStyle)	// everyone else
				var fontSize = document.defaultView.getComputedStyle(children[i],null).getPropertyValue("font-size");
			
			if(fontSize.match("px"))
				var unit = "px";
			else if(fontSize.match("%"))
				var unit = "%";
			else if(fontSize.match("em"))
				var unit = "em";
			// remove the px so we have an integer
			fontSize = fontSize.replace(/(px)|(em)|%/,"");
			// alert(fontSize)
			if(dir == "plus")
				var newSize = Math.round(fontSize * 1.2) + unit;
			else if(dir == "minus")
				var newSize = Math.round(fontSize/1.2) + unit;
			// alert(newSize)
			children[i].style.fontSize = newSize;
		}
	}
}

// function that allows getting elements by their class name. Returns the found elements in an array
// arguments: className - class name to search for, element - a starting point to start the search
// if no element is specified, all the nodes on the page are searched
document.getElementsByClassName = function(className,element)
{
	if(element)
	{
		var el = document.getElementById(element);
		var children = el.getElementsByTagName('*');
	}
	else
	{
		var children = document.getElementsByTagName('*') || document.all;
	}
	
	var elements = [];
	var regexp = new RegExp(className,"ig");
	for(i = 0; i < children.length; i++)
	{
		if(children[i].className.match(regexp))
			elements.push(children[i]);
	}
	if(elements.length > 0)
		return elements;
	else
		return false;
}
/*----------------------*/