var curScene = 0;
var alive = true;

//inventory
var dollars = 0;
var sausages = 0;
var vb = 0;
var fosters = 0;
var dog = 0;

//event variables
helpedfisherman = false;
feddog = false;
bearcharged = false;
savedbydog = false;
safepicnic = false;





function replaceNodeText(id, newText)
	{
	var node = document.getElementById(id);
	
	while (node.firstChild)
		{
		node.removeChild(node.firstChild);
		}
	node.appendChild(document.createTextNode(newText));
	}


function writecharsheet(textmsg)
	{
	var node = document.getElementById("charsheet");
	
	while (node.firstChild)
		{
		node.removeChild(node.firstChild);
		}
	node.appendChild(document.createTextNode(textmsg));

	}


function writemsg(textmsg)
	{
	
	var node = document.getElementById("scenetext");
	
	while (node.firstChild)
		{
		node.removeChild(node.firstChild);
		}
	node.appendChild(document.createTextNode(textmsg));

	}


function updatecharsheet()
	{
	var inventory = "";

	if (dollars || sausages || vb || fosters || dog)
		inventory = "You have";
	
	if (dollars)
		inventory = inventory + ", dollars: " + dollars;
	if (sausages)
		inventory = inventory + ", packs of sausages: " + sausages;
	if (vb)
		inventory = inventory + ", sixpacks of VB: " + vb;
	if (fosters)
		inventory = inventory + ", sixpacks of Foster\'s: " + fosters;
	if (dog)
		inventory = inventory + ", dogs following you: " + dog;
		

	writecharsheet(inventory);
	}



function showpaths() //don't call this function with more than one instance of each path
	{
	//turn off all path buttons first
	document.getElementById("north").style.visibility = "hidden";
	document.getElementById("south").style.visibility = "hidden";
	document.getElementById("west").style.visibility = "hidden";
	document.getElementById("east").style.visibility = "hidden";


	var i;
	for (i=0; i < arguments.length; i += 1)
		{
		if (arguments[i] === "n")
			document.getElementById("north").style.visibility = "visible";
		if (arguments[i] === "s")
			document.getElementById("south").style.visibility = "visible";
		if (arguments[i] === "w")
			document.getElementById("west").style.visibility = "visible";
		if (arguments[i] === "e")
			document.getElementById("east").style.visibility = "visible";
		}
	}



function showoptions() //don't call this function with more than one instance of each decision
	{
	//alert("at start of showoptions");

	//turn off all directions buttons first
	document.getElementById("optiona").style.visibility = "hidden";
	document.getElementById("optionb").style.visibility = "hidden";
	document.getElementById("optionc").style.visibility = "hidden";
	document.getElementById("optiond").style.visibility = "hidden";
	document.getElementById("optione").style.visibility = "hidden";
	document.getElementById("optionf").style.visibility = "hidden";


	if(arguments[0])
		{
		replaceNodeText("optiona", arguments[0]);
		document.getElementById("optiona").style.visibility = "visible";
		}
	if(arguments[1])
		{
		replaceNodeText("optionb", arguments[1]);
		document.getElementById("optionb").style.visibility = "visible";
		}
	if(arguments[2])
		{
		replaceNodeText("optionc", arguments[2]);
		document.getElementById("optionc").style.visibility = "visible";
		}
	if(arguments[3])
		{
		replaceNodeText("optiond", arguments[3]);
		document.getElementById("optiond").style.visibility = "visible";
		}
	if(arguments[4])
		{
		replaceNodeText("optione", arguments[4]);
		document.getElementById("optione").style.visibility = "visible";
		}
	if(arguments[5])
		{
		replaceNodeText("optionf", arguments[5]);
		document.getElementById("optionf").style.visibility = "visible";
		}
	
	}





