$(document).ready(function() {
	$("#nav input:text").keydown(function(e) {
		if (e.keyCode == 13) // enter key
		{
			$("form").submit(function() {
				return false;
			});
			// homelink will give us navlead
			window.location.href=$("#homelink").attr("href") + 'search/-' + escape($(this).val());
			return false;
		}
	});
	$("#searchbutton").click(function() {
		$("form").submit(function() {
			return false;
		});
		// homelink will give us navlead
		window.location.href=$("#homelink").attr("href") + 'search/-' + escape($("#nav input:text").val());
		return false;
	});
	$(".RelatedLinks2").appendTo("#content");
	
	if (($("div.template3").length == 0) && ($("div.template4").length == 0) && ($(".RelatedLinks1 li").length > 6))
	{
		$(".RelatedLinks1 li").gt(5).appendTo($("<div style=\"display: none;\"></div>").appendTo(".RelatedLinks1 ul"));
		$("<li><a href=\"javascript:void(0);\" class=\"more\" title=\"More\">More</a></li>").appendTo(".RelatedLinks1 ul").click(function() {
			var linkdivheight = $(".RelatedLinks1 ul div").height();
			var contentheight = $("#content").height();
			var sidebarheight = $("#sidebar").height();
			$("#content").height(contentheight + linkdivheight);
			$("#sidebar").height(sidebarheight + linkdivheight);
			$(".RelatedLinks1 ul div").slideDown("fast");
			$(this).remove();
		});
	}
	
	// open all external links in a new window
	$("a[@href^=http]").click(function() {
		window.open(this.href);
		return false;
	});
	
	// add class to even rows in a table
	$("#content table tr:even").addClass("alternate");
	
	// add video overlay to links
	$("a.video").objectOverlay(360, 280);
	
	// move news location to first paragraph
	$('span.news-location').prependTo('#content div.news p:first');
	
	// check if user is in Precious and Base Metal Services. If so, add a EIB pricing button
	if (window.location.href.indexOf('precious_base_metal_services') > 0)
	{
		var eib_pricing_url = "http://www.catalysts.basf.com/apps/eibprices/mp/",
			$eib_button = $('<a id="eib-prices" href="' + eib_pricing_url + '" title="EIB Prices"></a>');
		$eib_button.click(function() {
			window.open(this.href);
			return false;
		}).appendTo('div.subnavigation');
	}
	
	// fix overlapping blocks
	var blockbottom = $("#blocks").get(0).offsetTop + $("#blocks").height();
	var footertop = $("#footer").get(0).offsetTop;
	if (blockbottom > footertop)
		$("#content").height($("#content").height() + (blockbottom - footertop));
		
	// fix overlapping sidebar
	var sidebarbottom = $("#sidebar").get(0).offsetTop + $("#sidebar").height() + 190;
	var footertop = $("#footer").get(0).offsetTop;
	if (sidebarbottom > footertop)
		$("#content").height($("#content").height() + (sidebarbottom - footertop));
	
	// this should be the last to run
	$("div.equal-last > div:last-child, div.equal-all > div, div.equal").equalHeight();
	$("#sidebar").css("padding-top", 190);
	$("#sidebar").css("height", $("#sidebar").height() - 190);
});

function doOnShow() {
	$(".RelatedLinks1 a.showmore").remove();
}

$.fn.equalHeight = function(){
    var max_height = 0;
    
    this.each(function(){
        var bottom = this.offsetTop + $(this).height();
        if (bottom > max_height) max_height = bottom;
    });
    
    this.each(function(){
        var cur_height = $(this).height();
        
        // we don't want to touch the div we are trying to match up against, so test to see if the height of the div is smaller than what we are trying to match
        if (max_height - this.offsetTop > cur_height)
        {
            // remove bottom padding since the div will grow anyway, then get current height again
            this.style.paddingBottom = 0;	    
            cur_height = $(this).height();
        
            // remove top padding and calculate what it was so we can add it later. keeping padding will skew the height.
            this.style.paddingTop = 0;
            var pad_top = cur_height - $(this).height();
            
            var new_height = max_height - this.offsetTop - pad_top;
            $(this).css("height", new_height);
            $(this).css("padding-top", pad_top);
        }
    });
};