// var doTheScroll = true;

function debugRow(y, height) {
	if(y === undefined) y = 0;
	if(height === undefined) height = 1;	
	$('body').append('<div class="debugrow" style="top: '+y+'px; height: '+height+'px; background: blue; position: absolute; width: 100%; opacity: 0.5;"></div>');
}

$(function() {
	// EU COOKIE MONSTER
	var eupref = Cookies.get('eu-disable');
	if(eupref == undefined) {
		var cmaside = $('<aside id="cookie_monster">'+
						'	<div class="content">'+
						'		<p>Hello, did you know that I am using <strong>cookies</strong> in this website? I promise they are not tracking you, but I can disable them if you want.</p>'+
						'		<button class="button yes" type="button" data-disable="true">Disable them</button><button class="button no" data-disable="false">It\'s ok</button>'+
						'	</div>'+
						'</aside>');
		$('#top_banner').prepend(cmaside)
		$('#cookie_monster button').click(function(e) {
			t = $(e.target);
			if(t.data('disable')) {
				Cookies.set('eu-disable', true, { expires: 365 });
			} else {
				Cookies.set('eu-disable', false, { expires: 365 });
			}
			cmaside.fadeOut();
		});
	} else {
		Cookies.set('eu-disable', Cookies.get('eu-disable'), {expires: 365});
		if(Cookies.get('eu-disable') == "true") {
			window['ga-disable-UA-1176762-5'] = true;
		} else {
			window['ga-disable-UA-1176762-5'] = false;
		}
	}
	// EU COOKIE MONSTER
	
	if(screen.width > 1024 && screen.height > 768 && window.devicePixelRatio > 1) {
		$('[data-hq]').each(function() {
			t = $(this);
			if(t.is('img')) {
				t.attr('src', t.data('hq'));
			} else {
				t.css('background-image', 'url(' + t.data('hq') + ')');
			}
		});
	}
	
	baseline = 28;//parseFloat($('body').data('baseline'));

	$('figure').each(function(i, e) {
		figure = $(e);
		realTop = figure.offset().top;
		// debugRow(realTop);

		realBottom = realTop + figure.height() + parseFloat(figure.css('margin-bottom'));
		// debugRow(realBottom);

		gap = baseline - ((realBottom - $('article.body').offset().top) % baseline);
		figure.css('margin-top', '+=' + gap/2.0 + 'px');
		figure.css('margin-bottom', '+=' + gap/2.0 + 'px');
	});

	/**
	h = $(document).height();
	b = 28;
	c = $('#content_box').offset().top + b;
	$('body').append('<div id="baseline_overlay" style="position: absolute; top: '+c+'px; width: 100%; height: ' + h + 'px; opacity: 0.4;"></div>');
	bs = $('#baseline_overlay');
	cl = 0;
	while(cl < h) {
		bs.append('<div style="position: absolute; top: ' +cl+ 'px; height: 1px; background: red; width: 100%;"></div>');
		cl += b;
	}
	/**/
});