var duration = 1000;
var easing = 'easeOutCubic';
var snippet_type = 'rand01';

/* function widgetClose
--------------------------------------------------*/
function widgetClose(){
	$('.widget, #nav a').removeClass('current');
	$('#sidebar').animate({left:-400}, duration, easing);
	$('#nav a.close').fadeOut(duration);
}

/* smoothScroll
--------------------------------------------------*/
$.fn.smoothScroll = function(){
	if( !$(this)[0].hash || $(this)[0].hash == "#" ) return false;
	return this.each(function(){
		var targetOffset = $($(this)[0].hash).offset().top;
		$('html, body').animate({scrollTop:targetOffset}, duration, easing);
	});
}

/* function contentSlide
--------------------------------------------------*/
function contentSlide(url){
	$('html, body').animate({scrollTop:0}, duration/2, easing);
	widgetClose();
	$('#content').slideUp(duration, easing, function(){
		$('.loading').show();
		$('#pageTitle').slideUp(function(){
			$.ajax({
				url: url,
				success: function(html){
					document.title = /<title>(.*)<\/title>/.exec(html)[1];
					$('#pageTitle').html($(html).find('#pageTitle').text());
					$('#pageTitle').slideDown(function(){
						$(html).find('#content div.inner').each(function(){
							$('#content').html($(this));
						});
						$('.loading').fadeOut(duration/2);
						init();
					});
					var u = url;
					u ? _gaq.push(['_trackPageview', u]) : _gaq.push(['_trackPageview']);
				}
			});
		});
	});
}

/* pushChange
--------------------------------------------------*/
$.fn.pushChange = function(){
	if( !('pushState' in history) ) return this;
	this.live('click', function(){
		var url = this.href;
		history.pushState({path: url, title: document.title, pagetitle: $('#pagetitle').text() }, (this.title ? this.title : document.title), url);
		contentSlide(url);
		return false;
	});
	window.addEventListener('popstate', function(event){
		if(event.state){
			contentSlide(event.state.path);
		}
		else{
			contentSlide(location.href);
		}
	},false);
}

/* navigation
--------------------------------------------------*/
function navigation(){
	$('#nav a[href^="#"]').click(function(){
		var anc = $(this).attr('href');
		$('#nav a').removeClass('current');
		$(this).addClass('current');
		if( $('.widget').is('.current') ){
			if( !$(anc).is('.current') ){
				$('#sidebar').animate({left:-400}, duration, easing, function(){
					$('#sidebar .widget').removeClass('current');
					$(anc).addClass('current');
					$('#sidebar').animate({left:0}, duration, easing);
				});
			}
		}
		else{
			$(anc).addClass('current');
			$('#sidebar').animate({left:0}, duration, easing);
			$('#nav a.close').fadeIn(duration);
		}
		return false;
	});
	$('#nav a.close').click(function(){
		widgetClose();
	});
}

/* pagetop
--------------------------------------------------*/
function pagetop() { 
	$(window).scroll(function(){
		var offset = window.pageYOffset || document.body.scrollTop;
		if( offset > 500 ) $('#pageTop').fadeIn();
		else $('#pageTop').fadeOut();
	});
}

/* init
--------------------------------------------------*/
function init(){
	$('#content').slideDown(duration, easing);
	$.autopager({content: '.inner'});
	$('pre.preJS').snippet('javascript', {style:snippet_type});
	$('pre.preCSS').snippet('css', {style:snippet_type});
	$('pre.preHTML').snippet('html', {style:snippet_type});
	$('pre.prePHP').snippet('php', {style:snippet_type});
	$('pre.preXML').snippet('xml', {style:snippet_type});
}

/* load
--------------------------------------------------*/
$(function(){
	init();
	pagetop();
	$('body').append('<div class="loading"></div>');
	navigation();
	$('a[href^="#"]').click(function(){
		if( !$(this).parents().is('#nav') ){
			$(this).smoothScroll();
			return false;
		}
	});
	$('#wrapper a[target!="_blank"]').pushChange();
});
