$(function(){
	$('body').removeClass('js-disabled').addClass('js-enabled');
	
	$('#products').tabify();
	
	$('li.headlink').navDropdown();
	
	$('#searchTerm').clearSearch();
});

$.fn.tabify = function() {
    var tabStrip = $('<ul id="product_nav_tabs" class="tabs"></ul>');
    $(tabStrip).prependTo('#products');
    $('h4', this).each(function() {
        $('<li><a href="#">' + $(this).text() + '</a></li>').appendTo(tabStrip).children().click(changeToTab);
        $(this).remove();
    });
    $('a:eq(0)', tabStrip).click();
}

function changeToTab() {
		
    var index = $(this).parents('ul.tabs').children().index($(this).parent());
    // $(this).parents('div').children('div.products').hide().eq(index).show();
    $('div.products').hide().eq(index).show();

    // highlight the tab
    $(this).parent().addClass('current').siblings().removeClass('current');

    return false;
}

$.fn.navDropdown = function() {
	return this.each(function(){
		var header = $(this);
		var subnav = $('>ul',this);
		var timer;
		header.hover(
			function() {
				clearTimeout(timer);
				header.addClass('nav_js_background');
				subnav.show();
			},
			function() {
				timer = setTimeout(function(){
					header.removeClass('nav_js_background');
					subnav.hide();
				},300);
			}
		);
	});
}


$.fn.clearSearch = function() {
	var searchTitle = $(this).attr("title")
	var searchVal = $(this).attr("value")
	
	$(this).focus(function() {
		  if ( $(this).val() == "Product search")
		      $(this).val('');
		  }
  );

	$(this).blur(function() {
		  if ( $(this).val() == "")
		      $(this).val('Product search');
		  }
  );
}