$(document).ready(function(){
	
	// JS Enabled
	$("html").addClass('js');
	
	// FontSizer
	var options = { min: -5, max: 0.4};
	//Initialize the font sizer for the site.
	$.FontSizer.Init(options);
	$("a#tools-text-decrease").click(function() {
		$.FontSizer.DecreaseSize();
		return false;
	});
	
	$("a#tools-text-increase").click(function() {
		$.FontSizer.IncreaseSize();
		return false;
	});
	
	// External Links
	$("a[@href^=http]").each(
		function(){
			if(this.href.indexOf(location.hostname) == -1) {
				$(this).attr('target', '_blank')
					.attr('class', 'external-link')
					.attr('title', 'External link (will open in new window)');
			}
	
		}
	);
	
	// Navigation First List item
	$("ul#nav li:first").addClass("first");
	
	// Last Link item
	$("#site-info ul li:last").addClass("last");
	
	// Zebra Tables
	$("table.data tbody tr:odd").addClass("odd");
	
	// Focus on first form element
	$("#content-main :input:visible:enabled:first").focus();
	
	/**
	 * Print Page
	 *
	 * Print this page stylesheet switcher
	 */	 
	var printSheet = $("link[title='print preview']");
	var screenSheet = $("link[media='screen']:first");
	var printButton = $("a[title='Printer Friendly Version']");
	
	// Print Preview
	printButton.click( function() {
		// Switch to print
		$("body").fadeOut("fase", function() {
			screenSheet.each(function() {
				this.disabled = true;
			});
			printSheet.each(function() {
				this.disabled = true;
			});
			printSheet.each(function() {
				this.disabled = false;
			});
			$(this).fadeIn("slow");
			
			$('html, body').animate({scrollTop:0}, 'fast');

			// Create Print Preview Heading
			$("body").prepend('<div id="preview-message"><h3>Print preview</h3><p><a href="#" id="preview-print">Print this page</a> | <a href="#" id="turnoff-print">Return to the normal view</a></p></div>');
			$("#preview-message").hide().slideDown("slow");
			
			// Switch Back
			$("a#turnoff-print").bind("click", function(){
				$("body").fadeOut("fast", function() {
					$("#preview-message").remove();
					// Switch to screen
					screenSheet.each(function() {
						this.disabled = false;
					});
					printSheet.each(function() {
						this.disabled = true;
					});
					$(this).fadeIn("slow");
				});
				return false;
			});
			
			// Print this page
			$("a#preview-print").bind("click", function(){
				window.print();
				return false;
			});
		});
		
		return false;
	});
	
	// Print Preview Styling
	$("head").append(
		'<style type="text/css" media="screen">' +
			'#preview-message {' +
				'border-bottom: 1px solid #666;' +
				'background: #FF6;' +
				'padding: 5px 10px 10px;' + 
			'}' +
			
			'#preview-message h3 {' +
				'color: #000;' +
				'font-size: 16px;' +
				'margin: 0 0 5px;' +
			'}' +
			
			'#preview-message p {' +
				'margin: 0;' +
			'}' +
		'</style>'
	);
	
	$("head").append(
		'<style type="text/css" media="print">' +
			'#preview-message {' +
					'display: none;' +
			'}' +
		'</style>'
	);
	
	// Look over results and tag appropriately
	if ($("#search-results").size()) {
		var resultClass;
		var resultSite;
		$("#search-results p.url").each(function() {
			// Employers
			if ( $(this).is(":contains('/employers/')") ) {
				resultClass = 'employer';
				resultSite = 'Employers';
			}
			// Parents & Carers
			else if ( $(this).is(":contains('/parentsandcarers/')") ) {
				resultClass = 'parent-carer';
				resultSite = 'Parents & Carers';	
			}
			// Global
			else {
				resultClass = '';
				resultSize = '';
			}
			
			// Highlight each area
			if (resultClass != '') {
				$(this).siblings("h3").after('<p class="site">' + resultSite + '</em>');
				$(this).parent("li").addClass(resultClass);
			}
		});
	}
});