﻿var StateHistory = new Class(
{
	initialize : function()
	{
		this.active = true;
		this.states = [];
		this.position = -1;
	},

	load : function()
	{
		stateHistory.states.push(site.buildState());
		stateHistory.position = 0;
	},

	sleep : function()
	{
		stateHistory.active = false;
	},

	wakeUp : function()
	{
		stateHistory.active = true;
	},

	back : function()
	{
		if (stateHistory.position > 0)
		{
			stateHistory.position--;

			stateHistory.sendRequest(function()
			{
				if (stateHistory.position == 0)
				{
					//stateHistory.deactivateNavBtn($("btnPreviousState"));
				}

				//stateHistory.activateNavBtn($("btnNextState"));
			});
		}
	},

	forward : function()
	{
		if (stateHistory.position < stateHistory.states.length - 1)
		{
			stateHistory.position++;

			stateHistory.sendRequest(function()
			{
				if (stateHistory.position == stateHistory.states.length - 1)
				{
					//stateHistory.deactivateNavBtn($("btnNextState"));
				}

				//stateHistory.activateNavBtn($("btnPreviousState"));
			});
		}
	},

	sendRequest : function(onFinish)
	{
		stateHistory.sleep();

		UIService.RebuildState(stateHistory.states[stateHistory.position], function(commands)
		{
			ui.processCommands(commands, function()
			{
				stateHistory.wakeUp();
				onFinish();
			});
		});
	},

	activateNavBtn : function(navBtn)
	{
		imgBtn.setStatus("", navBtn, 0, true);
	},

	deactivateNavBtn : function(navBtn)
	{
		imgBtn.setStatus("", navBtn, 3);
	},

	push : function()
	{
		if (stateHistory.position < stateHistory.states.length - 1)
		{
			stateHistory.states = stateHistory.states.slice(0, stateHistory.position + 1);
		}

		stateHistory.states.push(site.buildState());
		stateHistory.position = stateHistory.states.length - 1;

		//stateHistory.activateNavBtn($("btnPreviousState"));
		//stateHistory.deactivateNavBtn($("btnNextState"));
	}
});

var stateHistory = new StateHistory();
