$(document).ready(function(){
	
	// display the right tabs
	$('.tab-panel').hide();
	$('.tab-panel:first').show();

	// tab click functions
	$('#episode-tabs .tab-links li a').click(function() {
		
		var panel = '#'+$(this).attr('rel');
		
		$('.tab-panel').hide().css({'opacity': 0});
		$(panel).show().css({'opacity': 0}).animate({'opacity': 1}, function() {});
		
		var index = $('#episode-tabs li a').index(this);
		
		$('#episode-tabs').removeClass();
		$('#episode-tabs').addClass('header tab tab-'+(index+1))
		
		return false;
		
	})
	
	// list click 
	
	$('div.episodes-index ol li h3 a.link').each(function() {
		
		$(this).parents('li').css('cursor', 'pointer');
		
		var link = $(this).attr('href');
		
		$(this).parents('li').click(function(){
			window.location.href = link;
		})
	})
	
	// equal hieghts 
	
	customEqualHeights($('.episodes-index ol li'));
	
	
	// login / register forms
	
	$('#register-button').click(function() {
		
		$('div.login').css({'opacity': 1}).animate({'opacity': 0}, function() {
			$(this).hide();
			$('.register').css({'opacity': 0}).show().animate({'opacity': 1}, function() {});
		});
		
		return false;
	})
	
	
	//validate forms 
	
	$(".register .form").validate();
	$(".new-form .form").validate();
	$(".login .form").validate();
	
	$('.form.logged-out, .logged-out input').click(function() {
		
		alert('You must be logged in to comment.');
		
		return false;
		
	})
	
	$('.logged-out .textarea, .logged-out input.text').attr('disabled','disabled').css('opacity', 0.5);
	
	
	// fade message 
	
	$('.message').css({'opacity': 1}).animate({'opacity': 0}, 1000, function() {
		$(this).hide();
		$('.no-show').css({'opacity': 0}).show().animate({'opacity': 1});
	});
	

});

function customEqualHeights(element) {
	
	var tallest = 0;

	$(element).each(function() {

		var height = $(this).height();

		if(height > tallest) tallest = height;

	});

	$(element).css('min-height', tallest);
	
}
