/* Author:
	Simon Lepel
*/

$(document).ready(function(){

	$(window).resize(function(){
		var wh = $(window).height();
		$('#nav').each(function(){
			$(this).css( 'left', $(this).width()*-1 );
		});
		$('.article').each(function(){
			$(this).css( 'min-height', wh );
		});
		$('#teaser-overlay').each(function(){
			var teaser_height = $(this).find('#teaser-content').height();
			$('#teaser-container').css( 'margin-top', Math.floor((wh-teaser_height-60)/2) );
		});
	}).trigger('resize');

	$('#logo').attr({ href: '#home', rel: 'section' });

	$('a[rel="section"]').each(function(){
		$(this).click(function(ev){
			ev.preventDefault();
			var section_key = $(this).attr('href'),
				section = $( section_key ),
				section_position = section.position();
			$('#nav a').not(this).removeClass('active').end().filter('[href="'+section_key+'"]').addClass('active');
			$('html,body').animate({
				scrollTop: section_position.top
			},{
				duration: 1500,
				queue: false,
				easing: 'easeInOutQuad'
			});
		});
	});
	
	$('#teaser-overlay').each(function(){
		$(this).find('#teaser-close').click(function(ev){
			ev.preventDefault();
			ev.stopPropagation();
			$('#teaser-overlay').fadeOut(800,function(){
				$('html').removeClass('teaser');
				$(this).remove();
			});
		});
		$(this).find('#teaser-content').click(function(ev){
			ev.stopPropagation();
			$('#teaser-close').trigger('click');
		});
		$(this).click(function(ev){
			$(this).find('#teaser-close').trigger('click');
		});
		$('html,body').scrollTop(0);
	});
	$(document).keypress(function(ev){
		if( $('#teaser-overlay').length>0 && ev.which==0 )
			$('#teaser-close').trigger('click');
	});
	
	$('#fb-like a').each(function(){
		var fade_duration = 600,
			container = $(this).parent().find('#fb-container');
		if( container.length<1 )
			container = $('<div class="fb-container" />').hide().appendTo( $(this).parent() );
		if( $('#fb-root').length<1 )
			$('body').append('<div id="fb-root" />');
		$(this).click(function(ev){
			ev.preventDefault();
			if( container.find('.fb-like-box').length<1 ) {
				container.append('<div class="fb-like-box" data-href="http://www.facebook.com/pages/Thomas-Azier/116006125140553" data-width="292" data-show-faces="false" data-stream="false" data-header="false" />');
				(function(d, s, id) {
					var js, fjs = d.getElementsByTagName(s)[0];
					if (d.getElementById(id)) return;
					js = d.createElement(s); js.id = id;
					js.src = "//connect.facebook.net/de_DE/all.js#xfbml=1&appId=302107193160440";
					fjs.parentNode.insertBefore(js, fjs);
				}(document, 'script', 'facebook-jssdk'));
			}
			$(this).toggleClass('box-visible');
			container.fadeToggle(fade_duration);
		});
	});

});

