/* set defaults */
var currentLI = null;
var currentSub = null;
var currentSubLi = null;
var currentSubImg = null;
var defaultState = true;

$(document).ready(function(){
	$("#nutrition").hide();	
	$(".main-arrow").hide();
	$(".sub-arrow").hide();
	//tie click event to all items
	$("li.item").click(function(e){
		_gaq.push(['_trackEvent', "Items", "Click", $(this).text()]);
		getNutrition($(this).attr("id"));
		e.preventDefault();
	});
});

function showSubMenu(itemname){

	if(defaultState){
		//hide the default food images... wait for return
		$("#menu-items").fadeOut("fast", function(){			
			defaultState = false;
			continueShowSubMenu(itemname)
		});
	} else {
		continueShowSubMenu(itemname);
	}
}

function continueShowSubMenu(itemname){

	//hide the current submenu if set
	if(currentSub != null){
		$("#" + currentSub).hide("fast");				
		//clear the active current sub li if set
		if(currentSubLi != null){			
			currentSubLi.removeClass('active-arrow');
			currentSubImg.hide();
		}
	}

	//turn off the nutrition table if on
	$("#nutrition").fadeOut("fast");

	//first turn off the current li	
	if(currentLI){
		currentLI.removeClass("active-arrow");
		$("#" + currentSub + "-arrow").hide();
	}	
	//set the current submenu being displayed
	currentSub = itemname;

	currentLI = $("#main-" + itemname).parent();	
	currentLI.addClass("active-arrow");
	$("#" + itemname + "-arrow").show();
	
	//show the submenu
	$("#" + itemname).show("fast");

}

function getNutrition(item_id){

	if(currentSubLi != null){
		//remove the current active LI
		currentSubLi.removeClass('active-arrow');
		currentSubImg.hide();
	}
	
	currentSubImg = $("#" + item_id + "-arrow");
	currentSubLi = currentSubImg.parent();	
	currentSubLi.addClass('active-arrow');
	currentSubImg.show();
	
	$("#nutrition").fadeOut("fast");
	$.get("/items/view", {id:item_id}, showNutrition, "html");	
	
}

function showNutrition(result){	
	$("#nutrition").html(result);
	$("#nutrition").fadeIn("fast");	
}

