/* retrieves the random profile and the link to the correct 
	profile page that shows on the homepage
	Also calls the showSelectedTab() function
*/

function showRandomProfile() {
	if (!document.getElementById) { return false; }
	if (!document.getElementById('big_profile_photo')) { return false; }
	if (!document.getElementById('profiletabs_list')) { return false; }
	if (!document.getElementById("profile_image_link")) { return false; }
	
	// create random pool array
	var poolArray = new Array();
	
	// add the profiles to it - 
	// weight should be 1-3
	// if its 3 add the profile 3 times
	for ( keyVar in profilesArray ) {
		var weight = eval(profilesArray[keyVar]['weight']);
		if ( weight < 1 ) {
			weight = 1;
		}else if( weight > 3 ){
			weight = 3;
		}
		for ( var i = 0; i < weight; i++ ) {
   			poolArray.push(keyVar);
		}
	}
	
	// count the pool
	var numInPool = poolArray.length;
	
	// pick a random profile from the pool
	var randomProfileKey = poolArray[Math.floor(Math.random()*numInPool)];
	
		
	// first get the category word (student, faculty, alumni, research)
	var categoryKey = profilesArray[randomProfileKey]['category'];
	var category = categoryArray[categoryKey];
	
	//swap the big image
	var profile_photo = document.getElementById('big_profile_photo');
	profile_photo.src = path_image_base + category + "/images/" + profilesArray[randomProfileKey]['image_file'];

	// make the correct tab selected
	var tablist = document.getElementById('profiletabs_list');
	var tablinks = tablist.getElementsByTagName('a');
	
	for ( var i=0; i< tablinks.length; i++) {
		// get the tab (word) and compare to the category we're in
		// (the first word of the title attribute of the (tab) link
		// needs to correspond to the category word - (or this wont work!))
		var current_tab_part = tablinks[i].title.split(" ");
		var current_mode = current_tab_part[0];
		if (current_mode == category ) {
			// highlighted state
			tablinks[i].setAttribute("class","hilite");
			tablinks[i].className="hilite"; //the above didn't seem to work in IE
			
			var r_href = path_document_base + category + "/" + profilesArray[randomProfileKey]['page_name'];
			
			tablinks[i].setAttribute("href",r_href);
			tablinks[i].href = r_href;
		}else{
			// normal state
			tablinks[i].setAttribute("class","normal");		
			tablinks[i].className="normal";
		}
	}
	
	// swap the href of the link(on the image) to the correct profile
	var profile_link = document.getElementById("profile_image_link");
	profile_link.href = path_document_base + category + "/" + profilesArray[randomProfileKey]['page_name'];
	
	
}





// preload profile images & thumbnail images
function preloadProfileImages() {
	var num_profiles = profilesArray.length;
	
	var loadedImages = new Array();
	
	for ( var k = 0 ; k < num_profiles; k++ ) {
		
		loadedImages[k] = new Image();
		loadedImages[k].src = path_image_base + categoryArray[profilesArray[k]['category']] + "/images/" + profilesArray[k]['image_file'];
		
		loadedImages[k].src = path_thumb_base + categoryArray[profilesArray[k]['category']] + "/images/thumbs/" + profilesArray[k]['thumb'];
	}
	
}



// used the jQuery Library because 
// http://jquery.com/
// waiting for window.onload was slow - image appeared to flicker

jQuery(document).ready(function() {
//$(document).ready(function() {
	//preloadProfileImages();
	showRandomProfile();
});

/*
window.onload = function() {
	showRandomProfile();
}
*/

