//therendstudio 2010
//V 0.5

inter_History = {
	excNavReplace : function(navReplace){
		this.getNavReplace = function(){return navReplace;};
	 },
	initialize : function(listener,defaultHash){
		this._listener = listener;
		this._curNav = null;
		if(this.getCurrentHash())
			this._checkHashChange();
		else if(defaultHash)
			this.navigate(defaultHash);
		setInterval(function(){inter_History._checkHashChange();},300);
	},
	_checkHashChange : function(){
		newHash = this.getCurrentHash();
		if(this._curNav == newHash) return;
		this._curNav = newHash;
		try{
			this._listener(newHash);
		}catch(exc){	//listener doesn't like the nav
			if(!exc || exc.constructor != this.excNavReplace) throw exc;
			//It shouldn't have existed - replace it
			window.location.replace('#'+exc.getNavReplace());
		}
	},
	getCurrentHash : function(){
		return unescape(window.location.href.split('#').slice(1).join('#'));
	},
	getCurrentNav : function(){
		return this._curNav;
	 },
	navigate : function(nav){
		nav = nav || '';
		if(this._curNav == nav) return;
		try{
			this._curNav = nav;
			this._listener(nav);
		}catch(exc){	//listener doesn't like the nav
			if(!exc || exc.constructor != this.excNavReplace) throw exc;
			nav = exc.getNavReplace();
			this._listener(nav);
		}
		this._curNav = nav;
		window.location.assign('#'+nav);
	}
};

