// courseResources.js

/************************************************************************
 *	DisplayResources -	fills the innerHTML of the resources menu		*
 *						div with the corresponding links of the segment	*
 *	@var courseModule	-	the selected course module containing the	*
 *							resource array of object					*
 ************************************************************************/
DisplayResources = function( courseModule )
{
	// verify course module parameter
	if( courseModule == null ) {
		alert("DisplayResources :: No course module specified.");
		return;
	}
	
	// get document element
	resourceDiv = document.getElementById('course-resources');
	
	// verify document element
	if( resourceDiv == null ) {
		alert("DisplayResources :: Error locating document element 'course-resources'.");
		return;
	}
	
	// if no resource file is needed, remove the resources box and end here.
	if( courseModule.resources == null) {
		resourceDiv.innerHTML = "";
		return;
	}
	
	// write div segment header data
	var HTML = '<h3>Resource Files</h3><div><ol>';
	
	var res = courseModule.resources;
			
	for(var i=0; i<res.length; i++)	
		HTML += '<li><p>' + (i+1) + '.</p><a href="'+res[i].href+'" title="'+res[i].name+'" target="_blank" >' +
				wordWrap(res[i].name) + '</a></li>';

	// write div segment footer data
	HTML += '</ol></div>';
	
	resourceDiv.innerHTML = HTML;
}

/************************************************************************
 *	wordWrap 		- format a link name to fit in the resource menu	*
 *					  (recursive function call to handle large strings)	*
 *	@var name		- the string name that is word wrapped				*
 ************************************************************************/
wordWrap = function( name )
{
	// handle links that are 50 characters or more
	if(name.length >= 50) {
		// from 0 to 50, find the last occurance of the whitespace
		var whitespace = name.substr(0,50).lastIndexOf(' ');
		
		// insert a line break and then indent the line to make it a hanging paragraph
		return name.substr(0, whitespace) + '<br /><p class="gap">&nbsp;</p>' + 
											wordWrap(name.substr(whitespace+1)); /* handle the tail of the string from the whitespace forward */
	}
	
	return name;
}

// end of file