$(document).ready( function() {

	/**
	 * Tweaks
	 *
	 * Tweaks and workarounds for presentation and funtionality
	 */
	 
	// Zebra Tables
	$('table tbody tr:even').addClass('even');
	
	// Scroll to top with animation
	$('.back-to-top a').bind('click', function() {
		$('html,body').animate({ scrollTop: $("#header").offset().top }, 500);
		return false;
	});
	
	// Fade linked images on hover
	var imageLinks = $("#content-main a img");
	imageLinks.hover(
		function() {
			$(this).fadeTo('fast', 0.8);
		},
		function() {
			$(this).fadeTo('fast', 1)
		}
	);
	
	// Add psuedo selector stand ins for graceful degredation
	$('ul li:last').addClass('last-child');
	$('ul li').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	
	/* Image Hover
	$('.content-tools a').hover(
		function() {
			$(this).fadeTo('slow', 0.5);
		},
		function() {
			$(this).fadeTo('fast', 1);
		}
	);*/
	
	
	/**
	 * High Contrast Link
	 *
	 * Insert and bind high contrast link to click
	 */
	
	// Insert high contrast link
	var contrastLink = $('.content-tools').append($('<li><a href="#" class="high-contrast">View this site in high contrast</a></li>')),
		bodyEl = $('body');
	$('a.high-contrast', contrastLink).bind("click", function() {
		bodyEl.toggleClass('high-contrast');
		
		// Set cookie
		if ($.cookie != undefined) {
			$.cookie('highContrast', bodyEl.hasClass('high-contrast'));
		}
		
		return false;
	});
	
	// Have we got a cookie? Nom nom
	if ($.cookie != undefined) {
		highContrastEnabled = $.cookie('highContrast');
		if (highContrastEnabled != null && highContrastEnabled == 'true') {
			bodyEl.addClass('high-contrast');
		}
	}
	
	
	/*
	 * Text Resize
	 *
	 * Insert text resize links and initiate text resize plugin
	 */

	// Insert text resize links
	var textLinks = $('.content-tools')
		.append(
			'<li>' +
				'<a href="#" class="text-decrease">Decrease the text size</a>' +
				'<a href="#" class="text-increase">Increase the text size</a>' +
			'</li>'
		),
		increaseTextLink = $('a.text-increase', textLinks),
		decreaseTextLink = $('a.text-decrease', textLinks),
		resetTextLink = $('<a href="#"></a>'),
		textArgs = {
			baseFontSize: '1em',
			maxZoom: 1.25,
			steps: 4
		};
		
	// Initiate text resize plugin
	$("#content-main").textResize(decreaseTextLink, increaseTextLink, resetTextLink, textArgs);
	
	// Fade out on disable
	fadeTextLinks();
	$('a', textLinks).bind('click', function(e){
		fadeTextLinks();
	});
	
	function fadeTextLinks(el) {
		$('a', textLinks).each(function(){
			el = $(this);
			if ( el.hasClass('link-disabled') ) {
				el.fadeTo('slow', 0.4);
			}
			else {
				el.fadeTo('fast', 1);
			}		
		});
	};

});