
function Dispatcher() 
{ 
	
	var _aActions;
	
	var _currentAction = null; //Action
	var _id_action;
	
	
	var _ctlr;
	var _controller;
	var _subController = null;
	var _isDies = false;
	
	var _action;
	
	var _isTweening = false;
	
	
	this.addActions = function(aActions)
	{
		_aActions = aActions;
	};
	
	this.addAction = function(action)
	{
		_aActions.push(action);
	};
	
	this.getAction = function(id_action)
	{
		for (var i = 0; i < _aActions.length; i++)
		{
			var action = _aActions[i];
			
			if (action.id == id_action)
			{
				return action;
			}
		}
		
		return null;
	};
	
	this.dispatch = function(id_action, directAnimate, projet)
	{
		
		directAnimate = directAnimate || false;
		projet = projet || null;
	
		if (_isTweening)
			return;
			
		if (_currentAction != null && _currentAction.id == id_action)
			return;
			
		$.each(_aActions, function (i, action)
		{
			//console.log(action.id +" == "+id_action);
			if (action.id == id_action)
			{
				action.projet = projet;
				_isTweening = true;
				
				if (_currentAction != null)
				{

					_currentAction.direct = directAnimate;
					
					_currentAction.postDispatch(action);
					_currentAction = action;
				}
				else
				{
					_currentAction = action;
					_currentAction.direct = directAnimate;
				
					_currentAction.action();
				}
				
				return;
			}
		});
	};
	
	this.resize = function()
	{
		_currentAction.direct = true;
		_currentAction.resize();
	}
	
	this.isTweening = function (isTweening)
	{
		_isTweening = isTweening;
	};
	
	this.getIsTweening = function ()
	{
		return _isTweening;
	};
	
	this.getController = function ()
	{
		return _controller;
	};
	
	this.getIsDies = function ()
	{
		return _isDies;
	};
	
	this.getCtrl = function ()
	{
		return _ctlr;
	};
	
	this.getSubController = function ()
	{
		return _subController;
	};
	
	this.getURL = function()
	{
		
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('/') + 1).split('/');
		
		// 0 : http, 1=host-name, donc on retourne le 2 
		_controller =  hashes[2];
		
		if (hashes[3] != undefined)
			_subController = hashes[3];
		
		//traitement du "#"
		if (_controller.substring(0, 1) == "#")
		{
			_controller = _controller.substring(1);
			_isDies=true;
		}
			
		//For ajax
		_ctlr = _controller;
		if (!_ctlr)
			_ctlr = "accueil";
			
		if (!_controller)
			_controller = "/";
		else
			_controller = "/"+_controller;
			
		
		var id_action = null;
		
		//Controller
		$.each(_aActions, function (i, action)
		{
			if (action.url == _controller)
			{
				id_action = action.id;
				return;
			}
		});
		
		//Subcontroller
		if (_subController != null)
		{
			$.each(_aActions, function (i, action)
			{
				
				if (action.url.indexOf(_controller))
				{
					id_action = action.id;
					return;
				}
			});
		}
		
		//default
		if (id_action == null)
			id_action = _aActions[0].id; // Accueil
			
		_id_action = id_action;
	};
	
	this.run = function ()
	{
		this.dispatch(_id_action, true);
	};
	
	var _dispatchByUrl = function(url)
	{
		
		$.each(_aActions, function (i, action)
		{
			if (action.url == url)
			{
				
				this.dispatch(action.id, true);
				return;
			}
		});
	};

} 

