extern function main()
{
	
	var state = WMLBrowser.getVar("state");
	var action = WMLBrowser.getVar("action");
	var image = WMLBrowser.getVar("image");
	var remainingTime = Lang.parseInt(WMLBrowser.getVar("remainingTime"));
	var done = 0;
	var useanimation = 1;
	var nexturl = "invalid.wml";
	
	//********begin debug code
	Console.println("********begin fsm*********");
	Console.println("State:" + state);
	Console.println("Action:" + action);
	Console.println("Image:" + image);
	Console.println("Time:" + remainingTime);
	//********end debug code

	if (action == "timer") {			

		// reduce remaining time. Once it becomes zero it is
		// no longer animation. Time to move to next state		
		remainingTime = remainingTime - 2;
		WMLBrowser.setVar("remainingTime", remainingTime);

		if (remainingTime > 0) {		

			//If animation is on, replace image
			if (useanimation == 1)  {

				// happy
				if (image == "happy1.bmp") {
					Console.println("setVar: happy2.bmp");
					WMLBrowser.setVar("image","happy2.bmp");
				}
				else if (image == "happy2.bmp") {
					Console.println("setVar: happy1.bmp");
					WMLBrowser.setVar("image","happy1.bmp");
				}

				// hungry
				if (image == "hungry1.bmp") {
					Console.println("Changing to hungry2");
					WMLBrowser.setVar("image","hungry2.bmp");
				}
				else if (image == "hungry2.bmp") {
					Console.println("Changing to hungry1");
					WMLBrowser.setVar("image","hungry1.bmp");
				}

				// play
				if (image == "upleft1.bmp") {
					WMLBrowser.setVar("image","upleft2.bmp");
				}
				else if (image == "upleft2.bmp") {
					WMLBrowser.setVar("image","dwright1.bmp");
				}
				if (image == "dwright1.bmp") {
					WMLBrowser.setVar("image","dwright2.bmp");
				}
				else if (image == "dwright2.bmp") {
					WMLBrowser.setVar("image","upleft1.bmp");
				}

				// sick
				if (image == "sick1.bmp") {
					WMLBrowser.setVar("image","sick2.bmp");
				}
				else if (image == "sick2.bmp") {
					WMLBrowser.setVar("image","sick1.bmp");
				}

				// cleanup
				if (image == "kaki1.bmp") {
					WMLBrowser.setVar("image","kaki2.bmp");
				}
				else if (image == "kaki2.bmp") {
					WMLBrowser.setVar("image","kaki1.bmp");
				}

				// dead
				if (image == "sleep1.bmp") {
					WMLBrowser.setVar("image","sleep2.bmp");
				}
				else if (image == "sleep2.bmp") {
					WMLBrowser.setVar("image","sleep1.bmp");
				}

				// healthy - not used in current finite state machine
				if (image == "health1.bmp") {
					WMLBrowser.setVar("image","health2.bmp");
				}
				else if (image == "health2.bmp") {
					WMLBrowser.setVar("image","health1.bmp");
				}

				Console.println("after all elses in main()");

				//********begin debug code
				Console.println("***********animation refresh***********");
				image = WMLBrowser.getVar("image");
				Console.println("Image:" + image);
				remainingTime = WMLBrowser.getVar("remainingTime");
				Console.println("Remaining Time:" + remainingTime);
				//********end debug code

				done = 1;
				//Console.println("******done*******");
				WMLBrowser.refresh();
				Console.println("WMLBrowser.getCurrentCard:" + WMLBrowser.getCurrentCard());
				//WMLBrowser.go(WMLBrowser.getCurrentCard());

			} // useAnimation==1

		} // remaining Time > 0
		
	} // action==timer
	
	if (done == 0) {		
		///////////////////////////////////////////////////////////////			
		// If we are here, it wasn't a timer action or 
		// if wasn't just a animation refresh, so ...
		// move to next state based on Finite state machine

		Console.println("***********state change***********");

		// all states
		if (action == "reset") {
			nexturl = "happyinit.wml";
		}
		//***need to add pause/resume here
		
		// happy
		if (state == "happy") {
			if (action == "timer") {
				nexturl = "hungryinit.wml";
			}
			if (action == "play") {
				nexturl = "playinit.wml";
			}
		}
		
		// hungry
		if (state == "hungry") {
			if (action == "feed") {
				nexturl = "happyinit.wml";
			}
			if (action == "timer") {
				Console.println("going to sick.wml");
				nexturl = "sickinit.wml";
			}
		}

		// play
		if ( state == "play" ) {
			if (action == "rest") {
				nexturl = "happyinit.wml";
			}
			if (action == "timer") {
				nexturl = "hungryinit.wml";
			}
		}

		//sick
		if (state=="sick") {
			if (action == "treat") {
				nexturl = "happyinit.wml";
			}
			if (action == "timer") {
				nexturl = "deadinit.wml";
			}
		}

		//dead
		if (state=="dead") {
			if (action == "reset") {
				nexturl = "happyinit.wml";
			}
			if (action == "timer") {
				nexturl = "happyinit.wml";
			}
		}

		//********begin debug code
		Console.println("********end fsm*********");
		Console.println("nexurl:" + nexturl);
		//********end debug code

		WMLBrowser.go(nexturl);

	}
	Console.println("done");
}	
