/* Javascript for NTU India site */

/*** 
The following executes when the js file is called
***/

/* Variable to determine if the page has loaded */
var pageLoaded = 0;
var originalProfileRemoved = 0;

/* Call the loaded function */
loaded();

function loaded() {

	/* If we have already removed the original profile, return out of the function.
	This stops occurences where the new random profile can be removed when the page loads quickly */
	if (originalProfileRemoved) return;


	/****
	If the 'indexImageArea' 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 hide the element as soon as it available, so we don't get a 'flicker'
	when changing the image/text to the randomly selected profile. We then set the element to
	'visible' once the random image/text has been added to the element.
	****/
	if (document.getElementById && document.getElementById('indexImageArea') != null) {
		document.getElementById('indexImageArea').style.visibility = 'hidden';
	}
	else if (!pageLoaded) setTimeout('loaded()', 10);
	
	
	/****
	Similar to above, but we delete all child nodes of the 'studentProfileText' element.
	This means that we can replace the current text with our random profile text, but
	those with js disabled will still see the original text.
	****/
	if(document.getElementById && document.getElementById('studentProfileText') != null) {
		var profileDiv = document.getElementById('studentProfileText');
		
		/* Remove each child node of the element */
		while(profileDiv.hasChildNodes()) {
			profileDiv.removeChild(profileDiv.firstChild);	
		}
		
		originalProfileRemoved = 1;
	}
}


/***
Once the page has loaded, choose a random profile to display,
and update the necessary elements to show the random text and image 
***/
window.onload = function displayRandomProfile() {

	/* If the page has loaded quickly, and we haven't removed the default profile, call the 'loaded' function to remove it */
	if (!originalProfileRemoved) loaded();
	
	pageLoaded = 1; /* Set pageLoaded to 1, so that the 'loaded' function stops running */
	
	/*****
	Setup array of student profiles
	*****/
	var profiles = new Array();

	/*****
	Populate the array with the student profile text and image
	*****/
	profiles[0] = ["", "resources/images/profiles/a.jpg"];
	profiles[1] = ["", "resources/images/profiles/b.jpg"];

	/*****
		Generate a random number between 1 and arraylength to decide which profile to show
	*****/
	var randomID = Math.floor(Math.random() * (profiles.length));
	
	/* Display the random profile text */
	if(document.getElementById && document.getElementById('studentProfileText') != null) {
		var profileText = document.createTextNode(profiles[randomID][0]);
		
		document.getElementById('studentProfileText').appendChild(profileText);
	}
	
	/* Display the random profile image */
	if(document.getElementById && document.getElementById('indexImageArea') != null) {
		document.getElementById('indexImageArea').style.backgroundImage = "url('" + profiles[randomID][1] + "')";
		document.getElementById('indexImageArea').style.visibility = '';
	}

}
