$(document).ready(function(){

    //Make blocks (products and categories) a link - product level 1, 2 and 3 - to appropriate link. 
    $('#main.products .block, #main.product-detail .related').each(function(){
	var block = $(this);
	block.click(function(){
		window.location = block.find('h3 a, dd a.hide').attr('href');
	});
    });
	
    // custom radio buttons and checkboxes!
    $(".wpcf7").customForms();
    
    $('.product-types input').change(function(){
	if ($(this).attr("checked") == false) {
	    $('.product-types input[value="All Product Types"]').attr("checked", false).siblings(".checkbox").add($('.product-types input[value="All Product Types"]').parent()).removeClass("selected");
	}
    });
    
    
    var searchbox = $("input#s");
    searchbox.defaultValue = "OEM part number or keyword";
    if(searchbox.val() == "") {
	searchbox.val(searchbox.defaultValue);
	searchbox.addClass("default");
    }
    searchbox.focus(function(){
	if(searchbox.val() == searchbox.defaultValue) {
	    searchbox.val("");
	    searchbox.removeClass("default");
	}
    });
    searchbox.blur(function(){
	if(searchbox.val() == "") {
	    searchbox.val(searchbox.defaultValue);
	    searchbox.addClass("default");
	}
    });
    
    // set up the breadcrumb sub-menus
    $("div.breadcrumb ul.menu li.menu-item").children("ul").parent().addClass("dropdown");
    $("div.breadcrumb ul.menu li.menu-item").last().addClass("last");
    
    //Open the breadcrumb sub-menu
    $("div.breadcrumb ul.menu li.menu-item .arrow").click(function(event){
        if (event.target != this) return;
	        
        var link = $(this).parent();
		
        var menu = link.children("ul");
        
        link.toggleClass("expanded").children(":not(ul)").css( {borderTopColor: "#e9e4dd", borderRightColor: "#643e2c", borderLeftColor: "#e9e4dd", borderBottomColor: "#ffffff" });
        menu.toggle();
	
	var menuWidth = link.width();
        var menuHeight = 0;
        menu.children("li").each(function(){
	    menuWidth = menuWidth < $(this).width() ? $(this).width() : menuWidth;
            menuHeight += $(this).height();
        });
        menu.height(menuHeight);
	if (!link.data("width") && menuWidth == link.width()) {
	    link.addClass("box");
	    menuWidth -= 13
	}
	if (!link.data("width")) menu.width(menuWidth);
	
	link.data("width", true);
	
    });
    
    //Hide the breadcrumb. 
    $("div.breadcrumb ul.menu li.menu-item").mouseleave(function(){
	var link = $(this);
	var menu = link.children("ul");
	if (link.hasClass("expanded")) {
	    menu.fadeOut();
	    link.children(":not(ul)").animate( { borderTopColor: "#ffffff", borderRightColor: "#ffffff", borderLeftColor: "#ffffff" }, function(){
		link.removeClass("expanded");
	    });
	}
    });
    
    //Put the Press Releases in the subnav. 
    $("#optionalInfo").click(function() {
    
	var heading = $(this);
	if (heading.css("background-position") == "0px 0px")
	    heading.css("background-position", "0px -21px");
	else heading.css("background-position", "0px 0px");
	
	heading.next().toggle();
    
    }).next().hide();
        
    $('#left-nav .page-item-106').append($('#left-nav .archive').show());

    //only submit search if something is in the input box. 
    $('#search').submit(function() {
    	var search_term = $('#s').val();
    	if (!search_term || search_term == '')
    	    return false;
	else
	    return true;
    });
    
    //Make two colums, only two checkboxes per row. 
    $(".wpcf7-checkbox.types").children().each(function(index){
	if (index != 0 && index % 2 == 0) $(this).css("clear", "left");
    });
    
    //Connect the print button to browser print function. 
    $("#content #main .print-page").click(function(){
	window.print();
    });
});

