/* Javascript for NBS site */


/* Variable to determine if the page has loaded */
var pageLoaded = 0;
var originalTextRemoved = 0;

/* Call the loaded function */
loaded();

function loaded() {

	/* If we have already removed the original text, return out of the function.
	This stops occurences where the new random text can be removed when the page loads quickly */
	if (originalTextRemoved) return;

	/****
	If the 'studentQuote' element is part of the DOM, then carry out neccesary work on it,
	otherwise, rerun this function in 10ms and see if the element is not part of the DOM.
	Repeat until the document is fully loaded (when pageLoaded = 1).
	
	Doing this allow us to remove the default quote text from the element. We can then replace this
	with the random quote generated in the function below.
	****/
	if (document.getElementById && document.getElementById('rotatingQuote') != null) {
		var profileDiv = document.getElementById('rotatingQuote');

		/* Remove each child node of the element */
		while(profileDiv.hasChildNodes()) {
			profileDiv.removeChild(profileDiv.firstChild);	
		}
		
		originalTextRemoved = 1;
		
	}
	else if (!pageLoaded) setTimeout('loaded()', 10);
}


/***
Once the page has loaded, choose a random quote to display,
and update the necessary elements to show the random quote text 
***/
window.onload = function displayRandomProfile() {

	/* If the page has loaded quickly, and we haven't removed the default text, call the 'loaded' function to remove it */
	if (!originalTextRemoved) loaded();
	
	pageLoaded = 1; /* Set pageLoaded to 1, so that the 'loaded' function stops running */
	
	/*****
	Setup array of quotes
	*****/
	var quotes = new Array();

	/*****
	Populate the array with the quotes
	*****/
	
	/* Create variable for quote 1 link */
	var quote1Link = document.createElement('a');
	quote1Link.setAttribute('href','http://www.ntu.ac.uk/apps/pss/courses/cf/60672-1/3/LPC_Legal_Practice_ Course_(Full-time).aspx');
	quote1Link.setAttribute('class','whiteLinkUnderline');
	quote1Link.innerHTML = "new LPC for 2009";

 	quotes[0] = ["SRA gives work-based learning pilot the green light"];
	quotes[1] = ["Nottingham Law School launches "];

	/*****
		Generate a random number between 1 and arraylength to decide which quote to show
	*****/
	var randomID = Math.floor(Math.random() * (quotes.length));

	/* Display the random quote text */
	
	if(document.getElementById && document.getElementById('rotatingQuote') != null) {
		var quoteText = document.createElement("p");
		quoteText.appendChild(document.createTextNode(quotes[randomID][0]));
		if(randomID == 1){
			quoteText.appendChild(quote1Link);
		}
		document.getElementById('rotatingQuote').appendChild(quoteText);
	}

}
