$(document).ready(function(){
	// Declare Javascript Enabled
	$("html").addClass("js");
	
	/*
	 * Cosmetic & Functional Tweaks
	 *
	 */
	 
	// External Links
	$('a[@href^="http"]').each(
		function(){
			if(this.href.indexOf(location.hostname) == -1) {
				if (!$(this).attr("title")) {
					$(this).attr('title', 'External link')
				}
			}
		}
	);
	
	// Menu collapsed/expanded
	// This should have been done server side, but alas, matrix is matrix
	$("#nav > li").not(":has(li)").addClass("leaf");
		
	
	// List item and position helpers to make up for Matrix
	$("ul li:first-child, .group .block:first-child").addClass("first-child");
	$("ul li:last-child").addClass("last-child");
	
	// Remove from second level
	$("#nav li li a div").remove();
	
	// Remove Matrix default text on search
	$("#form-search input[type=text]").attr("onblur", "").attr("onfocus", "");
	
	// Matrix pagination enhancement
	$(".pagination ul li").contents().not("[nodeType=1]").each( function() {
		if (this.nodeValue != " ") {
			$(this).wrap('<span/>');
		}
	});
	
	// Focused Forms
	$('.semantic-form input, .semantic-form textarea, .semantic-form select').focus(function(){
		$(this).parents('.form-item').addClass("focused");
	}).blur(function(){
		$(this).parents('.form-item').removeClass("focused");
	});
	
	
	/*
	 * IE6 Min and Max Width
	 *
	 */
	if (jQuery.browser.msie) {
		if(parseInt(jQuery.browser.version) == 6) {
			// Min and max width for IE6
			$(window).wresize(checkAvailableWidth);
			checkAvailableWidth();
		}

		$(".semantic-form input, .semantic-form select, .semantic-form textarea").bind("focus", function() {
			$(this).addClass("focused");
		});
		
		$("..semantic-form input, .semantic-form select, .semantic-form textarea").bind("blur", function() {
			$(this).removeClass("focused");
		});
	} 
	
	/*
	 * Horizontal Rule for IE
	 *
	 */
	if (jQuery.browser.msie) {
		$("#content-main hr").wrap('<div class="hr"/>');
	}
	
	
	/*
	 * Misc IE6 Hacks
	 *
	 */
	if (jQuery.browser.msie && (parseInt(jQuery.browser.version) == 6) ) {
		$("#content-main blockquote p:first-child").addClass("first-child");
		$("form input[type=submit]").hover(
			function() {
				$(this).addClass("hover");
			},
			function() {
				$(this).removeClass("hover");
			}
		);
	}  
	
	
	/**
	 * Print Page
	 *
	 * Print this page stylesheet switcher
	 */
	
	// Insert print button
	$('#content-tools').prepend('<li id="print-version"><a title="Print friendly version" href="#">Print friendly version</a></li>');
	
	var printSheet = $("link[title='print preview']");
	var screenSheet = $("link[media='screen']");
	var printButton = $("a[title='Print friendly version']");
	
	// Print Preview
	printButton.click( function() {	
		// Switch to print
		$("body").fadeOut("fast", function() {
			screenSheet.each(function() {
				this.disabled = true;
			});
			printSheet.each(function() {
				this.disabled = true;
				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;
	});
	
	// Dont print preview message
	$("head").append(
		'<style type="text/css" media="print" title="Hide print preview message on print">' +
			'#preview-message {' +
					'display: none;' +
			'}' +
		'</style>'
	);
	
	
	/*
	 * Text Resize
	 *
	 */
	 
	// Insert resize links
	$('#content-tools').prepend('<li id="text-size"><a href="#" title="Decrease Text Size" id="text-decrease">Decrease Text Size</a> <a href="#" title="Increase Text Size" id="text-increase">Increase Text Size</a></li>');
	// Have we got a cookie?
	savedFontSize = $.cookie('fontSize');
	if (savedFontSize != null) {
		$('body').css('font-size', savedFontSize);
	}
	else {
		$('body').css('font-size', "11px");
	}
	// Increase Font Size
	$("a#text-increase").click(function(){
	var currentFontSize = $('body').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize, 10);
	var newFontSize = currentFontSizeNum+1;
	if (currentFontSizeNum < 16) {
		$('body').css('font-size', newFontSize);
		// Set Cookie
		$.cookie('fontSize', newFontSize+'px');
	}
	return false;
	});
	// Decrease Font Size
	$("a#text-decrease").click(function(){
	var currentFontSize = $('body').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize, 10);
	var newFontSize = currentFontSizeNum-1;
	if (currentFontSizeNum > 9) {
		$('body').css('font-size', newFontSize);
		// Set Cookie
		$.cookie('fontSize', newFontSize+'px');
	}
	return false;
	});
	
	
	/*
	 * Form Labels
	 * 
	 * Sexy labels in inputs for search 
	 *
	 */
	var searchBlock = $("#form-search");
	$(searchBlock).find("input[type=text]").each( function() {
		var input = $(this);
		var labelObj = $("label[for=" + input.attr("id") + "]");
		var label = $(labelObj).text();
		
		// Add and Broadcast labeled input
		input
			.addClass("labeled")
			.val(label);
			
		// Hide label element
		labelObj.hide();
			
		// Remove label if no input has been made
		input.bind("focus", function() {
			if ($(this).val() == label) {
				$(this).val("").removeClass("labeled");
			}
		});
		
		// Add label again if no input added
		input.bind("blur", function() {
			if ($(this).val().length == 0) {
				$(this).val(label).addClass("labeled");
			}
		});
	});

	// Remove labels on submit
	inputEls = $(searchBlock).find("input[type=text]");
	$(searchBlock).find("input[type=text]").parents("form").bind("submit", function() {
		inputEls.each( function() {
			if ($(this).val() == $("label[for=" + $(this).attr("id") + "]").text()) {
				$(this).val("");
			}
		});
		return true;
	});
});

function checkAvailableWidth() {
	var minWidth = parseFloat($("#wrapper").css("min-width"));
	var maxWidth = parseFloat($("#wrapper").css("max-width"));
	var winWidth = $(window).width(); 
	var conWidth = 'auto';
	if (winWidth < minWidth) { conWidth = minWidth; }
	else if (winWidth > maxWidth) { conWidth = maxWidth; }
	$("#wrapper").css('width', conWidth);
}