$(document).ready(function() {

	// Preload all rollovers
	$("#logo img, #nav img").each(function() {
		// Set the original src
		rollsrc = $(this).attr("src");
		rollON = rollsrc.replace('_off', '_on');
		newImg = new Image(); 				// create new image obj
		$(newImg).attr("src", rollON);	// set new obj's src
	});

	// Navigation rollovers
	$("#logo a[href], #nav a[href]").mouseover(function(){
		imgsrc = $(this).children("img").attr("src");

		if (typeof(imgsrc) != 'undefined') {
			imgsrcON = imgsrc.replace('_off', '_on');
			$(this).children("img").attr("src", imgsrcON);
		}

	});

	// Handle mouseout
	$("#logo a[href], #nav a[href]").mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
			$(this).children("img").attr("src", imgsrc);
		}
	});

	//$('<div style="position: absolute; top: 0; left: 0; z-index: 1000; background-color: #ffa; width: 250px; padding: 4px; font: 11px/1 trebuchet;">Testing<br>testing</div>').prependTo('body');

	// adjust attachment property of #bodywrap bg image (see below)
	adjust_bodywrap_bg();

});

$(window).bind('resize', function () {
	//adjust_bodywrap_bg();
//alert('resize');
});

$(window).resize(function() {
	adjust_bodywrap_bg();
});

// ensure background watermark is always visible
// when the body wrapper is taller than the viewport,
// change the background-attachment property from 'scroll' to 'fixed'
function adjust_bodywrap_bg() {
	//$("#bodywrap").css("background-image", 'none');
	var $viewportheight		= $(window).height();
	var $viewportwidth		= $(window).width();
	var $bodywrapheight		= $("#bodywrap").height();
	var $bodywrapwidth		= $("#bodywrap").width();
	var $marginleft			= Math.floor(($viewportwidth - $bodywrapwidth) / 2);

	// if content is taller than viewport, change attachment to fixed
	// if attachment is fixed, then bg position must be calculated relative to viewport
	var $bodywrapbgattach	= ($viewportheight > $bodywrapheight) ? 'scroll' : 'fixed';
	$("#bodywrap").css("background-attachment", $bodywrapbgattach);
	if ($bodywrapbgattach == 'fixed') {
		$("#bodywrap").css("background-position", $marginleft + "px 100%");
	} else {
		$("#bodywrap").css("background-position", "0 100%");
	}

	$adjustedbgattachment	= $("#bodywrap").css("background-attachment");
	$adjustedbgposition		= $("#bodywrap").css("background-position");

	// debug
	var $msg						= 'viewportwidth: ' + $viewportwidth + 'px<br />';
	$msg						  += 'bodywrapwidth: ' + $bodywrapwidth + 'px<br />';
	$msg						  += 'marginleft: ' + $marginleft + 'px<br />';
	$msg						  += 'adjustedbgattachment: ' + $adjustedbgattachment + '<br />';
	$msg						  += 'adjustedbgposition: ' + $adjustedbgposition + '<br />';
	//display_debug($msg);
}

function display_debug($message) {
	$("#jquery-debug").remove();
	$('<div id="jquery-debug" style="position: absolute; top: 0; left: 0; z-index: 1000; background-color: #ffa; width: 250px; padding: 4px; text-align: left; font: 11px/1.1 \'Trebuchet MS\';">' + $message  + '</div>').prependTo('body');
}

