function form_slider () {
	var thisLink = $(this);
	var form = $(thisLink.attr('href'));
	if (form.css('display') == 'none') {
		form.slideDown(250, resize_columns);
		thisLink.attr('old_text', thisLink.text());
		thisLink.text('Hide form');
	} else {
		form.slideUp(250, resize_columns);
		thisLink.text(thisLink.attr('old_text'));
	}
	
	return false;
}

function menu_slider () {
	var sub = $($(this).attr('href'));
	if (sub.css('display') == 'none') {
		$("#menu a.top-level").removeClass('expanded').children('span.exp').text('+');
		$("#menu li ul").slideUp(250);
		sub.slideDown(250);
		$(this).addClass('expanded');
		$(this).children('span.exp').text('-');
	}
	return false;
}

function find_current_page () {
	$("#menu a").each(function (i) {
		if (document.location.pathname == $(this).attr('href')) {
			$(this).addClass('current');
		}
	});
}

function resize_columns () {
	// size all columns to be equal to the greatest
	var max_height = 0;
	var cols = $([$('#nav'), $('#content'), $('#sidebar')]);
	cols.each(function () {
		$(this).css('height', 'auto');
		if ($(this).height() > max_height) {
			max_height = $(this).height();
		}
	});
	cols.each(function () {
		$(this).height(max_height);
	});
}

// onready
$(function () {
	// hide all nav submenus
	$('#nav li ul').hide();
	$('#nav a.top-level').removeClass('expanded');
	// show the appropriate one for the current page
	find_current_page();
	var menu = $('#menu ul:has(a.current)');
	menu.show();
	menu.prev("a.top-level").addClass('expanded').children('span.exp').text('-');
		

	// set up the sliders
	$("#menu a.top-level").each(function (i) {
		$(this).click(menu_slider);
	});
	
	// hide the contact forms
	$('#ask-form').hide();
	$('#appointment-form').hide();
	$('#ask-form-link').click(form_slider);
	$('#appointment-form-link').click(form_slider);
	
	// swap out the org logos
	$('div.logos img').each(function () {
		var curr = $(this);
		var colour_href = curr.attr('src');
		var gray_href = curr.attr('src').replace(/-c\./, '-g.');
		curr.attr('src', gray_href);
		curr.mouseover(function () {
			curr.attr('src', colour_href);
		});
		curr.mouseout(function () {
			curr.attr('src', gray_href);
		});
	});
	
	// rotate sponsor logos
	$('#rotating-logos').each(function (idx, elem) {
		var me = $(elem);
		function doRotate () {
			var links = me.find('a');
			var firstLink = $(links.get(0));
			firstLink.hide().remove();
			me.append(firstLink);
			firstLink.fadeIn(800);
		}
		window.setInterval(doRotate, 5000);
	});
});

// onload
$(window).load(resize_columns);

