
// ensure that the debug console exists
if (window['loadFirebugConsole']) window.loadFirebugConsole(); 
else if (!window['console']) window.console = { info:function(s){}, log:function(s){}, warn:function(s){}, error:function(s){} }

jQuery(function() {
	// line sidebar horizontal line with the top of the first image
	var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	var margin = $(".post-title").css('marginBottom');
	if(margin !== undefined)
	{
		margin = parseInt(margin.replace("px",""));
		if(IE7) margin += 50;
		$("#sidebar").css('top', ($(".post-title").height() + margin) + "px");
	}
	
	// make foto section scroll infinetly
	$("#foto-wrapper").infinitescroll({
		navSelector: ".foto-page-links",
		nextSelector: "#next-page-link",
		itemSelector: ".flickr-images",
		bufferPx: 400,
		loadingImg: '',
		loadingText: '',
		donetext: ''
	});
	
	// make blog scroll infinetly
	$(".narrowcolumn").infinitescroll({
		navSelector: "div.navigation",
		nextSelector: "div.alignleft > a:first",
		itemSelector: "div.post",
		bufferPx: 1000,
		loadingImg: '',
		loadingText: '',
		donetext: ''
	});
	
	// make footer draggable
	$("footer").draggable();
	
	// create glitch button and append it to all images, except footer.
	$('img').not('#footer-circle').after('<a href="#" class="glitch-button"></a>');
	
	// show glitch button on image mouse over, and hide it on mouse out.
	// data is used to keep the events synchronized when the mouse goes over the glitch button.
	$('.glitch-button').data('isMouseOver', false);
	$('img').not('#footer-circle').data('isMouseOver', false);
	
	$('img').not('#footer-circle').mouseenter( function() {
		$(this).next().fadeIn('fast');
		$(this).data('isMouseOver', true);
	});
	
	$('img').not('#footer-circle').mouseleave( function() {
		var img = $(this);
		$(this).data('isMouseOver', false);
		setTimeout(checkIsMouseOver,100);
		function checkIsMouseOver()
		{
			if($(img).next().data('isMouseOver') === false)
				$(img).next().fadeOut('fast');
		}
	});
	
	$('.glitch-button').mouseenter( function() {
		$.data(this, 'isMouseOver', true);
	});
	
	$('.glitch-button').mouseleave( function() {
		$.data(this, 'isMouseOver', false);
		var img = $(this).prev();
		setTimeout(checkIsMouseOver,100);
		function checkIsMouseOver()
		{
			if($(img).data('isMouseOver') === false)
				$(img).next().fadeOut('fast');
		}
	});
	
	$('.glitch-button').click(onGlitchButtonClick);
	
	$("#contact-form .fields input, #contact-form .fields textarea, #comment-form input, #comment-form textarea").inlineFieldLabel();
	$("#contact-form .fields label").hide();
});

function onGlitchButtonClick(event) {
	var $image = $(this).prev();
	
	if ($image.data("old-src")) {
		$image.attr("src", $image.data("old-src"));
		$image.data("old-src", "");
	}
	else {
		$image.data("old-src", $image.attr("src"));
		$image.attr("src", "/glitch_image.php?filename="+encodeURIComponent($image.attr("src")));
	}
	
	return false;
}
