
$(function() {
	
	rotating_images_duration = 6; // seconds
	
	setup_previous_and_next_links();
	
	$('.card a').each(function() { // fix for longer names
		if($(this).height() > 20) $(this).css('margin-top','2px');
	});
	
	$('.home #options a').click(function() {
		if($(this).attr('href').indexOf('#') != 0) return true;
		$('.home #options a').removeClass('current');
		$(this).addClass('current');
		$('.option_block').hide();
		$('#news_and_events').show();
		$($(this).attr('rel')).show();
		return false;
	});
		
	$('.home #attorneys, .home #practice_areas, .home #interstitial').mouseenter(function() { clear_timer(); $(this).show(); });
	$('.home #attorneys, .home #practice_areas').mouseleave(function() { clear_timer(); $(this).hide(); $('.home #options a').removeClass('current'); });
	var _global_timer = null;
	
	function clear_timer() { if(_global_timer != null) {window.clearTimeout(_global_timer);} }
	
	$('.home #options a').hover( // callouts on home
		function() {
			
			el = this;
			
			if($(this).attr('rel').indexOf('#') != 0 ) return;
			
			if( $(this).attr('rel').indexOf('#interstitial') > -1) {
				return;
			}
			
			clear_timer();
			
			_global_timer = window.setTimeout(function() {
				$('.home #options a').removeClass('current');
				$(el).addClass('current');
				$('.option_block').hide();
				$('#news_and_events').show();
				$($(el).attr('rel')).show();
			},300);
		
			return false;
		}, function() { 
			clear_timer(); 
			
			if( $(this).attr('rel').indexOf('#interstitial') > -1) {
				return;
			}
			
			opt = $(this).attr('rel');
			_global_timer = window.setTimeout(function() {
				$(opt).hide();
				$('.option_block').hide();
				$('#news_and_events').show();
				$('.home #options a').removeClass('current');
			},300);
		}
	);
	
	$('.home #features header a').click(function() { // tabs for featured section
		if($(this).attr('href').indexOf('#') != 0) return true;
		$('.home #features header a').removeClass('current');
		$(this).addClass('current');
		$('.tab').hide();
		$($(this).attr('href')).show();
		return false;
	});	
	
	if ($('.rotating_images').length > 0) { // rotating images
		$('.rotating_images img').hide();
		$('.rotating_images img:first-child').show();
		window.setInterval( function() { 
			// rotate image
			current_image = $('.rotating_images img:visible');
			next_image = $(current_image).next();
			if( $(next_image).length == 0) next_image = $('.rotating_images img:first-child');
			$(current_image).fadeOut();
			$(next_image).fadeIn();
		}, rotating_images_duration*1000);
	}
	
	$('body.search_results .search input[type=checkbox]').each(function() {
		selector = '.' + $(this).attr('name') + '_results';
		if($(selector).length == 0) { $(this).attr('disabled',true); $(this).attr('checked',false); $(this).parents('label').css('color','rgb(190,190,190)');}
	});
	$('body.search_results .search input[type=checkbox]').bind('change',function() {
		selector = '.' + $(this).attr('name') + '_results';
		$(this).attr('checked') ? $(selector).show() : $(selector).hide();
	});
	
});

function setup_previous_and_next_links() {
	$('.home .prev, .home .next').click(function() {
		category = $(this).data('category');
		index = $(this).data('index');
		url = "/news/category/" + category + '/' + index;
		$(this).parents('.tab').load(url);
		return false;
	});
}
