var parser;
var domDoc;
var docRoot;
var foodDoc = null;
var listXSL = null;
var recipeXSL = null;
var foodsXSL = null;
var recipeDoc = null;
var foodListDoc = null;
var nutritionXSL = null;
var nutritionDoc = null;
var foodRowXSL = null;
var foodRowDoc = null;
var foodIndex = 0;
var aXML = new Array();
var xslReq;
var loadin;
var listContainer;
var recipeContainer;
var xmlContainer;
var aTotals;
var initLoggedIn = false;
var userBowl = "";
var foodCount = new Array();
var foodLimit = new Array();
var tipIndex = new Array();
var cabAlertBox;
var cabAlertButton;
var cabAlertMsg;

logging = false;
logging__ = false;
xsltdebug = false;
xpathdebug = false;

function initXML(sLogged) {
	if (sLogged == "yup") {
		initLoggedIn = true;	
	}
	
	foodCount["Meats"] = 0;
	foodCount["Vegetables"] = 0;
	foodCount["Sauces"] = 0;
	foodCount["Starches"] = 0;
	foodLimit["Meats"] = 3;
	foodLimit["Vegetables"] = 10;
	foodLimit["Sauces"] = 3;
	foodLimit["Starches"] = 10;
	
	aTotals = new Array("tc","tf","sf","carb","df","prot");
	
	// This is definitely going into Bosco's badass library o' tricks
//	addFile("foodDoc","/experience/ingredients.xml");
	addFile("foodDoc","/experience/ingredients.php");

	addFile("foodsXSL","/experience/transforms/foodList.xsl");
	addFile("recipeXSL","/experience/transforms/recipeCard.xsl");
	addFile("nutritionXSL","/experience/transforms/recipeNutrition.xsl");
	addFile("foodRowXSL","/experience/transforms/foodRow.xsl");
		
	loadXML();
}

function addFile(sVarName,sFileName) {
	obj = new Object();
	obj.varName = sVarName;
	obj.fileName = sFileName;
	
	aXML.push(obj);
}

function loadXML() {
	if(aXML.length > 0) {
		loadin = aXML.pop();
	
		$.ajax({
			url: loadin.fileName,
			type: 'GET',
			dataType: 'html',
			success: function(xmlData) {
				eval(loadin.varName + ' = xmlData');
				if(aXML.length > 0) {
					loadXML();
				} else {
					initDoc();	
				}
			}
		});
	}
}

function initDoc() {
	domDoc = xmlParse(foodDoc);
	foodListDoc = xmlParse(foodsXSL);
	recipeDoc = xmlParse(recipeXSL);
	nutritionDoc = xmlParse(nutritionXSL);
	foodRowDoc = xmlParse(foodRowXSL);
	
	foodSlider = document.getElementById("foodList");
	menuContainer = document.getElementById("menuContainer");
	cabAlertBox = document.getElementById("alertBox");
	cabAlertButton = document.getElementById("closeBoxButton");
	cabAlertMsg = document.getElementById("alertMsg");

	//now set main menu contents for if they're logged in or not
	document.getElementById("mmContents").innerHTML = (initLoggedIn) ? document.getElementById("mm_loggedin").innerHTML : document.getElementById("mm_loggedout").innerHTML;
	drawDisplay();
}

function drawDisplay() {
	document.getElementById("recipeContents").innerHTML = xsltProcess(domDoc, recipeDoc);
	document.getElementById("bowlNutrition").innerHTML = xsltProcess(domDoc, nutritionDoc);
//	document.getElementById("recipeContents").innerHTML = xsltProcess(bowlDoc, recipeDoc);
	
}


