(function($) {


$(function() {

	pagesCtl.init();

	$(window).hashchange(function() {

		var hashData = location.hash.split('/');

		if(hashData[0] === '#!' && hashData[1] && pagesCtl.exists(hashData[1])) {

			pagesCtl.open(hashData[1]);

		} else if(/^(|#)$/.test(hashData[0]) && !hashData[1]) {

			pagesCtl.open('top');

		} else {

			pagesCtl.open('notfound');

		}

	}).hashchange();

});

var miscorner = {
	TITLE: 'miscorner/c+llooqtortion'
};

var pagesCtl = {

	pages: null,
	container: null,

	current: null,

	init: function() {
		this.pages = $('article[data-page-id]');
		this.container = $('#pages');
	},

	get: function(pageId) {
		return this.pages.filter('article[data-page-id=' + pageId + ']');
	},

	exists: function(pageId) {
		return !! this.get(pageId).size();
	},

	title: function(title) {
		document.title = miscorner.TITLE + (title ? ' - ' + title : '');
	},

	open: function(pageId) {
		var _this = this,
			handler = function() {
				_this.current = pageId ? _this.get(pageId).fadeIn() : null;
				//_this.current = pageId ? _this.get(pageId).fadeIn(function() {_this.adjust();}) : null;
				_this.title($.trim(_this.current.find('h1').text()));
			};

		this.current ? this.current.fadeOut('fast', handler) : handler();
	},

	adjust: function() {
		if(!this.current) return;
		this.container.height(this.current.outerHeight(true));
	}
	
}

window.miscorner = miscorner;

})(jQuery);
