//this function will hide all letters without content
function hideEmptyLetters()
{
	//go through each letter at the top
	$(".letter").each(function(){
		
		var letterID = $(this).attr("id");
		
		var num_items = $("." + letterID + ":not(:hidden)").size();

		if(num_items == 0)
		{
			var letter_split = letterID.split("_");
			var letter = letter_split[1];
			
			$("#" + letter + "_list").hide();
			
			//lets remove the link to this letter
			var link_text = $(this).html();
			$(this).after("<span id=\""+ letterID +"\" class=\"disabled\">" + link_text + "</span>");
			$(this).remove();
			
			$(this).addClass("disabled");
			
		}
	});
}

function reactivateLinks()
{
	$("#alphabet_list span.disabled").each(function() {
		var link_text = $(this).html();
		var letterID = $(this).attr("id");
		var letter_split = letterID.split("_");
		var letter = letter_split[1];
		
		$(this).after("<a id=\""+ letterID +"\" href=\"#"+ letter +"\" class=\"letter\">" + link_text + "</a>");
		$(this).remove();
	});
}

function filterNotSelected()
{
	//find each hidden list div and disable the letter at the top
	$(".item_letters:hidden").each(function(){
		var listID = $(this).attr("id");
	
		var letter_split = listID.split("_");
		var letter = letter_split[0];
		
		var letterID = "letter_" + letter;
		var link_text = $("#" + letterID).html()

		$("#" + letterID).after("<span id=\"letter_" + letter + "\" class=\"disabled\">" + link_text + "</span>");
		$("#" + letterID).remove();
			
		$("#" + letterID).addClass("disabled");
	});


	//go through each letter at the top, and disable 
	
	
	
	
	
}

function showNonEmpty()
{
	$(".item_letters").show();
	
}

$(document).ready(function(){
	
	$(".supplement_info").hide();
	hideEmptyLetters();
	
	//hide all the descriptions when the page loads, then show the complete list
	$(".category_desc").each(function(){
		$(this).hide();
	})
	
	$("#complete_list").parent().addClass("open");
	$("#complete_list_desc").show();
	
	//make an on click event for the links inside the list
	$("#product_categories li a").each(function() {
		$(this).click(function(){
			var linkId = $(this).attr("id");
			
			//close all of the description boxes
			$(".category_desc").each(function(){
				var descId = $(this).attr("id");
				if(descId != linkId + "_desc")
				{
					$(this).addClass("hidden_desc");
					$(this).hide(65);
				}
			});
			
			//for any description field that is now hidden, remove the open class for the list parent
			$(".hidden_desc").each(function(){
				$(this).parent().removeClass("open");
			})
				
			//now that all of the list items are closed, we can open the list item that was clicked
			var categoryId = $(this).attr("id");
			$("#"+categoryId+"_desc").toggle(65);
			$("#"+categoryId+"_desc").removeClass("hidden_desc");
			$(this).parent().toggleClass("open");
			
			
			//now lets make it so that all the other category of products are hidden in the right column
			
			$("#product_list li:not(." + categoryId + ")" ).hide();
			$("#product_list li:not(." + categoryId + ")" ).parent().parent().hide();
			
			$("#product_list li." + categoryId).show();
			$("#product_list li." + categoryId).parent().parent().show();
			
			reactivateLinks();
			filterNotSelected();
			
			return false;
		});
		
		
	});	
	
	
	$(".letter").click(function(){

		var divOffset = $('#product_list').offset().top;
    	var pOffset = $('#product_list p:eq(2)').offset().top;
    	var pScroll = pOffset - divOffset;
    	$('#product_list').animate({scrollTop: '+=' + pScroll + 'px'}, 1000, 'bounceout');
		return false;
	});
	
	//set the product links to open the appropriate div
	$(".supplement_link").click(function() {
		//hide the supplement info stuff
		
		var id = $(this).attr("id");
		
		
		$("#supplement_wrapper").fadeOut("normal", function(){
			 $("#" + id + "_info").fadeIn();
		});
		
		
		//fade in the tab for this supplement
		
		return false;
	});
	
	$(".back_button").click(function(){
		//hide the detail div
		$(".supplement_info").fadeOut("normal", function(){
			$("#supplement_wrapper").fadeIn();
		});
		
		return false;
	});
	
});

function wrapper_fadein()
{
	
}

function info_fadein(id)
{
	
}