function addFood(id,sCat) {
	
	if (foodCount[sCat] >= foodLimit[sCat] ) {
		switch(sCat) {
			case "Meats":
				popMessage("<span style=\"font-size:18px\">That's a lot of meat!</span><p>As a general rule, try to limit the meat and seafood in your bowl to about 9 ounces or less <br/> (about 3 full tongs).</p>");
				break;
			case "Vegetables":
				popMessage("<span style=\"font-size:18px\">Veggie overload!</span><p>We love vegetables too, but you'll want to limit your vegetables to around 10 ounces worth <br>(our bowls aren't <em>that</em> gigantic)</p>");
				break;
			case "Sauces":
				popMessage("<span style=\"font-size:18px\">Your sauce cup runneth over!</span><p>Our sauce cups hold about 3 ounces, so up to 3 ounces of your favorite sauces should do it</p>");
				break;
		}
	} else {
		foodCount[sCat]++;
	
		var expr = xpathParse("//food[@id=" + id + "]");
		var nodez = expr.evaluate(new ExprContext(domDoc));
		var aFoods = nodez.nodeSetValue();
	
	
		var cnt = aFoods[0].getAttribute("num");
		cnt++;
		aFoods[0].setAttribute("num",cnt);
		
		aFoods[0].setAttribute("sorty",foodIndex);
		foodIndex++;
		
		var aTotal = domDoc.getElementsByTagName("totals");
		
		for(x in aTotals) {
			fTotal = parseFloat(Number(aFoods[0].getAttribute(aTotals[x])).toFixed(2));
			rTotal = parseFloat(Number(aTotal[0].getAttribute(aTotals[x])).toFixed(2));
			
			tot = fTotal + rTotal;
	
			aTotal[0].setAttribute(aTotals[x],parseFloat(tot.toFixed(2)));
		}
	
	
		document.getElementById("food_" + id).innerHTML = xsltProcess(xmlParse(xmlText(aFoods[0])), foodRowDoc);
	//	updateFoodRow(id,xmlText(aFoods[0]));
	//	drawFoods(sCat);
		drawDisplay();
	}
}

