if(document.getElementById 
	&& document.getElementsByTagName)
{
	Event.observe(window, 'load', init, false);
}

function init()
{
	addListeners();
}

/*
 *	Add event listeners
 */
function addListeners()
{
	var deptNames = document.getElementsByClassName('deptName');

	for (var i = 0; i < deptNames.length; i++)
	{
		Event.observe(deptNames[i], 'click', togglePreceptorsDisplay, false);			
	}
}

/*
 *	Toggles the state of the preceptors display for each department.
 */
function togglePreceptorsDisplay(e)
{
	var preceptorsDept = $(Event.element(e).parentNode.id);
	
	// If display is on, turn it off.
	if (preceptorsDept.hasClassName('on'))
	{
		preceptorsDept.removeClassName('on').addClassName('off');
	}
	else // display is off, turn it on
	{
		// Load display if this is the first view
		if (!preceptorsDept.hasClassName('loaded'))
		{
			loadPreceptorsDisplay(preceptorsDept);
		}
		
		preceptorsDept.removeClassName('off').addClassName('on');
	}
	
	// Remove focus from the anchor that caused this event
	preceptorsDept.blur();
	
	Event.stop(e);
	return false;
}

/*
 *	AJAX call to server to load the preceptors display given a department
 */
function loadPreceptorsDisplay(dept)
{
	var url = preceptorsURL;
	
	// Split the dept ID into an array in order to extract the deptartment Id
	// and division ID (if available)
	var idAry = dept.id.split(/-/);
	
	// Build alphanumeric display ID (i.e. 6 --> deptDisplay-6-17)
	var preceptorDisplayId = idAry[0] + 'Display'; 
	
	// Append the department ID to the URL (i.e. dept-6 --> 6)
	url = url + '?deptId=' + idAry[1];
	preceptorDisplayId = preceptorDisplayId + '-' + idAry[1];
	
	// Append the division ID to the URL, if available (i.e. dept-6-17 --> 17)
	if (idAry.length == 3)
	{
		url = url + '&divId=' + idAry[2];
		preceptorDisplayId = preceptorDisplayId + '-' + idAry[2];
	}
   	                                    
	var myAjax = new Ajax.Updater(
								  {success: preceptorDisplayId},
	                              url,
	                               {
	                               	method: 'get',
	                               	onComplete: function()
	                               	{
	                               		dept.addClassName('loaded');
	                               	},
	                               	onException: function(r,e)
	                               	{
	                               		//TO-DO: Figure out why IE is throwing 
	                               		//an exception here
	                               		window.location = 'clinical-experience.cfm?deptIds=' + idAry[1];
	                               	},
	                               	onFailure: function()
	                               	{
	                               		alert('Sorry, preceptors could not be displayed');
	                               	}
	                               });
}