function setupscene()
	{
	//alert("start of setupscene, curScene = " + curScene);

	switch (curScene)
		{
		case 0:
			//replaceNodeText("optiona", "Start Game");
			//showoptions("a");			

			//update image
			document.getElementById("sceneimg").src = "images/scene0.png";
			
			showpaths("n");

			break;
		
		case 1:
			//update text
			writemsg("You are at a path. It goes north and east.");
			
			showoptions("");
			
			//update image
			document.getElementById("sceneimg").src = "images/scene1.png"

			showpaths("n", "e");

			break;
		
		case 2:
			if (!feddog)
				{		
				if (sausages > 0)
					{
					showoptions("Pat dog", "Give your sausages to the dog");
					}
				else
					{
					showoptions("Pat dog");
					}
				//update text
				writemsg("You are at the bottom of a hill. There is a dog here.");
				document.getElementById("sceneimg").src = "images/scene2.png";
				}

			else
				{
				writemsg("You are at the bottom of a hill.");
				showoptions("");
				//update image
				document.getElementById("sceneimg").src = "images/scene15.png";
				}


			

			showpaths("n", "s", "e");
			
			break;

		case 3:
			//update text
			writemsg("There is a bridge here.");
			
			showoptions("Look under the bridge");
	
			//update image
			document.getElementById("sceneimg").src = "images/scene3.png";

			showpaths("n", "w", "e");
			
			break;

		case 4:
			//update text
			writemsg("The is a shop here. It sells beer and sausages.");
			
			showoptions("Buy sausages ($2)", "Buy Victoria Bitter beer ($3)", "Buy Foster\'s beer ($3)", "Sell sausages", "Sell VB ", "Sell Foster\'s");

		
			//update image
			document.getElementById("sceneimg").src = "images/scene4.png";

			showpaths("w");
			
			break;
		
		
		case 5:
			if (!helpedfisherman)
				{			
				//update text
				writemsg("There is a man catching a fish. It is difficult.");
			
				showoptions("Help man");
		
				//update image
				document.getElementById("sceneimg").src = "images/scene5.png";
				}
			else
				{
				writemsg("There is a man fishing by the river. He is happy because he caught a big fish.");
				document.getElementById("sceneimg").src = "images/scene14.png";
				showoptions("");
				}

			showpaths("s", "w");
			
			break;

		
		case 6:
			//update text
			writemsg("There is a dark forest here.");
			showoptions("");
		
			//update image
			document.getElementById("sceneimg").src = "images/scene6.png";
			showpaths("n", "s");
			
			break;

		
		case 7:						
			//update text
			writemsg("There is a big space in the middle of the forest. Some teddy bears are having a picnic.");
			//alert("first picnic message");
			showoptions("Join in picnic");
			
		
			//update image
			if (safepicnic)
				{
				document.getElementById("sceneimg").src = "images/scene11.png";
				}
			else
				{				
				document.getElementById("sceneimg").src = "images/scene7.png";
				}

			showpaths("s");
			
			break;


		}

	}





function relocate(direction)
	{
	//alert ("start of relocate, curScene = " + curScene + " direction = " + direction);
	
	switch (curScene)
		{
		case 0:
			if (direction === "north")
				curScene = 1;
			break;
			
		case 1:			
			if (direction === "north")
				curScene = 2;
			if (direction === "east")
				curScene = 3;
			break;

		case 2:		
			if (direction === "north")
				curScene = 6;
			if (direction === "east")
				curScene = 5;
			if (direction === "south")
				curScene = 1;
			break;
		
		case 3:										
			if (direction === "north")
				curScene = 5;
			if (direction ==="west")
				curScene = 1;
			if (direction === "east")
				curScene = 4;
			break;

		case 4:							
			if (direction === "west")
				curScene = 3;
			break;

		case 5:										
			if (direction === "south")
				curScene = 3;
			if (direction ==="west")
				curScene = 2;
			break;
		case 6:										
			if (direction === "north")
				curScene = 7;
			if (direction ==="south")
				curScene = 2;		
			break;
		case 7:										
			if (direction === "south")
				curScene = 6;
			break;



		}
	
	if(alive)
		setupscene();

	}



