$(document).ready(function(){
	
	
	//---------------------------------------------------------------------------
	// "Zebra Stripe" tables
	// NOTE: 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.styled');
		table.find('tr:even').addClass('even');
		table.find('tr:odd').addClass('odd');
	}
	stripeTables();
	
	//---------------------------------------------------------------------------
	// add hover effects to table rows
	//---------------------------------------------------------------------------
	$('table.styled tbody > 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();
	
	//---------------------------------------------------------------------------
	// Close Message Boxes
	//---------------------------------------------------------------------------
	$('.messages').live('mouseenter', function(){
		$(this).find('a.closeMessage').stop(true, true).show();
	}).live('mouseleave', function(){
		$(this).find('a.closeMessage').stop(true, true).hide();
	});
	$('a.closeMessage').click(function(){
		$(this).parents('div.messages').slideUp(300, function(){ $(this).remove(); });
		return false;
	});
	
	//---------------------------------------------------------------------------
   // Hide/Show Elements			   
   //---------------------------------------------------------------------------
	$('.hide').hide();
	$('.show').show();
	
	//---------------------------------------------------------------------------
	// Check/Uncheck all checkboxes (eg: for tables)
	//---------------------------------------------------------------------------
	$('.checkAll').click(function(){
		var el = $(this);
		var $r = el.attr('rel');
		$('input[type=checkbox]').each(function(){
			if( $(this).not('.noCheck') && $(this).attr('rel') == $r ) {
				$(this).attr('checked', el.attr('checked'));
			}
		});
	});
	
	//---------------------------------------------------------------------------
	// Tooltips
	//---------------------------------------------------------------------------
	$('.tooltip, .tooltip-s, abbr, dfn').tipsy({ gravity: 's', fade: true });
	$('.tooltip-n').tipsy({ gravity: 'n', fade: true });
	$('.tooltip-e').tipsy({ gravity: 'e', fade: true });
	$('.tooltip-w').tipsy({ gravity: 'w', fade: true });
	$('.text').tipsy({trigger: 'focus', gravity: 'w', fade: true});
		
	//---------------------------------------------------------------------------
	// lightbox (jquery fancybox -- http://fancybox.net/)
	//---------------------------------------------------------------------------
	$('.lightbox').fancybox({
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'speedIn' : 600, 
		'speedOut' : 200, 
		'overlayShow' : true,
		'titlePosition' : 'inside'
	});
	
	
	$('.nyi').click(function(){
		alert('Sorry, but this feature isn\'t quite ready yet. Rest assured that we\'re working hard on it though!');
		return false;
	});
	
	$('.nocert').click(function(){
		alert('This is no certificate on file for this entry.');
		return false;
	});
	
	$('#view_cert_warnings').click(function(){
		$('#cert_warnings').slideToggle(500);
		return false;
	});

	$('#no_expire').click(function(){
		
		if( $(this).is(':checked') ) {
			
			$('#cert_expires').fadeOut(300);	
		} else {
			$('#cert_expires').fadeIn(300);	
		}
	});
	
	$('#search_employees').click(function(){
		if( $(this).val() == 'Search Employees' ) {
			$(this).val('');	
		}
	}).blur(function(){
		if( $(this).val() == '' ) {
			$(this).val('Search Employees');	
		}
	});
	
	
	$('#certificate_name').change(function(){
		if( $(this).val() == 'other') {
			$('#other_certificate').slideDown(300, function(){
				$(this).children('.text').focus();
			});
		} else {
			$('#other_certificate').slideUp(300);	
		}
	});
	
	$('.mark-inactive').click(function(){
		return confirm('You are about to mark this employee as inactive!');
	});
	
	$('.mark-active').click(function(){
		return confirm('You are about to reactivate this employee!');
	});
});
