function adjustColumns () {
	// if secondary content doesn't exist, widen main column to include right column space
	if ($("#pageBodyContent > .secondary").length == 0 || $("#pageBodyContent > .secondary").height() == 0 || $("#pageBodyContent > .secondary").text().length == 0) $("#pageBodyContent > .primary").css("padding-right", "0px");
	
	// if secondary content does exist, equalize the primary and secondary columns
	if ($("#pageBodyContent > .secondary").length > 0) {
//		var targetHeight = Math.max($("#pageBodyContent > .primary").height(), $("#pageBodyContent > .secondary").height());
//		$("#pageBodyContent > .primary, #pageBodyContent > .secondary").height(targetHeight);\
		$('#pageBodyContent > .primary, #pageBodyContent > .secondary').setEqualHeight();
	}
}

(function($){
	$.fn.deorphanize = function(){
		var ALLOWED = 'b|i|em|strong';
		var reALLOWED_TAGS = new RegExp('<(\/?)(' + ALLOWED + ')>', 'g');
		var reALLOWED_TAGS_TEMP = new RegExp('\{\{\{(\/?)(' + ALLOWED + ')\}\}\}', 'g');
		var reLAST_TWO_WORDS = new RegExp('([^ ^<^>]+) ([^ ^<^>]+)$');
		return this.each(function(){
			var obj = $(this);
			var bef = obj.html();
			var aft = bef.replace(reALLOWED_TAGS, '{{{$1$2}}}');
			aft = aft.replace(reLAST_TWO_WORDS, '$1&nbsp;$2');
			aft = aft.replace(reALLOWED_TAGS_TEMP, '<$1$2>');
			obj.html(aft);
		});
	};
})(jQuery);

(function($){
	// from http://www.webringideas.com/web-development/divs-of-equal-height-using-jquery-script.html
	jQuery.fn.setEqualHeight=function(o) {
		var maxHeight=0;
		var maxElement=null;
		jQuery(this).each(function(i) {
			if ((jQuery(this).height() + parseInt(jQuery(this).css("padding-bottom")) + parseInt(jQuery(this).css("padding-top"))) > maxHeight) {
				maxHeight = jQuery(this).height() + parseInt(jQuery(this).css("padding-top")) + parseInt(jQuery(this).css("padding-bottom"));
				maxElement = this;
			}
		});
		jQuery(this).not($(maxElement)).each(function(){
			$(this).height(maxHeight - parseInt(jQuery(this).css("padding-top")) - parseInt(jQuery(this).css("padding-bottom")))
		});
	};
})(jQuery);

(function ($) {
// based on vertical-align function @ http://www.seodenver.com/simple-vertical-align-plugin-for-jquery/
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);



$(document).ready(function() {

	if ( !( ($.browser.msie) && (parseInt(jQuery.browser.version) < 8) ) ) {
		// don't do this for IE if less than v8
		$('#nav .mainMenu > ul > li > a').vAlign().css('top', '-8px');
	}

	addBreadCrumb();
	adjustColumns();
	$('#pageBodyContent > .primary').find('h1, h2, h3, h4, p, li').deorphanize();
	$('#subnav').find('li a').deorphanize();

	function addBreadCrumb () {				
		$("#nav ul.breadcrumb").appendBreadCrumbList("/", {
			delimAfter: " &raquo; "
			, docRootLabel: "Home"
			, labels: {
				"about": "About Us"
			}
			, urlFilters: [ [/\/products\/.+$/, '/products/'] ]
		});
		$("#nav .mainMenu ul li, #subnav ul li").indicateCurrentPage("sel");
	}

});
