SUBROOT=ROOT + "omniscience/hunger/";					// ROOT previously declared, using it to locate specific folder used
var hungerpic=new Array();
var hungerstage=new Array(7,7,6,6,7,6,7,7);
var pastmeals=new Array();
turn=0;
for (i=0; i<8; ++i)
    // sets up each persons hunger stage, loads pics
	{hungerpic[i]= new Image();
	hungerpic[i].src=SUBROOT + "stage" + i + ".gif";
	pastmeals[i]=0;	
	}
function resetboard()
    // sets the game form up for a new game
	{turn=0;
	hungerstage[0]=hungerstage[1]=hungerstage[2]=hungerstage[4]=hungerstage[7]=7;
	hungerstage[3]=hungerstage[5]=hungerstage[6]=6;	
    // assigns right hunger stage for each person
	for (i=0; i<8; ++i)
    // assigns pics and score for each person
		{pastmeals[i]=0;
		document.getElementById("b" + i).style.visibility = "visible";
		document.getElementById('pc' + i).checked=false; 
		document.getElementById("pp" + i).src=hungerpic[hungerstage[i]].src;
		document.getElementById("pm" + i).innerHTML = "Meals: " + pastmeals[i];
		}	
	document.getElementById("hungerbutton").innerHTML="Feed The Hungry";
	document.getElementById("tot4").innerHTML = "Turn: " + turn;
	document.getElementById("tot1").innerHTML = "62.5 %";
	document.getElementById("tot2").innerHTML = "37.5 %";
	document.getElementById("tot3").innerHTML = "0 %";
	document.getElementById("tot0").innerHTML = "8";
    // sets up all totals
	}
function hungerbutton()
    // handles button click, if game over resets it else process turn
	{if (turn>=6) resetboard();
	else hungerturn();		
	}
function hungerturn()
    // assigns food for 1 turn
	{feed= new Array(0,0,0,0,0,0,0,0);
	mealcount=0;
	for (i=0; i<8; ++i) if (document.getElementById('pc' + i).checked) 
    // sees how many people are fed
		{++feed[i];
		++mealcount;
		}
	if (mealcount!=5)
    // if not feeding exactly 5 people, give error
		{msg="You poor, poor, delusional piece of shit:\n\nNice try, but you can't (nor can anyone else) save the whole fucking\nworld. Like ";
		msg+="a trailer family who traded all their food stamps for Brillo\npads, KY Jelly, Sudafed and cigarettes, someone has to go hungry.\n\n";
		msg+="You can only feed 5 people, so uncheck some boxes.";  	
		if (mealcount<5) msg="You callous and/or illiterate cocksucker:\n\nYou have to check exactly 5 people to receive a meal.";
		alert(msg);
		}

	else
    // if feeding 5, allocates meals and sees how that affects health
		{healthy=hungry=mal=0;
		++turn;
		for (i=0; i<8; ++i)
    // takes feedings and applies it to all people
			{if ((feed[i]==0) || (hungerstage[i]<5))
    // if not feed, sets meals to 0, decreases hungerstage;
				{pastmeals[i]=0;
				if (hungerstage[i]>0) --hungerstage[i];
				}
			else
				{++pastmeals[i];
				if ((hungerstage[i]<7) && (hungerstage[i]>4) && (pastmeals[i]>=2)) ++hungerstage[i]; 
				}
			switch(hungerstage[i])
    // determines total health of all people
				{case 7:	++healthy;						break;
				case 6:	++hungry;						break;
				case 5:	++mal;							break;
				case 4:	document.getElementById("b" + i).style.visibility = "hidden";		break;
				}
			document.getElementById('pc' + i).checked=false; 
			document.getElementById("pp" + i).src=hungerpic[hungerstage[i]].src;
			document.getElementById("pm" + i).innerHTML = "Meals: " + pastmeals[i];
			}
		alive=healthy + hungry + mal;		
		document.getElementById("tot4").innerHTML = "Turn: " + turn;
		document.getElementById("tot1").innerHTML = Math.round((healthy/alive)*100) + " %";
		document.getElementById("tot2").innerHTML = Math.round((hungry/alive)*100) + " %";
		document.getElementById("tot3").innerHTML = Math.round((mal/alive)*100) + " %";
		document.getElementById("tot0").innerHTML = alive;
		if (turn==6)
    // game over, telling them how they did
			{msg="Congratulations, you won. Hunger is a problem that solves itself.  Now be sure to use the form on this page to ";
			msg+="send those misguided bullshit hunger ending programs some insight in how to properly solve the problem.";
			if (alive!=healthy) 
				{msg="Congratulations You Suck.\n\nIn your quest to keep everyone alive, you actually caused more harm than good.\n\n";
				msg+="Because of you and your good intentions the population is worse off than when you started.";
				}
			alert(msg);
			document.getElementById("hungerbutton").innerHTML="Play God Again";
			}
		}
	}
