$(document).ready(function() {


//	$("#contentTopper-text h1").hide();


	// Form input text swap
	$("input[type='text']").each(function(){
		this.orig_value = $(this).val();
		$(this).focus(function(){
			if ( $(this).val() == this.orig_value)
				$(this).val('');
		}).blur(function(){
			if ( ($(this).val() == this.orig_value) || (!$(this).val().length))
				$(this).val(this.orig_value);
		});
	});




	// splitList...but only if it's NOT a For Teachers template
	$("#article-intro ul").splitList(2, { wrapClass: "floatyList" });
	$(".pdfCols ul").splitList(2, { wrapClass: "pdfCol" });



	tweak_header_icon_position();



	// Firsts and lasts
	$("#utility-navigation li:first-child").addClass("first");
	$(".teachers #article-intro ul li:first-child").addClass("first");
	$("#article-intro div.floatyList:first-child").addClass("floatyList-first");
	$(".pdfCols div.pdfCol:first-child").addClass("pdfCol-first");



	// Sometimes the "Did You Know?" box forces itself into the footer. This makes the body tall enough to accomodate.

//	$(".did-you-know").css("outline","1px solid red");
//	var boxHeight = $(".did-you-know").height();
//	var contentHeight = $("#content-wrap").height();
//	var revisedHeight = boxHeight + contentHeight + 300;
//	alert("the yellow box is: "+boxHeight+" pixels tall");
//	alert("the content overall is: "+contentHeight+" pixels tall");
//	alert("if you add them together it becomes: "+revisedHeight+" pixels tall");

//	if($(".did-you-know").length > 0) {
//		alert("This page has a sidebar.");
//		if(contentHeight < revisedHeight) {
//			$("#content-wrap").height(revisedHeight);
//		}
//	}



	// Let's move the "Did You Know?" box into the #secondary div.
	$(".did-you-know").appendTo("#secondary");

	// Background Substitution for iPhone
	 if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		$("#top-container").css({ 'background-image': 'url(images/bg/cloud_repeaters_iphone.gif)'});
	}

	// Making peace with Safari
	if($.browser.safari) {
		$("#search").css(
			{
				"right":"58px",
				"top":"11px"
			}
		);
	}
	if($.browser.safari) {
		$(".homepage #search").css(
			{
				"right":"40px"
			}
		);
	}
	if($.browser.safari) {
		$("#content-wrap").addClass("safariAdjust");
	}

	// guarantee image resizing only after loading
	$('.image, .lightbox').onImagesLoad({
		itemCallback: resize_image_containers_postLoad
	});

});

function resize_image_containers_postLoad(item) {
	var img_container = $(item);
	var width = img_container.find("img").width();
	img_container.css({'width': (width + 2) + "px"});
	img_container.find(".image-enlarge").html("<span>Click to enlarge</span> &raquo;");
}

function tweak_header_icon_position() {
	// Adjusts the background position of header icons when a subhed is present.
	var subhed, container, coords, adjusted_position;
	subhed = $("h2.whichParent");
	if (subhed.length > 0) {
		container = $("#contentTopper-text");
        try {
            coords = container.css("background-position").replace(/px/g, "").split(" ");
        }
        catch (e) { return; }
		coords[1] = new Number(coords[1]) + subhed.height() - 4;
		adjusted_position = coords[0] + "px " + coords[1] + "px";
		container.css({"background-position": adjusted_position});
	}
}
