$(document).ready(function() {

	//---------------------------------------------------------------------------
	// "Zebra Stripe" tables
	// FYI: This was made into a function so that it can be called 
	// after any AJAX actions that appends new rows to our table.
	//---------------------------------------------------------------------------
	function stripeTables() {
		var table = $('table:not(".noStripe")');
		table.find('tr:even').addClass('even');
		table.find('tr:odd').addClass('odd');
	}
	stripeTables();
	
	//---------------------------------------------------------------------------
	// add hover effects to table rows
	//---------------------------------------------------------------------------
	$('table:not(".noStripe", ".recaptchatable") tr').live('mouseenter', function(){
		$(this).addClass('hover');
	}).live('mouseleave', function(){
		$(this).removeClass('hover');
	});
	
	//---------------------------------------------------------------------------
	// Fade elements in/out on hover
	//---------------------------------------------------------------------------
	$('.fadeOutOnHover').live('mouseenter', function() {
		$(this).stop(true, true).animate({opacity: .5}, 300);
	}).live('mouseleave', function() {
		$(this).stop(true, true).animate({opacity: 1}, 700);
	});
	$('.fadeInOnHover').live('mouseenter', function() {
		$(this).stop(true, true).animate({opacity: 1}, 300);
	}).live('mouseleave', function() {
		$(this).stop(true, true).animate({opacity: .6}, 700);
	});
	$('.fadeInOnHover').css({opacity: .7});
	
	//---------------------------------------------------------------------------
	// Add an asterisk to required form fields
	//---------------------------------------------------------------------------
	$('.required').parents('li').find('label.desc').append(' <span class="req">*</span>');
	
	//---------------------------------------------------------------------------
	// Auto focus fields
	//---------------------------------------------------------------------------
	$('.focus').focus();
	
		
	$('.redirect').click(function(){
		var el = $(this);
		if( el.attr('rel') != '' ) {
			window.location = el.attr('rel');	
		}
		return false;
	});
   
	$('.hide').hide();
	$('.show').show();	
	
	//------------------------------------------------------------------
	// "Pulse" Function
	//------------------------------------------------------------------
	var pulseTimer;
	function pulse(){
		var el = $('.pulse');	
		if( el.css('opacity') == 1 ) {
			el.stop(true, true).animate({'opacity': .2}, 700);	
		} else {
			el.stop(true, true).animate({'opacity': 1}, 350);	
		}
	}
	
	$('.pulse').mouseover(function() {
		clearTimeout(pulseTimer);
		$(this).stop(true,true).animate({'opacity': 1}, 500);
	}).mouseleave(function(){
		pulseTimer = setInterval(pulse, 500);	
	});
	
	pulseTimer = setInterval(pulse, 500);
	
	
	
	$("#slideshow").jCarouselLite({
  		auto: 10000,
  		speed: 800,
  		btnNext: '#btn-next',
  		btnPrev: '#btn-back'
	});
	
	//---------------------------------------------------------------------------
	// Rotate the "points" on the Join page
	//---------------------------------------------------------------------------
	var el = 'ul.points';
	var pause = 20; // seconds
	var itemCount = 0;
	var itemToShow = 0;
	itemCount = $(el).children('li').length;
	function rotateListItems() {
		
		// If this is the last item then reset the counter
		if( itemToShow == itemCount ) {
			itemToShow = 0;
		}
		
		// show the next item
		var item = $(el + ' li:eq(' + itemToShow +')').find('.expand');
		if( item.length != 0 ) {
			$('.expand.expanded').stop(true, true).slideUp(300).removeClass('expanded');
			item.stop(true, true).slideDown(300).addClass('expanded');
			//$('.expand.expanded').slideUp(300).removeClass('expanded'); // hide the expanded div
			//item.slideDown(300).addClass('expanded'); // show the requested div
		}
		itemToShow++;
	}
	rotateListItems();
	var listTimer = setInterval(rotateListItems, pause*1000);
	
	$('ul.points li h4').click(function(){
		var el = $(this).next('.expand');
		if( el.hasClass('expanded') ) {
			return false;
		}
		$('.expanded').stop(true, true).slideUp(300).removeClass('expanded');
		el.stop(true, true).slideDown(300).addClass('expanded');
		clearInterval(listTimer);
	});
	
});