function removeFood(id,sCat) {
	foodCount[sCat]--;

	var expr = xpathParse("//food[@id=" + id + "]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();
	
	var cnt = aFoods[0].getAttribute("num");
	cnt--;
	if(cnt < 0) {
		cnt = 0;
		aFoods[0].setAttribute("sorty",0);
	}
	
	aFoods[0].setAttribute("num",cnt);

	var aTotal = domDoc.getElementsByTagName("totals");
	
	for(x in aTotals) {
		fTotal = parseFloat(Number(aFoods[0].getAttribute(aTotals[x])).toFixed(2));
		rTotal = parseFloat(Number(aTotal[0].getAttribute(aTotals[x])).toFixed(2));
		
		tot = rTotal - fTotal;

		aTotal[0].setAttribute(aTotals[x],parseFloat(tot.toFixed(2)));
	}

	if(dv = document.getElementById("food_" + id)) {
		dv.innerHTML = xsltProcess(xmlParse(xmlText(aFoods[0])), foodRowDoc);
	}


//	drawFoods(sCat);
	drawDisplay();
}

function updateFoodRow(id,fNode) {
	if(dv = document.getElementById("food_" + id)) {
		dv.innerHTML = xsltProcess(domDoc, foodRowDoc);
	}
}

function xmlDisplay() {
	rLT = /</g;	
	rGT = />/g;
	
	docString = new String(xmlText(domDoc));
	
	xmlContainer.innerHTML = "<pre>" + docString.replace(rLT,"&lt;") + "</pre>";
}

function foodCat(sCat) {
	document.getElementById("tipContent").innerHTML = document.getElementById("tips_" + sCat).innerHTML;
	drawFoods(sCat);
	slideMainMenu("foods");
}

function drawFoods(sCat) {
	var expr = xpathParse("//food[@cat='" + sCat + "']");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();

//	alert("string: " + nodez.stringValue());
	
	var sXML = "<root><cat>" + sCat + "</cat>";
	for(i=0;i<aFoods.length;i++) {
		sXML += xmlText(aFoods[i]);
	}

	var xml = xmlParse(sXML + "</root>");

	var html = xsltProcess(xml, foodListDoc);
	
//	alert(html);

	document.getElementById("foodList").innerHTML = html;
}


function changeTips(dest,sDiv) {
	if(dest < 0) {
		tipIndex.push(sDiv);
	} else {
		tipIndex.pop();
		if (tipIndex.length <= 0) {
			sDiv = (userBowl == "") ? "welcome" : "main";
		} else {
			sDiv = tipIndex[(tipIndex.length - 1)];
		}
	}

	var tip = null;

	switch(sDiv) {
		case "foods_Meats":
		case "foods_Vegetables":
		case "foods_Sauces":
		case "foods_Spices":
			tip = "tips_" + String(sDiv).substr(6);	
			break;
			
		case "signature_list":
			tip = $("#sigTips").val();
			break;
		
		default:
			if(document.getElementById("tips_" + sDiv)) {
				tip = "tips_" + sDiv;	
			} else {
				$("#tipContent").html(sDiv);
			}
			
			break;	
	}
	
	if(tip != null) {
		document.getElementById("tipContent").innerHTML = document.getElementById(tip).innerHTML;
	}
	
	$("#tipContent a").lightBox();
}

////////////////////////////////////////////
//  User Accounts

function showErrorMsg(sDiv,sMsg) {
	errBox = document.getElementById(sDiv)
	errBox.innerHTML = sMsg;
	errBox.style.visibility = "visible";	
}

function closeErrorMsg(sDiv) {
	errBox = document.getElementById(sDiv)
	if(errBox.style.visibility == "visible") {
		errBox.innerHTML = "";
		errBox.style.visibility = "hidden";
	}
}

function createAccount() {

	var caFlds = new Array("userNameCreate","userEmailCreate","userPassCreate","userPassConfirm");
	for (x in caFlds) {
		if (document.getElementById(caFlds[x]).value == "") {
			showErrorMsg("err_createAccount","You must fill out all 4 fields to create your account");
			return;
		}
	}

	if ($("#userPassCreate").val() != $("#userPassConfirm").val()) {
		showErrorMsg("err_createAccount","Your password entries do not match");
		return;
	}
	
	$.ajax({
		url: "/experience/doCabAccountCreate.php",
		type: 'POST',
		data: {username: $("#userNameCreate").val(), userpass: $("#userPassCreate").val(), useremail: $("#userEmailCreate").val()  },
		dataType: 'html',
		success: function(data) {
			sReturno = String(data);			
			if (sReturno.substr(0,7) == "WELCOME") {
				$("#loginName").html(sReturno + ' | <a class="redLink" href="javascript:logout()">LOG OUT</a>');
	
				if ((foodCount["Meats"] + foodCount["Vegetables"] + foodCount["Sauces"]) > 0) {
					$("#mmContents").html($("#mm_loggedin_bowl").html());
				} else {
					$("#mmContents").html($("#mm_loggedin").html());
				}
				
				closeErrorMsg("err_createAccount");
				slideMainMenu("createAccount");	
			} else {
				showErrorMsg("err_createAccount",sReturno);
			}
		}
	});
}

function sendLogin() {
	$.ajax({
		url: "/experience/doCabLogin.php",
		type: 'POST',
		data: { username: $("#userName").val(), userpass: $("#userPass").val() },
		dataType: 'html',
		success: function(data) {
			sReturno = String(data);
			
			if (sReturno.substr(0,7) == "WELCOME") {
				document.getElementById("loginName").innerHTML = sReturno + ' | <a class="redLink" href="javascript:logout()">LOG OUT</a>';

				if ((foodCount["Meats"] + foodCount["Vegetables"] + foodCount["Sauces"]) > 0) {
					document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin_bowl").innerHTML;
				} else {
					document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin").innerHTML;
				}
				
				initLoggedIn = true;

				closeErrorMsg("err_login");
				slideMainMenu("loginMenu");
			} else {
				showErrorMsg("err_login",sReturno);
			}
		}
	});
}

function forgotPass() {
	$.ajax({
		url: "/experience/doForgotPassword.php",
		type: 'POST',
		data: { username: $("#forgotPassEmail").val() },
		dataType: 'html',
		success: function(data) {
			if(data == "sent") {
				closeErrorMsg("err_forgotpass");
				
				popMessage("You password has been sent to your email account");

				slideMainMenu("forgotPass");
			} else {
				showErrorMsg("err_forgotpass",xslReq.responseText);
			}
		}
	});
}

function logout() {
	$.ajax({
		url: "/experience/doCabLogout.php",
		type: 'POST',
		dataType: 'html'
	});
	
	initLoggedIn = false;

	if ((foodCount["Meats"] + foodCount["Vegetables"] + foodCount["Sauces"]) > 0) {
		$("#mmContents").html($("#mm_loggedout_bowl").html());
	} else {
		$("#mmContents").html($("#mm_loggedout").html());
	}
	$("#loginName").html("YOU ARE NOT LOGGED IN");
}

//////////////////////////////////////
// Loading, Saving, and Fabulous Printing of Recipes

function nameBowl() {
	userBowl = $("#bowlName2").val();
	$("#recipeName").html(userBowl);
	$("#bowlName").val(userBowl);
	
	$("#foods_Meats").css("left","276px");
	tipIndex.pop();
	$("#foods_Vegetables").css("left","276px");
	tipIndex.pop();
	$("#foods_Sauces").css("left","276px");
	tipIndex.pop();
	$("#foods_Spices").css("left","276px");
	tipIndex.pop();
	
	if (initLoggedIn) {
		$("#mmContents").html($("#mm_loggedin_bowl").html());
	} else {
		$("#mmContents").html($("#mm_loggedout_bowl").html());
	}
	
	slideMainMenu('nameMenu');

}

function newBowl() {
	resetBowl();	
	drawDisplay();
	$("#recipeName").html(":: Original bd's Bowl Creation ::");
	$("#bowlName").val("");
	$("#bowlName2").val("");
	userBowl = "";
	
	slideMainMenu('foods_Meats');
}

function userBowlXML() {
	var expr = xpathParse("//food[@num>0]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();

	var sXML = "<root><bowlname>" + $("#bowlName").val() + "</bowlname>";
	for(i=0;i<aFoods.length;i++) {
		sXML += xmlText(aFoods[i]);
	}

	var expr = xpathParse("//totals");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aTots = nodez.nodeSetValue();
	sXML += xmlText(aTots[0]);
	
	sXML += "</root>";

	return sXML;
}

function printBowl() {
	sXML = userBowlXML();

	$.ajax({
		url: "/experience/doPrintBowl.php",
		type: 'POST',
		data: { bname: $("#bowlName").val(), bowl: sXML },
		dataType: 'html',
		success: function(data) {
			winRecipe = window.open("recipe.html","printRecipe","width=620,height=750,scrollbars=auto");
		}
	});
		
	userBowl = $("#bowlName").val();
}

function sendEmail() {
	$("#sendinButton").attr("disabled","disabled");

	sXML = userBowlXML();

	$.ajax({
		url: "/experience/doEmailBowl.php",
		type: 'POST',
		data: { toaddy: $("#sendToEmail").val(), bname: $("#bowlName").val(), emailMsg: $("#emailMsg").val(), bowl: sXML},
		dataType: 'html',
		success: function(data) {
			popMessage(data);

			//this condition added due to camron's constant nagging
			if(data != "You must be logged in to send an email") {
				slideMainMenu('sendEmail');
				$("#sendToEmail").val("");
				$("#emailMsg").val("");
			}
			
			$("#sendinButton").removeAttr("disabled");
		}
	});
	
	userBowl = $("#bowlName").val();
}

function saveBowl() {
	popStatus("saving");
	// dump only the food nodes that are used into a string
	var expr = xpathParse("//food[@num>0]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();

	var sXML = "<root><bowlname>" + $("#bowlName").val() + "</bowlname>";
	for(i=0;i<aFoods.length;i++) {
		sXML += xmlText(aFoods[i]);
	}
	
	sXML += "</root>";

	$.ajax({
		url: "/experience/doSaveBowl.php",
		type: 'POST',
		data: { bname: $("#bowlName").val(), bowl: sXML},
		dataType: 'html',
		success: function(data) {
			if (data == "toomanyrecipes") {
				closeStatus();
				popMessage("You have reached your limit of 10 saved recipes.");	
			} else {
				$("#recipeName").html(":: " + userBowl + " ::");
				closeStatus();
				slideMainMenu("saveMenu");
			}
		}
	});
}


function listSavedBowls() {
	$.ajax({
		url: "/experience/bowlLoader.php",
		type: 'POST',
		data: { accion: "list"},
		dataType: 'html',
		success: function(data) {
			if (data == "timedout") {
				closeStatus();
				slideMainMenu("loginMenu");
				logout();
			} else {
				$("#recipeList").html(data);
				closeStatus();
				slideMainMenu("savedList");
			}
		}
	});

	popStatus("loading");
}

function loadRecipe(iKey) {
	$.ajax({
		url: "/experience/bowlLoader.php",
		type: 'POST',
		data: { accion: "load", key: iKey},
		dataType: 'html',
		success: function(data) {
			if (data == "timedout") {
				slideMainMenu("loginMenu");
				logout();
			} else {
				resetBowl("ih8oil");
				
				var svBowl = xmlParse(data);
				var aIngs = svBowl.getElementsByTagName("food");
				var aTotal = domDoc.getElementsByTagName("totals");
				
				for (n in aIngs) {
					fId = aIngs[n].getAttribute("id");

					var expr = xpathParse("//food[@id='" + fId + "']");
					var nodez = expr.evaluate(new ExprContext(domDoc));
					var aFoods = nodez.nodeSetValue();
					aFoods[0].setAttribute("num",aIngs[n].getAttribute("num"));

					for(x in aTotals) {
						fTotal = parseFloat(Number(aFoods[0].getAttribute(aTotals[x])).toFixed(2)) * parseInt(aIngs[n].getAttribute("num"));
						rTotal = parseFloat(Number(aTotal[0].getAttribute(aTotals[x])).toFixed(2));
						
						tot = fTotal + rTotal;
				
						aTotal[0].setAttribute(aTotals[x],parseFloat(tot.toFixed(2)));
					}

					foodCount[aIngs[n].getAttribute("cat")]++;
					$("#food_" + fId).html(xsltProcess(xmlParse(xmlText(aIngs[n])), foodRowDoc));
				}
				
				drawDisplay();

				//now handle the damn bowl name
				aIngs = svBowl.getElementsByTagName("bowlname");
				bName = xmlValue(aIngs[0]);
				userBowl = bName;
				$("#bowlName").val(bName);
				$("#bowlName2").val(bName);
				$("#recipeName").html(":: " + bName + " ::");
				$("#mmContents").html($("#mm_loggedin_bowl").html());
				
				closeStatus();
				slideMainMenu("savedList");
			}
		}
	});
	
	popStatus("loading");
}


function resetBowl(bDefault) {
	var aTotal = domDoc.getElementsByTagName("totals");
	for(x in aTotals) {
		aTotal[0].setAttribute(aTotals[x],0);
	}

	var expr = xpathParse("//food[@num>0]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();
	for (x in aFoods) {
		aFoods[x].setAttribute("num",0);
		$("#food_" + aFoods[x].getAttribute("id")).html(xsltProcess(xmlParse(xmlText(aFoods[x])), foodRowDoc));

	}

	//Last minute hack to support Sesame Vegtable Oil as a default ingredient
	if (bDefault == null) {
		var expr = xpathParse("//food[@id=51]");
		var nodez = expr.evaluate(new ExprContext(domDoc));
		var aFoods = nodez.nodeSetValue();
		aFoods[0].setAttribute("num",1);
		$("#food_51").html(xsltProcess(xmlParse(xmlText(aFoods[0])), foodRowDoc));
		//now adjust totals
		for(x in aTotals) {
			aTotal[0].setAttribute(aTotals[x],aFoods[0].getAttribute(aTotals[x]));
		}
	}
	
	foodCount["Meats"] = 0;
	foodCount["Vegetables"] = 0;
	foodCount["Sauces"] = 0;
	foodCount["Starches"] = 0;

}

function popMessage(sMsg) {
//	alert("poppin");
	cabAlertMsg.innerHTML = sMsg;
	cabAlertBox.style.display = "inline";
}

function closeBox() {
	cabAlertBox.style.display = "none";
	cabAlertMsg.innerHTML = "";
}

function popStatus(sStatus) {
	switch (sStatus) {
		case "loading":
			cabAlertMsg.innerHTML = "<div class=\"cabLoading\"></div>";
			break;
		case "saving":
			cabAlertMsg.innerHTML = "<div class=\"cabSaving\"></div>";
			break;
	}
	
	cabAlertButton.style.display = "none";
	cabAlertBox.style.display = "inline";
}

function closeStatus() {
	cabAlertBox.style.display = "none";
	cabAlertButton.style.display = "inline";
	cabAlertMsg.innerHTML = "";
}

function deleteRecipe(iKey) {
	if (confirm("Are you sure you want to delete this recipe?")) {
		$.ajax({
			url: "/experience/doDeleteRecipe.php",
			type: 'POST',
			data: { key: iKey},
			dataType: 'html',
			success: function(data) {
				popMessage(data);
				slideMainMenu("savedList");
			}
		});		
	}
}


//////////////////////////////////////////////
//  Signature Recipes

function getSignatureList(sCat) {
	$.ajax({
		url: "/experience/bowlLoader.php",
		type: 'POST',
		data: { accion: sCat},
		dataType: 'html',
		success: function(data) {
			if (data == "timedout") {
				closeStatus();
				slideMainMenu("loginMenu");
				logout();
			} else {
				$("#signature_list").html(data);
				closeStatus();
				slideMainMenu("signature_list");
			}
		}
	});

	popStatus("loading");
	
}

function loadSignatureRecipe(iKey) {
	$.ajax({
		url: "/experience/bowlLoader.php",
		type: 'POST',
		data: { accion: "load_signature", key: iKey},
		dataType: 'html',
		success: function(data) {
			if (data == "timedout") {
				slideMainMenu("loginMenu");
				logout();
			} else {
				resetBowl("ih8oil");
				
				var svBowl = xmlParse(data);
				var aIngs = svBowl.getElementsByTagName("food");
				var aTotal = domDoc.getElementsByTagName("totals");
				
				for (n in aIngs) {
					fId = aIngs[n].getAttribute("id");

					var expr = xpathParse("//food[@id='" + fId + "']");
					var nodez = expr.evaluate(new ExprContext(domDoc));
					var aFoods = nodez.nodeSetValue();
					aFoods[0].setAttribute("num",aIngs[n].getAttribute("num"));

					for(x in aTotals) {
						fTotal = parseFloat(Number(aFoods[0].getAttribute(aTotals[x])).toFixed(2)) * parseInt(aIngs[n].getAttribute("num"));
						rTotal = parseFloat(Number(aTotal[0].getAttribute(aTotals[x])).toFixed(2));
						
						tot = fTotal + rTotal;
				
						aTotal[0].setAttribute(aTotals[x],parseFloat(tot.toFixed(2)));
					}

					foodCount[aIngs[n].getAttribute("cat")]++;
					$("#food_" + fId).html(xsltProcess(xmlParse(xmlText(aIngs[n])), foodRowDoc));
				}
				
				drawDisplay();

				//now handle the damn bowl name
				aIngs = svBowl.getElementsByTagName("bowlname");
				bName = xmlValue(aIngs[0]);
				userBowl = bName;
				$("#bowlName").val(bName);
				$("#bowlName2").val(bName);
				$("#recipeName").html(":: " + bName + " ::");
				$("#mmContents").html($("#mm_loggedin_bowl").html());
				
				//draw new tip
				nodeTip = svBowl.getElementsByTagName("description");
				$("#tipContent").html(xmlValue(nodeTip[0]));
				
				aImg = svBowl.getElementsByTagName("image_thumb");
				if(aImg.length > 0) {
					aBigImg = svBowl.getElementsByTagName("image_full");
					$("#tipContent").prepend("<a href=\"/experience/recipeImages/" + xmlValue(aBigImg[0]) + "\"><img src=\"/experience/recipeImages/" + xmlValue(aImg[0]) + "\" class=\"recipeThumb\" style=\"margin-right:10px;\" /></a>");
					
				}
				
				closeStatus();
				
				//slide back menus
				$("#signature_recipes").css("left","276px");
				tipIndex.pop();
				tipIndex.pop();
				
				sBowlDescription = $("#tipContent").html();
				//tipIndex.push($("#tipContent").html());
				//tipIndex.push("<br />");  // dummy tip that will get popped

				slideMainMenu('signature_list');
				
				$("#tipContent").html(sBowlDescription);
				$("#tipContent a").lightBox();
			}
		}
	});
	
	popStatus("loading");
}
/////////////////////////////////
// debugging
function reportXML() {
	alert(xmlText(domDoc));	
}

////////////////////////////////////////////////////
// rollovers

function foodOver(el) {
	el.className = "foodRowOver";	
}

function foodOut(el) {
	el.className = "foodRow";	
}

function foodSelectedOut(el) {
	el.className = "foodRowSelected";	
}

////////////////////////////////////////////////////
//animation functions
var a = null;
var b = null;
var c = null;
var slider = null;
var infoSlider = null;
var infoCat = null;
var openSlider = null;
var foodSlider = null;
var menuContainer = null;
var tmpMenu = null;

//function slideMainMenu(sDiv) {
//	if($("#menuContainer").height() > 540) {
//		slideMenuUp(sDiv);
//		return;			
//	}
//	
//	slider = $("#" + sDiv);
//	var dest = (parseInt(slider.css("left")) > -3) ? -3 : 276;
//		
//	slider.animate({left: dest + "px"}, 600);
//	changeTips(dest,sDiv);
//}

function slideMainMenu(sDiv) {
	if (a != null) {
		return;
//		a.stop();
//		a = null;
	}

	if(parseInt(menuContainer.style.height) > 590) {
		slideMenuUp(sDiv);
		return;
	}

	slider = document.getElementById(sDiv);

	var dest = parseInt(slider.style.left) > -3 ? -3 : 276;

	a = new Accelimation(slider.style, "left", dest, 600, .8, "px");
	a.onend = mmEnd;
	a.onframe = mmSlide;
	a.start();
	
	changeTips(dest,sDiv);
}

function mmSlide(x) {
	slider.style.left = x + "px";
}

function mmEnd() {
	a.stop();
	a = null;
	slider = null;
	openSlider = null;
}

//function slideNutriton(sDiv,sCat) {
//	infoSlider = $("#" + sDiv);
//	infoCat = $("#" + sCat + "_guts");	
//	
//	
//	var dest = (infoSlider.height() > 0) ? 0 : document.getElementById(sDiv).scrollHeight + 10;
//
//	infoSlider.animate({height: dest + "px"}, 400,"swing",function() {
//		if(dest <= 0) {
//			if($("#menuContainer").height() > 540 && infoCat.outerHeight() <= 540) {
//				slideMenuUp(null);	
//			}
//		}
//	});
//}

function slideNutriton(sDiv,sCat) {
	if (b != null) {
		return;
//		a.stop();
//		a = null;
	}

	infoSlider = document.getElementById(sDiv);
	infoCat = document.getElementById(sCat + "_guts");

//	var dest = parseInt(infoSlider.style.height) > 0 ? 0 : 120;
	var dest = parseInt(infoSlider.style.height) > 0 ? 0 : infoSlider.scrollHeight + 10;

	b = new Accelimation(infoSlider.style, "height", dest, 400, .8, "px");
	b.onend = (dest > 0) ? infoEndDown : infoEndUp;
	b.onframe = infoSlide;
	b.start();
}

function infoSlide(x) {
	infoSlider.style.height = x + "px";
	infoHeight = infoCat.offsetHeight;
	if(infoHeight > 540) {
		menuContainer.style.height = infoHeight + "px";	
	}
}

function infoEndDown() {
//	alert("div: " + infoCat.id + "\noffsetHeight: " + infoCat.offsetHeight)
	b.stop();
	b = null;
	infoSlider = null;
}

function infoEndUp() {
	b.stop();
	b = null;
	infoSlider = null;
	
	if(parseInt(menuContainer.style.height) > 540 && infoCat.offsetHeight <= 590) {
		slideMenuUp(null);	
	}
}

//function slideMenuUp(sDiv) {
//	if(sDiv != null) {
//		tmpMenu = $("#" + sDiv);	
//	} else {
//		tmpMenu = null;
//	}
//	
//	$("#menuContainer").animate({"height" : "540px"}, 200, "swing", function() {
//		if(tmpMenu != null) {
//			slideMainMenu(tmpMenu);
//			tmpMenu = null;
//		}
//	});
//																		
//}

function slideMenuUp(sDiv) {
	if (c != null) {
		return;
//		a.stop();
//		a = null;
	}
	
	if(sDiv != null) {
		tmpMenu = sDiv;	
	}

	c = new Accelimation(menuContainer.style, "height", 590, 200, .5, "px");
	c.onend = mcEnd;
	c.onframe = mcSlide;
	c.start();

}

function mcSlide(x) {
	menuContainer.style.height = x + "px";
}

function mcEnd() {
	c.stop();
	c = null;

	if(tmpMenu != null) {
		slideMainMenu(tmpMenu);
		tmpMenu = null;
	}
}

