/* Menu Rollover Code */
$.fn.rolloverMenu = function(){
	return this.each(function(){
		var active = $('LI.active', this);
		active.addClass('over');
		$('LI', this).hover(
			function(){
				active.removeClass('over');
				$(this).addClass('over');
			},
			function(){
				$(this).removeClass('over');
				active.addClass('over');
			}
		);
	});
};

$.fn.nextUntil = function(expr) {
	var match = [];
	this.each(function(){
		for (var i = this.nextSibling; i; i = i.nextSibling) {
			if (i.nodeType != 1) continue;
			if ($(i).is(expr)) break;
			match.push(i);
		}
	});
	return this.pushStack(match, arguments);
};

$.slugify = function(value){
	return value.toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/[-]+/g, '-').replace(/^-|-$/g, '');
};

$.fn.idify = function() {
	return $(this).each(function(){
		if (!this.id) {
			this.id = $.slugify($(this).text());
		}
	});
};

$(function(){
	$('#nav').rolloverMenu();
});

$(function(){
	$('hr').replaceWith('<div class="hr"></div>');
});

$(function(){
	$('#pagecontent IMG[align=right]').addClass('alignRight');
	$('#pagecontent IMG[align=left]').addClass('alignLeft');
});

$(function(){
	$('.base TBODY TR:even').addClass('even');
});

$(function(){
	if ($('#subnavFrequentlyAskedQuestions.active').length) {
		
		$('#pagecontent').addClass('expandable-content');
		
		$('#pagecontent H3').click(function(){
			$(this).attr('className', ($(this).next().toggle().is(':visible') ? 'expanded' : 'contracted'));
		}).each(function(){
			$(this).nextUntil('H3').wrapAll('<div class="content-wrapper"></div>');
		}).addClass('contracted').next().hide();
		
		$('.content-wrapper').append('<div class="hr"></div>');
		
		/*$toc = $('<ul id="toc" />').prependTo('#pagecontent');
		$('#pagecontent H3').each(function(i){
			this.id = 'faq'+i;
			$a = $('<a />').attr('href', '#'+this.id).text($(this).text());
			$toc.append($a.wrapInner('<li />'));
			if (i > 0) {
				$(this).before('<p><a href="#top">Back to top</a></p>');
			}
		});
		$('#pagecontent').append('<p><a href="#top">Back to top</a></p>');*/
	}
});

$(function(){
	$('A[href=#top]').parent().addClass('backtotop');
});

$(function(){
	$('a[rel^=fancybox]').fancybox({
		zoomSpeedIn:400,
		zoomSpeedOut:400,
		overlayShow: true,
		overlayColor: '#000',
		overlayOpacity: 0.8,
		hideOnContentClick:false
	});
});

$(function(){
	$('A, AREA').filter(function(){
		var href = $(this).attr('href');
		return !this.target && href && ((href.indexOf(window.location.hostname) == -1 && href.match(/^https?/i)) || href.match(/\.pdf$/i));
	}).attr('target', '_blank');
});

$(function() {
	$('H2, H3').idify();
	// we seem to need this to make the page jump to the right spot (at least in firefox)
	var h = window.location.hash;
	if (h) {
		window.location = h;
	}
});

$(function(){
	$a = $('.home_announcements .announcement');
	var numPages = Math.ceil($a.length / 3);
	var currentPage = 1;
	if (numPages > 1) {
		setTimeout(rotateCells, 10000);
	}
	function rotateCells(){
		if (++currentPage > numPages) {
			currentPage = 1;
		}
		var startIndex = (currentPage - 1) * 3;
		$b = $a.filter(function(i, e){
			return i >= startIndex && i < startIndex + 3;
		});
		$a.fadeOut();
		setTimeout(function(){
			$b.fadeIn();
		}, 600);
		setTimeout(rotateCells, 12000);
	}
});
