/*jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};*/

$(document).ready(function () {
	
	// clear form values on focus
	$('.default-value').each(function() {
   	var default_value = this.value;
      $(this).focus(function() {
			if(this.value == default_value) {
				this.value = '';
			}
			});
			// restore value on blur
			$(this).blur(function() {
     	if(this.value == '') {
     		this.value = default_value;
     	}
			});	
	});
	
	// reveal the nextPage button on mouse movement, just to let people know it's there
	$('html').mousemove( function() {
		$('.nextPage').animate({width: 50});
	});
	
	
	$('.blog .nextPost').hide();
	// initialise scrollables for blog, archive and scrollArticle
	var api = $('.scrollable').scrollable({ 
		api: true,
		size: 2,
		easing: 'easeOutExpo', 
		speed: 700,
		// the onBeforeSeek event is required to hide the .prevPost button before the seek actually happens - otherwise you get 2 buttons overlapping
		onBeforeSeek: function() {
			var index = this.getPageIndex() + 1;
			var amount = this.getPageAmount();
			//console.log(index + '/' + amount);
			if (index > 0) {
				$('.prevPost').hide();
			} else {
				$('.prevPost').show();
			};
		},
		// onSeek
		onSeek: function() {
			var index = this.getPageIndex() + 1;
			var amount = this.getPageAmount();
			if (index == amount) {
				$('.nextPost').show();
				//var url = $('.nextPost').attr('href');
				//$(document).bind('keydown', 'right',function (evt){$('.nextPost').load(url); return false; });
				//$(document).attr('href', url); 
			} else {
				$('.nextPost').hide();	
			};
			if (index > 1) {
				$('.prevPost').hide();
			} else {
				$('.prevPost').show();
			};	
		}
	});
	
	$('.ngg-gallery-scroll').scrollable({ size: 1 });
	
	
	// the menu expander
	$('a#menu-link').click( function() {
		$('#submenu').slideToggle();
	});
	
	$('a#feedback-link').click( function(e) {
		$('#feedback').slideToggle();
		e.preventDefault();
	});
	
	// hover effects for directional arrows
	$(".browse-button").hover(
	  function (e) {
			e.preventDefault();
	    $(this).css('opacity', '1');
	  },
	  function () {
	    $(this).css('opacity', '0.5');
	  }
	);
	
	// shimmy on down to the comment box
	$('#comments-link a').click( function() {
		$.scrollTo( '#comments', 400);
		return false;
	});
	
	// binds key presses to various functions
	$(document).keydown(function (e) {
	  var keyCode = e.keyCode || e.which,
	      arrow = {left: 37, up: 38, right: 39, down: 40 };

	  switch (keyCode) {
	    case arrow.left:
			$('.prevPage').css('opacity', '1');
			window.setInterval(function() {
				$('.prevPage').css('opacity', '0.5');
				}, 500);	
	    break;
	    case arrow.up:
				e.preventDefault(); // stops the normal scroll behaviour
	      $.scrollTo('-=400px', 400, { axis:'y' });
	    break;
	    case arrow.right:
				$('.nextPage').animate({width: 50});
	      $('.nextPage').css('opacity', '1');
			window.setInterval(function() {
				$('.nextPage').css('opacity', '0.5');
				}, 500);
	    break;
	    case arrow.down:
				e.preventDefault(); // stops the normal scroll behaviour
				$.scrollTo('+=400px', 400, { axis:'y' });
	    break;
	  }
	});

	// extracts and displays the user's Flickr username from the first thumbnail URL in the FlickrRSS feed
	if ( $('#flickrrss').length != 0) { // check if the relevant div exists in the DOM
		var url = $('#flickrrss').find('li:first a').attr('href');
		var name = url.split('/')[4];
		$('#flickrrss strong').html('<a href="http://www.flickr.com/photos/' + name + '/">' + name + '</a>');
	}	
	
	// png transparency in IE
	$(document).pngFix();

});
