/* ----- PLUGINS AND FUNCTIONS ----- */
(function($) {

	$.fn.placeholder = function() {
		
		return this.focus(function() {
		
			if(this.value === this.defaultValue ) {
				this.value = "";
			}
			
		}).blur(function() {
		
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
			
		});
		
	};

})(jQuery);

var caption = {

	open: function($this) {
		var caption = $this.find('.caption');
		caption.fadeIn(200);
	},
	
	close: function($this) {
		var caption = $this.find('.caption');
		caption.fadeOut(200);
	},
	
	initialise: function() {
	
		$('.gallery .image').mouseover(function() {
			caption.open($(this));
		});
		
		$('.gallery .image').mouseleave(function() {
			caption.close($(this));
		});
		
	}

};

var scroll = {

	page: function(id) {

	    var offset = $(id).offset();
	
	    $('html, body').animate({
	        scrollTop: offset.top
	    }, 1100, 'easeInOutQuint');
	
	},
	
	showBackToTop: function() {
		if($(window).scrollTop() >= $(window).height() / 2) {
			$('#back-to-top').fadeIn(300);
		} else {
			$('#back-to-top').fadeOut(300);
		}
	},
	
	initialise: function() {
	
		scroll.showBackToTop();
	
		$(window).scroll(function() {
			scroll.showBackToTop();
		});
		
		$('#back-to-top').click(function() {
			scroll.page('#header');
			return false;
		});
		
	}

};

/* ----- EVENTS ----- */
$(document).ready(function() {
	
	$('input[type=text]:not(.no-placeholder)').placeholder();
	caption.initialise();
	scroll.initialise();
	
	if($('a[rel=modal]').length > 0) {
		$('a[rel=modal]').fancybox();
	}
	
});

