$(document).ready(function() {
	$(".no-script-only").hide();
				    
	// set click actions for the menu tabs
	$("a.category").click(function() {
		showCategoryMenu();
		return false;
	});
	$("a.collection").click(function() {
		showCollectionMenu();
		return false;
	});
	
	if($("div.showCollectionFirst").height() != undefined)
	{
	    showCollectionMenu();
	}
	
    uniformPods();
	priceCode();
	
	productNav();
	resizeNav(); // do this last.
});

function productNav(){
	// close all to start
	$(".alpha-group").not($("#collection-menu li.selected").parents(".alpha-group")).hide();
	// bind events
	$(".alpha-group-toggle").click(function(){
	    $(this).toggleClass("open").siblings(".alpha-group").slideToggle("fast");
	    $(".alpha-group").not($(this).siblings(".alpha-group")).slideUp("fast");
	    $(".alpha-group-toggle").not($(this)).removeClass("open");
	    return false;
	});
	
	if($("#collection-menu .selected").length > 0)
	{
        showCollectionMenu();
        //$("#collection-menu li.selected").parents(".alpha-group").siblings(".alpha-group-toggle").click();
    }
    else
	    showCategoryMenu();
}

// menu show / hide functions
function showCategoryMenu(){
	$("a.collection").removeClass("collection-selected");
	$("a.category").addClass("category-selected");
	$("#collection-menu").hide();
	$("#category-menu").show();
	//resizeNav();
}

function showCollectionMenu(){
	$("a.category").removeClass("category-selected");
	$("a.collection").addClass("collection-selected");
	$("#collection-menu").show();
	$("#category-menu").hide();
	//resizeNav();
}

function resetLine(){
	$('#collection-menu').find('li').slice(0,1) 	
}
    
    
function resizeNav(){
	var paddingOffset = $("div.page-numbers").css("padding-top");
	var homeMidSection = $("#middle").height();
	if(paddingOffset != undefined){
		var mainColSize = $("#products-content").height() - ($("div.page-numbers").height() + parseInt(paddingOffset));
	} else if (homeMidSection != undefined) {
		var mainColSize = homeMidSection;
	} else {
		var mainColSize = $("#products-content").height() - ($("div.page-numbers").height());
	}
	//$("#products-sidebar").height( Math.max($("#products-sidebar").height(), mainColSize)); // doesn't work when dynamically resizing.
	$("#products-sidebar").css('min-height',mainColSize+'px');
}

function priceCode(){
	// 'price code' appear on hover over product image
	$(".price-code").css("opacity", "0").show(); // prep for fading.
	$("a.product-image-link").hover(
		function () {
		    $(this).children(".price-code").stop().animate({opacity:1},"500");
		}, 
		function () {
		    $(this).children(".price-code").stop().animate({opacity:0}, "500"); 
		}
	);
}

function uniformPods(){
	var thePods = $("div.listing-pod, #related-products li");
	var noPods = thePods.length;
	var noRows = Math.ceil(noPods/3);
	for(i=0; i<noRows; i++){
		var highest = 0;
		for(k=(i*3); k<(i*3 + 3); k++){
			if(k < noPods){
				var podHeight = $(thePods[k]).height();
				if(podHeight > highest){
					highest = podHeight;
				}
			}
		}
		for(k=(i*3); k<(i*3 + 3); k++){
			$(thePods[k]).height(highest);
			$(thePods[k]).find('div').filter('[class=price-code]').hide();
		}
	}
}
