// JavaScript Document

// addLoadEvent is supposed to keep multiple onload functions from clobbering each other
function addLoadEvent(func) { 
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
};


// TJK_hank adds class="current" to a-tags within #mainnav on current page
// note that link must be site-root relative (beginning with /newenglandtest/ for test site, or /newengland/ for live site)
addLoadEvent(function() { 
	TJK_hank('mainnav','index.php','current','');
});
//-->


// sets #members_only (section within mainnav) class to "loggedin" only if user is logged in
addLoadEvent(function() { 
	$.get(
		'http://www.acacamps.org/newengland/members/index.php', 
		// get() must retrieve a file on the same server as the file running the script.
		// For ACA New England, this can be any page in the password-protected members folder, 
		// because if the user is not logged in, a request for any protected page gets the login page
		{}, // if this were a cgi script with arguments, they would go between these curly brackets as JSON
		function(data,status) {
			if(/<title>Members Login - American Camp Association, New England<\/title>/i.test(data)) {
				$('#members_only.loggedin').addClass('notloggedin');
				$('#members_only.loggedin').removeClass('loggedin');
			}
			else {
				$('#members_only.notloggedin').addClass('loggedin');
				$('#members_only.notloggedin').removeClass('notloggedin');
			}
		}
	);
});
//-->

// allows collapsing block-level elements, by adding class="toggle" to the opening tag
// appends the toggle link to the end of the previous element
addLoadEvent(function() { 
	var showText="More"; // choose text for the show link
	var hideText="Hide"; // choose text for the hide link
	$(".toggle").prev().append(' <a href="#" class="toggleLink">'+showText+'</a>'); // append show/hide links to the element directly preceding the element with a class of "toggle"
	$('.toggle').hide(); // hide all of the elements with a class of 'toggle'
	$(".toggleshow").prev().append(' <a href="#" class="toggleshowLink">'+hideText+'</a>'); // append hide/show links to the element directly preceding the element with a class of "toggleshow"
	$('.toggleshow').show(); // show all of the elements with a class of 'toggleshow'

	$('a.toggleLink').click(function() { // capture clicks on the toggle links
		if ($(this).html()==showText) {
			$(this).html(hideText);
		}
		else {
			$(this).html(showText);
		} // change the link depending on whether the element is shown or hidden
		$(this).parent().next('.toggle').toggle('fast'); // toggle the display
		return false; // return false so any link destination is not followed
	});
	
	$('a.toggleshowLink').click(function() { // capture clicks on the toggle links
		if ($(this).html()==hideText) {
			$(this).html(showText);
		}
		else {
			$(this).html(hideText);
		} // change the link depending on whether the element is shown or hidden
		$(this).parent().next('.toggleshow').toggle('fast'); // toggle the display
		return false; // return false so any link destination is not followed
	});
});
//-->
