var $j = jQuery.noConflict();
$j(document).ready(function() {
	
	// Cycles through a group of elements and gives them the same (max) height.
	function sameHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $j(this).height();
			if(tallest < thisHeight) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}

	$j('#topnav a[href="' + location.pathname + '"]').addClass('active');
	
	$j("h2 a").each(function() {
		var wordArray = $j(this).text().split(" ");
		wordArray[wordArray.length-2] += "&nbsp;" + wordArray[wordArray.length-1];
		wordArray.pop();
		$j(this).html(wordArray.join(" "));
	});
	
	$j("#sidebar h2").wrapInner("<span></span>");

	/* Hack to ensure the height is correctly calculated in 
	Webkit browsers. If images don't have a height defined, 
	those browers calculate based on an image height of zero. */
	$j("#sidebar #sponsors #squareads img").attr("height",125);

	// Display ads in the sidebar only if there is room. 
	heightDiff = $j("#content").height() - $j("#sidebar").height();
	
	if(heightDiff > 600) {
		$j("#sidebarad-160x600-1").show();
		$j("#sidebarad-160x600-2").show();		
	} else if(heightDiff > 250) {
		$j("#sidebarad-300x250").show();
	}
	
	// Remove bold from our code samples. Currently there's a line-height bold-text bug in Firefox.
	$j('span[style]').css('font-weight','normal');
	
	// Normalize some heights.
	sameHeight($j(".recent-article"));
	sameHeight($j(".footer-col"));
	$j("#footer").height($j(".footer-col").height());	
	
	$j("a:has(img), img[src^='https']").addClass("no-border");
	$j(".adblock").wrap('<div class="adwrap"></div>');
	
	if($j(".subscribers").html() == "N/A" || $j(".subscribers").html() == "0") {
		$j(".subscribers").html("thousands of");
	}
});