function optionhandler(option)
	{
	//alert ("start of interact, curScene = " + curScene + " option = " + option);
	

	switch (curScene)
		{
		case 0:
			break;
			
		case 1:			
			break;

		case 2:
			if (option === "a")
				{
				writemsg("The dog is happy. He looks at you.");
				}

			if (option === "b")
				{
				writemsg("The dog eats the sausages. He starts to follow you.");
				//document.getElementById("sceneimg").src = "images/scene13.png"; //got rid of this scene
				dog = 1;
				sausages -= 1;
				feddog = true;
				showoptions("Pat dog");
				}

			break;
		
		case 3:				
			if (option === "a")
				{
				document.getElementById("sceneimg").src = "images/scene8.png";
				writemsg("A hungry monster jumps out of the water under the bridge! He eats you!");
				showoptions("");
				showpaths("");
				alive = false;				
				}							

			break;

		case 4:
			if (option === "a")
				{
				if (dollars >= 2)
					{
					writemsg("You buy some sausages for 2 dollars.");
					sausages += 1;
					dollars -= 2;
					}
				else
					{
					writemsg("Sausages cost 2 dollars. You need more money.");
					}
				}

			if (option === "b")
				{
				if (dollars >= 3)
					{
					writemsg("You buy a sixpack of VB for 3 dollars.");
					vb += 1;
					dollars -= 3;
					}
				else
					{
					writemsg("Beer costs 3 dollars a sixpack. You need more money.");
					}
				}

			if (option === "c")
				{
				if (dollars >= 3)
					{
					writemsg("You buy a sixpack of Foster\'s for 3 dollars.");
					fosters += 1;
					dollars -= 3;
					}
				else
					{
					writemsg("Beer costs 3 dollars a sixpack. You need more money.");
					}
				}

			if (option === "d")
				{
				if (sausages > 0)
					{
					writemsg("You get 2 dollars back for some sausages.");
					sausages -= 1;
					dollars += 2;
					}
				else
					{
					writemsg("You don't have any sausages to sell!");
					}
				}

			if (option === "e")
				{
				if (vb > 0)
					{
					writemsg("You get 3 dollars back for a sixpack of VB.");
					vb -= 1;
					dollars += 3;
					}
				else
					{
					writemsg("You don\'t have any VB to sell!");
					}
				}

			if (option === "f")
				{
				if (fosters > 0)
					{
					writemsg("You get 3 dollars back for a sixpack of Foster\'s.");
					fosters -= 1;
					dollars += 3;
					}
				else
					{
					writemsg("You don't have any Foster\'s to sell!");
					}
				}

	
			break;
		
		case 5:
			if (option === "a")
				{
				writemsg("You help the man. He is happy. He gives you 5 dollars.");
				dollars += 5;
				helpedfisherman = true;
				showoptions("");
				}
			break;
		
		case 6:
			break;
		
		case 7:
			if (safepicnic)
				{
				if (option === "a")
					{
					if (vb > 0) //win
						{
						writemsg("The teddy bears see your beer. They invite you to join their picnic. What a great afternoon! Congratulations, you were right to go down in the woods today - THE END.");
						document.getElementById("sceneimg").src = "images/scene12.png";
						showoptions ("");
						showpaths ("");
						}
					else
						{
						writemsg("The teddy bears look at you but they don't ask you to join their picnic.");
						showoptions("");
						showpaths ("s");
						}
					}
				else
					{
					showpaths (s);
					}
				}
					
			if (savedbydog && !safepicnic)
				{
				writemsg("There are some teddy bears having a picnic. They are not dangerous.");
				showoptions("Join picnic");
				showpaths("s");
				document.getElementById("sceneimg").src = "images/scene11.png";
				safepicnic = true;				
				}
			
			if (bearcharged && !savedbydog)
				{
				if (dog === 1)
					{
					writemsg("Your dog attacks the bear!");
					document.getElementById("sceneimg").src = "images/scene10.png";
					savedbydog = true;
					dog -= 1;
					showoptions("Yay!");
					showpaths("");
					}
				else
					{
					writemsg("The bear kills you. What a bad afternoon!");
					showoptions("");
					//writecharsheet("");
					alive = false;
					}
				}

			if (!bearcharged)
				{		
				if (option === "a")
					{
					writemsg("A bear attacks you!");
					showpaths("");
					document.getElementById("sceneimg").src = "images/scene9.png";
					showoptions("Oh my God!");
					bearcharged = true;
					}
				}
			
				
			break;			
		}

	if (alive)
		updatecharsheet();

	}





