$(document).ready( function() {
	
	$("a[rel=external]").attr({ "target" : "_blank" });
	
	// handle subnav
	$(".nav_sub a:not(.on)").hover(
		function () {
			$(this).parents("li").toggleClass("hover");
		},
		function () {
			$(this).parents("li").toggleClass("hover");			
		}
	);
	$(".nav_sub a.on").parents("li").addClass("on");
	
	// manage product tables
	$(".table_products tbody tr:last").addClass("last");
	
	// match column heights
	addLoadEvent(function() {
		$(".content, .sidebar").matchColumns();
	});	
	
	// twice because of some IE bugs
	if($.browser.msie){
		$(".content, .sidebar").matchColumns();
	}
	
});

jQuery.fn.matchColumns = function(){

	var height_tar = 0;
	
	$(this).each(function(){
		height_obj = (this.offsetHeight) ? this.offsetHeight : this.pixelHeight;
		height_tar = Math.max(height_tar, height_obj);
	});
	
	$(this).each( function() {
		var pad_top = $(this).css("padding-top");
		var pad_bot = $(this).css("padding-bottom");
		pad_top = Number (pad_top.split('px')[0]);
		pad_bot = Number (pad_bot.split('px')[0]);
		
		var offset = pad_top + pad_bot;
		
		$(this).css({ "height" : (height_tar-offset)+"px" });
	});
	
};

// onload window event
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}


function empty(x) {
	return (x=="" || x==null);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars ||"\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}