$(function() {

	function getQueryVariable(variable) { 
		var query = window.location.search.substring(1); 
		var vars = query.split("&"); 
		for(var i=0; i<vars.length; i++) { 
			var pair = vars[i].split("="); 
			if( pair[0] == variable ) { 
				return pair[1]; 
			} 
		} 
	} 

	/*--- NAV RELATED ---*/
	var currNavItem = null;

	// open the first menu group on page load
	var firstNavGroup = $("#left-bar h3.collapse:first + ul");
	currNavItem = "#"+firstNavGroup.attr("id");
	firstNavGroup.show().addClass("current-nav");

	 // handle the left nav accordian
     $("#left-bar h3.collapse").click(function() {
         var selectedUL = "#menu-" + 
$(this).text().toLowerCase().replace(/ /g, "-");

         // clear the 'current nav item'
         $(".current-nav").removeClass("current-nav").slideUp("fast");

         // open the selected nav item
         if( currNavItem !== selectedUL ) {
             $(selectedUL).slideDown("fast").addClass("current-nav");
             currNavItem = selectedUL;
         } else {
             currNavItem = "";
         }
     });

	// keep current menu group open if a department is in the URL
	if( location.search.length > 0 ) {
		var newID;
		if( location.search.indexOf("deptid=") > -1 ) {
			newID = getQueryVariable("deptid");
		} else if( location.search.indexOf("catid=") > -1 ) {
			newID = getQueryVariable("catid");
		}
		var parUL = $("#m-"+newID).parent();

		if( parUL.length > 0 ) {
			$("#left-nav ul").hide();
			$(parUL).show().addClass("current-nav");
			currNavItem = "#"+parUL.attr("id");
		}
	}
	
	/*--- DEPT RELATED ---*/
	$(".catname:eq(1)").hide();
	$(".pagecount:last").addClass("last-pagecount");

	/*--- PRODUCT RELATED ---*/
	// remove the prev/next images and replace with their alt text
	$(".prodnav a").each(function() {
		var text = $(this).children("img").attr("alt");
		$(this).removeAttr("onmouseout").removeAttr("onmouseover").html(text);
	});
	$(".prodnav a:first").attr({ id: "prev" });
	$(".prodnav a:last").attr({ id: "next" });

	// remove the inline style on sale prices so we can make them orange
	$(".price span, .smallprice span, .rprice span").removeAttr("style");

	// switch to TB for detail popup
	$(".product .pictrow a").each(function() {
		// grab the product ID from the URL
		if( location.search.indexOf("pfid=") > -1 ) {
			$(this).attr({ href: "/detail.asp?pfid="+getQueryVariable("pfid")+"&width=650&height=600" }).removeAttr("onclick");
		}
	});
	// re-initialize TB
	tb_init(".product .pictrow a");

	// hide the bottom pagination on the product page
	$(".catname:eq(1)").hide();

	// we assign an ID to the quantity field for styling
	$(".prodformtable input[name='quantity']").attr({ id: "product-quantity" });

	$(".prodformtable img").replaceWith("<p>Product Options:</p>");

	$("#addtobag").removeAttr("onmouseout").removeAttr("onmouseover");

	/*--- SEARCH RELATED ---*/
	// remove the prev/next images and replace with the proper text
	$("#PreviousT, #NextT, #PreviousB, #NextB, #PreviousB1, #NextB1").each(function() {
		var newNames = { nextPage: "Next Page", prevPage: "Previous Page" };
		var text = newNames[$(this).attr("alt")];
		$(this).parent("a").removeAttr("onmouseout").removeAttr("onmouseover").html(text).attr({ id: $(this).attr("id") });
	});

	// move the pagination under the search header
	$("#results-inside h1").after('<div id="pagination">&nbsp;</div>');
	$("#PreviousT, #NextT, .pagecount:first").appendTo("#pagination");

	// target table rows for products in the search results
	$("table.results tr[valign='top']").addClass("result-row");
	
	var cn = $(".catname h1").html();
	if(cn == "") {
			$(".catname h1").html("Search Results");	
	}
	
	$(".results:contains('product(s) found for'):eq(1)").remove();
	
	/*--- ADD TO BAG RELATED ---*/
	$(".shoppingbag input[name='checkout']").attr({ width: "89" });
});
