$(document).ready(function() {
	// Set active page link as active in the menu
	var activeLink = false;
	
	$('#footer .menu a').each(function() {
		var url = window.location.pathname.substring(window.location.pathname.length - $(this).attr('href').length);
		
		if (url == $(this).attr('href')) {
			$(this).parent().addClass('active');
			
			activeLink = true;
		}
	});
	
	// Sets home page as default active
	if (!activeLink) {
		$('#footer .menu li:first').addClass('active');
	}
	
	// Use input titles as current text and clear on click
	$('input[title], textarea[title]').each(function() {
		var title = $(this).attr('title');
		
		// Set value only if its empty
		if ($(this).val().length == 0) {
			$(this).val(title);
		}
		
		// Bind focus event to clear title text
		$(this).focus(function() {
			if (title == $(this).val()) {
				$(this).val('');
			}
		});
		
		// Bind blur event to reinstate if input empty
		$(this).blur(function() {
			if ($(this).val().length == 0) {
				$(this).val(title);
			}
		});
	});
	
	// clear inputs on title populated forms before submit
	$('form:has(input[title], textarea[title])').submit(function() {
		$(this).find('input[title], textarea[title]').each(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
	});
	
	// Contact form checkbox replacement
	$('.contact .newsletter label').each(function() {
		// Set class to change styling
		$(this).addClass('replace');
		
		// Hide default inputs
		$(this).find('input').hide();
		
		// Hook onto click event to change classes
		$(this).click(function(){
			$(this).addClass('active').siblings().removeClass('active');
		});
	});
	
	// Hide error messages
	$('#errors .close').click(function() {
		$(this).parent().parent().hide();
	});
	
	// Social links open in a new window
	$('#social a, a.external').click(function(e) {
		e.preventDefault();
		
		window.open($(this).attr('href'));
	});
	
	// Functions to add odd/even classes to table rows
	$('table.alternate tr:even').addClass('even');
	$('table.alternate tr:odd').addClass('odd');
	
	// Album artwork slimbox integration
	$('#timeline a.artwork').click(function(e) {
		// Prevent default
		e.preventDefault();
		
		// Get images to use
		var images = $(this).parent().find('ul.gallery img');
		
		// Trigger slimbox if images found
		if (images.length > 0) {
			// Array to store content
			var content = new Array();
			
			// Build content from images
			images.each(function() {
				var source = $(this).attr('src');
				var description = $(this).attr('alt');
				
				content[content.length] = [source, description];
			});
			
			$.slimbox(content, 0);
		} else {
			alert('No artwork available for this album.');
		}
	});
});

