/**
 *  NuitHistory - Javascript History Manager v0.1
 *  ----------------------------------------- 
 *  NuitHistory adds history and bookmarking support to ajax/javascript driven
 *  applications. NuitHistory's tested and known to work on IE6, IE7, FF2 and Opera9.
 *  NuitHistory needs Mootools v1.0 to run. For more information, see 
 *  <http://www.solutoire.com/NuitHistory>
 *  
 *	Copyright
 *	---------
 *	Copyright 2007 (c) Bas Wenneker <b.wenneker+spamme[a]gmail[d]com>
 *	For use under the BSD license. <http://www.solutoire.com/NuitHistory>
 */
var NuitHistory = function(){
var registry = {};
var pHash = '';
var scriptUri;
var callback;
return {
	cHash: '',
	start: function(	uri, elements, fn	){
		NuitHistory.parse(	elements	);
		callback = fn || 'alert';
		scriptUri = uri || '';
		cHash = NuitHistory.getHash() || '';
		if(!document.all){
			(function(){
				cHash = NuitHistory.getHash();
				if(cHash !== '' && pHash != cHash){
					pHash = cHash;
					NuitHistory.doCallback();
				}
			}).periodical(200);
		} else {
			var iframe = new Element('iframe').setProperty('name','_NuitHistory___');
			try{
				iframe.setProperty('id','_NuitHistory___').setProperty('frameBorder', '0').setProperty('width','1').setProperty('height','1');
			}catch(e){}
			document.appendChild(iframe);
			if(cHash !== '') NuitHistory.add(cHash);
		}
	},
	add: function(key){
		if(!key) return;
		if(!document.all) window.location.hash = 'h_'+key;
		else frames['_NuitHistory___'].location.href = scriptUri + "?arg=" + registry[key].arg + "&hash=" + key;
	},
	doCallback: function(arg){
		try{
			var obj = registry[NuitHistory.getHash()];
			window[obj.callback || callback](arg || obj.arg);
		}catch(e){}
	},
	parse: function(elements){
		elements.each(function(el){
			var attr = el.getProperty('lang');
			if(attr !== undefined){
				eval('var obj = ' + attr);
				var key = (obj.hash) ? obj.hash : obj.arg;
				registry[key] = {arg:obj.arg, callback: (obj.callback || false)};
				el.addEvent('click',function(e){
					new Event(e).stop();
					NuitHistory.add(key);
				});
			}
		});
	},
	getHash: function(){
		return window.location.hash.replace('#h_','');
	}
};}()