function Places() {
	var self = this;
	
	self.dummyHash = 'primat';
	
	self.places = {};
	
	self.defaultPlace = null;
	
	self.currentPlace = null;
	
	self.hashvalues = {};
	
	self.parseHash = function() {
		self.hashvalues = {};
		
		if( window.location.hash ) {
			var split = window.location.hash.substring(1).split('&');
			
			for( var i = 0; i < split.length; i++ ) {
				var keyvaluestring = split[i];
				var keyvalue = keyvaluestring.split('=');
				
				if( keyvalue.length >= 2 ) {
					self.hashvalues[keyvalue[0]] = keyvalue[1];
				}
			}
		}
	}
	
	this.reload = function() {
		var url = null;
		
		if( window.location.hash ) {
			url = window.location.href.substr( 0, window.location.href.indexOf( '#' ) );
		} else {
			url = window.location.href;
		}
		
		window.location.href = url;
	}
	
	this.go = function() {
		self.gotoPlace( self.hashvalues.place );
	}
	
	this.gotoPlace = function( place ) {
		if( place ) {
			if( typeof(self.places[place]) == 'function' )
				self.places[place]();
		} else if( typeof(self.defaultPlace) == 'function' ){
			self.defaultPlace();
		}
	}
	
	this.addPlace = function( place, callback ) {
		if( 'function' == typeof callback ) {
			self.places[ place ] = callback;
		}
	}
	
	this.addDefaultPlace = function( callback ) {
		if( 'function' == typeof callback ) {
			self.defaultPlace = callback;
		}
	}
	
	this.setPlace = function( place ) {
		if( !place ) {
			window.location.hash = self.hash = self.dummyHash;
		} else {
			window.location.hash = self.hash = 'place=' + place;
		}
		
		self.currentPlace = place;
		
		self.parseHash();
	}
	
	jQuery(window).hashchange( function() {
		self.parseHash();
		self.go();
	} );
};

places = new Places();

$(document).ready(function() {
	$('body').append('<script type="text/javascript">$(document).ready(function() {jQuery(window).hashchange()})</script>');
});

