/*

*	Classname: HashController
*	Dependencies: none

*	Public method: addObserver (Adds an eventlistener on defined hash and executes given method)
*		Parameters: String:hash, Function:method, String:uniqueIdentifier(not required)
*		Return: Void

*	Public method: removeObserver (Removes an eventlistener on defined hash)
*		Parameters: String:hash
*		return Void

*/

var HashController = function()
{
	var self = this;
	this.hashState = this._getHash();
	this.runnedOnStart = false;
	this.observers = new Array();
	this.hashObserver = setInterval(function()
	{
		var currentHash = self._getHash();
		if(self._getHash() != self.hashState) {
			self.hashState = currentHash;
			var observerId = self._checkObservers(currentHash);
			self._runMethod(observerId);
			observerId = null;
		}
		currentHash = null;
	},50);
};

HashController.prototype = {
	
	addObserver: function(hash, method, uid)
	{
		var observer = new Object();
		observer.hash = hash;
		observer.method = method;
		observer.uid = uid || (this.observers.length);
		this._addObserverToArray(observer);
	},
	
	removeObserver: function(hash)
	{
		var observerId = this._checkObservers(hash);
		if(typeof observerId == 'number')
			this.observers.splice(observerId,1);
	},
	
	_addObserverToArray: function(observer)
	{
		this.observers.push(observer);
		var startIndex = this._checkObservers(this._getHash());
		if(typeof startIndex != 'undefined')
		{
			if(this.runnedOnStart == false)
			{
				this._runMethod(this._checkObservers(this._getHash()));
				this.runnedOnStart = true;
			}
		}
	},
	
	_getHash: function()
	{
		return window.location.hash;
	},
	
	_getState: function()
	{
		return this.hashState;
	},
	
	_checkObservers: function(hash)
	{
		var observers = this.observers;
		for(var observerId = 0; observerId < observers.length; observerId++)
			if(typeof observers[observerId] == 'object')
				if(observers[observerId].hash.toLowerCase() == hash.toLowerCase().replace('#','') || observers[observerId].hash.toLowerCase().match(hash.toLowerCase().replace('#','')))
					return observerId;
	},
	
	_runMethod: function(observerId)
	{
		if(typeof this.observers[observerId] != 'undefined')
			if(typeof this.observers[observerId].method == 'function')
				this.observers[observerId].method(this);
	},
			
	_destroy: function()
	{
		clearTimeout(this.hashObserver);
		this.observers = new Array();
	},
	
	hasRunnedOnStart: function()
	{
		return this.runnedOnStart;
	}
};
if(!$Hash)
	var $Hash = new HashController;

/*
*
*	Example with prototypejs Ajax.Request
*

	<a href="#pageOne" title="Go to page one">Page 1</a>
	
	<script type="text/javascript">
		document.observe('dom:loaded', function() 
		{
			$Hash.addObserver('#pageOne', function(hashObj) //By adding hashObj as parameter in the dummy function, you can get the current instance of HashController in return.
			{
				var self = this;
				new Ajax.Request('index.php',
				{
					method: 'get',
					parameters: {
						'c_' : 'MyClassName',
						'm_' : 'GetContent',
						'pageid' : self.uid // return uid:'ph01'
					},
					onSuccess: function(transport)
					{
						$('htmlContentContainer').update(transport.responseText); // Inserts ajax responsetext in element <htmlContentContainer>
					}
				});
			}, 'pg01');			
		});
	</script>

*/