
StateManager.prototype.vars = null;
StateManager.prototype.observerInterval = null;

function StateManager()
{
	this.vars = new Array();
	this.actions = new Array();
}

StateManager.prototype.startObserving = function()
{
	this.observerInterval = window.setInterval(_bind(this, this.observe), 50);
}

StateManager.prototype.observe = function()
{
	var newVars = new Array();

	var hash = window.location.hash.toString();
	if(hash != '' && hash != '#')
	{
		var newVars = hash.substr(1).split('&');
		for(var i = 0; i < newVars.length; i++) newVars[i] = newVars[i].split('=');
	}

	for(var i = 0; i < this.vars.length; i++)
	{
		var found = false;
		for(var j = 0; j < newVars.length; j++)
		{
			if(newVars[j][0] == this.vars[i][0])
			{
				if(newVars[j][1] != this.vars[i][1] && this.actions[this.vars[i][0]].active)
				{
					this.vars[i][1] = newVars[j][1];
					if(this.actions[this.vars[i][0]] != null) this.actions[this.vars[i][0]].onChange();
				}
				
				newVars.splice(j, 1);
				found = true;
				break;
			}
		}

		if(!found)
		{
			var name = this.vars[i][0];
			this.vars.splice(i, 1);
			i--;
			if(this.actions[name] != null && this.actions[name].active) this.actions[name].onRemove();
		}
	}

	for(var i = 0; i < newVars.length; i++)
	{
		if(this.actions[newVars[i][0]] != null && this.actions[newVars[i][0]].active)
		{
			this.vars.push([newVars[i][0], newVars[i][1]]);
			if(this.actions[newVars[i][0]] != null) this.actions[newVars[i][0]].onCreate();
		}
	}
}

StateManager.prototype.defineActions = function(name, onCreate, onChange, onRemove)
{
	this.actions[name] = new SM_Var(onCreate, onChange, onRemove);
}

SM_Var.prototype.active = null;
SM_Var.prototype.onCreate = null;
SM_Var.prototype.onChange = null;
SM_Var.prototype.onRemove = null;

function SM_Var(onCreate, onChange, onRemove)
{
	this.active = false;
	this.onCreate = onCreate;
	this.onChange = onChange;
	this.onRemove = onRemove;
}

SM_Var.prototype.enable = function() { this.active = true; }
SM_Var.prototype.disable = function() { this.active = false; }

function init_state_mgr()
{
	if(msie() && _$('__state_mgr_frame') == null)
	{
		if(_get('r') == null) _set_local('r', _rand());
		var ifr = document.createElement('iframe');
		ifr.setAttribute('id', '__state_mgr_frame');
		ifr.setAttribute('src', 'hist.html?r=' + _get('r') + window.location.hash.toString());
		ifr.style.display = 'none';
		document.body.appendChild(ifr);
	}
}

function _set(hvar, value)
{
	vars = new Array();
	hash = window.location.hash.toString();
	var found = false;
	if(hash != '' && hash != '#')
	{
		vars = hash.substr(1).split('&');
		for(var i = 0; i < vars.length; i++)
		{
			vars[i] = vars[i].split('=');
			if(vars[i][0] == hvar)
			{
				vars[i][1] = value;
				found = true;
			}
		}
	}

	if(!found) vars.push(new Array(hvar, value));

	var new_hash = '#';
	for(var i =  0; i < vars.length; i++) new_hash += (i > 0 ? '&' : '') + vars[i][0] + '=' + vars[i][1];

	window.location = new_hash;

	if(msie())
	{
		_set_local('r', _rand());
		_$('__state_mgr_frame').src = 'hist.html?r=' + _get('r') + window.location.hash.toString();
	}
}

function _set_local(hvar, value)
{
	vars = new Array();
	hash = window.location.hash.toString();
	var found = false;
	if(hash != '' && hash != '#')
	{
		vars = hash.substr(1).split('&');
		for(var i = 0; i < vars.length; i++)
		{
			vars[i] = vars[i].split('=');
			if(vars[i][0] == hvar)
			{
				vars[i][1] = value;
				found = true;
			}
		}
	}

	if(!found) vars.push(new Array(hvar, value));

	var new_hash = '#';
	for(var i =  0; i < vars.length; i++) new_hash += (i > 0 ? '&' : '') + vars[i][0] + '=' + vars[i][1];

	window.location = new_hash;
}

function _get(hvar)
{
	hash = window.location.hash.toString();
	if(hash != '' && hash != '#')
	{
		vars = hash.substr(1).split('&');
		for(var i = 0; i < vars.length; i++)
		{
			vars[i] = vars[i].split('=');
			if(vars[i][0] == hvar) return vars[i][1];
		}
	}

	return null;
}

function _get_from(hash, hvar)
{
	if(hash != '' && hash != '#')
	{
		vars = hash.substr(1).split('&');
		for(var i = 0; i < vars.length; i++)
		{
			vars[i] = vars[i].split('=');
			if(vars[i][0] == hvar) return vars[i][1];
		}
	}

	return null;
}

function _remove(hvar)
{
	vars = new Array();
	hash = window.location.hash.toString();
	if(hash != '' && hash != '#')
	{
		vars = hash.substr(1).split('&');
		for(var i = 0; i < vars.length; i++)
		{
			vars[i] = vars[i].split('=');
			if(vars[i][0] == hvar)
			{
				vars.splice(i, 1);
				i--;
			}
		}
	}

	var new_hash = '#';
	for(var i =  0; i < vars.length; i++) new_hash += (i > 0 ? '&' : '') + vars[i][0] + '=' + vars[i][1];

	window.location = new_hash;

	if(msie())
	{
		_set_local('r', _rand());
		_$('__state_mgr_frame').src = 'hist.html?r=' + _get('r') + window.location.hash.toString();
	}
}








