$(document).ready(function() {
	/**
	 * Allow text fields to be cleared when they gain focus
	 * But only if the user hasn't already entered something
	 * If the field is cleared and then they don't type anything, it reverts to the default value
	 */
	$('.clear-on-focus').live('focusin', function() {
		if(this.value == this.defaultValue) {
			this.value = '';
		}
	}).live('focusout', function() {
		if(!this.value.length) {
			this.value = this.defaultValue;
		}
	});
	
	$("a[href='#top']").click(function() {
		$("html").animate({scrollTop: 0}, "slow");
	});
	
	$(".comment-reply-login").click(function() {
		window.location.hash = '#top';
		$("html").animate({scrollTop: 0}, "slow");
		return false;
	});
	
	$('#sidebar').height($('#content').height() + 40);


	/**
	 * Handle the user logging in using AJAX
	 * This will help to prevent them having to see the ugly WordPress login page
	 */
	$('#login-form').live('submit', function(event) {
		event.preventDefault();
		
		// Get the form object, we'll need to be able to manipulate it in the response
		var form = $(this);
		
		// We need a way to track if there's been an error
		// So that we don't try to process the form
		var is_error = false;
		
		// Get the form values
		var email = form.find('#email');
		var password = form.find('#pass');
		var redirect_to = form.find('#redirect').val();
		
		// Start off by removing any error message from before
		email.removeClass('error');
		password.removeClass('error');
		
		// Validate the email address
		if(email.val() == '' || email.val() == 'Email address') {
			email.addClass('error');
			is_error = true;
		}
		
		// Validate the password
		if(password.val() == '' || password.val() == 'Password') {
			password.addClass('error');
			is_error = true;
		}
		
		if(is_error) return;
		
		// Submit the form
		$.ajax({
			type: 'post',  
			url: $(this).attr('action'),  
			data: $(this).serialize(),  
			success: function(response) {
				// Again, we need a way to track errors
				var response_error = false;
				
				// Check for an error message
				if(response.indexOf('Invalid username') != -1) {
					email.addClass('error');
					response_error = true;
				}
				if(response.indexOf('Incorrect password') != -1) {
					password.addClass('error');
					response_error = true;
				}
				
				// If there where no errors, redirect the user
				if(response_error) return;
				
				location.reload(true);
			}
		});
	});
	
	/**
	 * When they click for more info in the gigs listing
	 * Slide down the info + share buttons
	 */
	$('.gig-list a.more-info').click(function() {
		var content = $(this).parent().find('.content');
		
		// First, hide the others
		$('.gig-list .content:visible').each(function() {
			$(this).slideUp();
		});
		
		// Now show this item, if it's already visible, hide it
		if(content.is(':visible')) content.slideUp();
		else content.slideDown();
		
		return false;
	});
	
	/**
	 * If we're on the gigs page and have a hash (for a specific gig)
	 * Make that gig slide open 
	 */
	if(location.href.indexOf(SITE_URL + 'gigs') != -1 && location.hash != '') {
		$('.gig-list ' + location.hash + ' .content').slideDown();
	}
});



/**
 * Masonry for the gallery page
 */
$(window).load(function() {
	if($('#photo-gallery').length > 0) {
		$('#photo-gallery').masonry({
			columnWidth: 320,
			itemSelector: 'img'
		});
	}
});










