/**
 * StateInitializator Class
 * 
 * @author   Volodymyr Iatsyshyn
 * @date     2009-06-02
 * @see
 */

if (!window.Keepa)
	var Keepa = {};

dojo.provide('Keepa.StateInitializator');

dojo.declare('Keepa.StateInitializator', null, {
	STATE_SEPARATOR: '.',
	
	constructor: function () {
		var hash = window.location.hash.substring(1);
		var args = hash.split(this.STATE_SEPARATOR);
		if (args.length == 2)
			this[args[0] + '_state_'].call(this, args[1]);
		
		dojo.subscribe('on-current-project-changed', this, 'onCurrentProjectChanged_');
	},
	
	project_state_: function (state) {
		dojo.publish('open-menu-for', [state]);
		dojo.publish('on-current-project-changed', [state]);
		
		return this;
	},
	
	onCurrentProjectChanged_: function (projectId) {
		window.location.hash = '#project.' + projectId;
		
		return this;
	}
});

dojo.mixin(Keepa.StateInitializator, {
	initialize: function () {
		new Keepa.StateInitializator();
		return this;
	}
});

dojo.addOnLoad(Keepa.StateInitializator, 'initialize');