var fadeTime = 0;
var buttonTime = 0;
var waitTime = 0;
var lastTab = "";
var lastProfileType = "";

function showSelectedTab(mode) {
	if(!document.getElementById('profiletabs_list')) { return false; }
	
	var tablist = document.getElementById('profiletabs_list');
	var tablinks = tablist.getElementsByTagName('a');
	
	for ( var i=0; i< tablinks.length; i++) {
		
		// the id's on each link are format: mode_profile
		
		var tab_part = tablinks[i].title.split(" ");
		var link_mode = tab_part[0];
		
		if (link_mode == mode ) { 
			// highlighted state
			tablinks[i].setAttribute("class","hilite");
			tablinks[i].className="hilite"; //the above didn't seem to work in IE
		}else{
			// normal state
			tablinks[i].setAttribute("class","normal");		
			tablinks[i].className="normal";
		}
	}
}


function complete() {
    
  }



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

function showRandomProfile(mode) {
	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; }
	
	if( fadeTime == 0 ) 
	{
		// run first time to hide hilite image
		$(".profile_top_img").fadeOut(0); 
	}
	
	var categoryNames = new Array ();
	categoryNames["r"] = "research";
	categoryNames["s"] = "student";
	categoryNames["f"] = "faculty";
	categoryNames["a"] = "alumni";
	
	// these arrays from /profiles/profile_data.shtml page
	var profiles = new Array (studentArray, alumniArray, facultyArray, researchArray);
	var randomProfileArray;
	var profileType = '';	
	
	if (mode == "all" || mode == "homepage") {
		// homepage
		// randomly choose a category
		profileType =  Math.floor(Math.random()*profiles.length);
		//alert('PT = ' + profileType + ' PL = ' + profiles.length   + ' LP = ' + lastProfileType );
		
		if( profileType == lastProfileType )
		{
			profileType = profileType + 1;
		}
		
		if( profileType > profiles.length - 1 )
		{
			profileType = 0;
		}
		
		//alert('PT = ' + profileType + ' PL = ' + profiles.length   + ' LP = ' + lastProfileType );
		
		lastProfileType = profileType;
		randomProfileArray = profiles[profileType];
		
	}else{
		//category landing pages
		switch(mode) {
			case "student":
				randomProfileArray = profiles[0];
				break;
			case "alumni":
				randomProfileArray = profiles[1];
				break;
			case "faculty":
				randomProfileArray = profiles[2];
				break;
			case "research":
				randomProfileArray = profiles[3];
				break;
			default:
				randomProfileArray = profiles[0];			
		}
	}
	
	// randomly choose a profile
	var randomProfileKey = Math.floor(Math.random()*randomProfileArray.length);
	var randomProfile = randomProfileArray[randomProfileKey];
	//alert('RP = ' + randomProfileKey);
	// get the category word (student, faculty, alumni, research)
	var categoryKey = randomProfile[2];
	var categoryWord = categoryNames[categoryKey];
	
	var tablist = document.getElementById('profiletabs_list');
	var tablinks = tablist.getElementsByTagName('a');
	
	if (mode == "all") 
	{
		categoryWord = 'all';
	}
	
	$("#big_profile_photo").fadeOut(fadeTime,function(){
		
		$("#" + lastTab + "_profile_top_img").fadeOut(buttonTime*2);
		buttonTime = 850;
		
		//swap the big image
		var profile_photo = document.getElementById('big_profile_photo');
		profile_photo.src = randomProfile[4];
		profile_photo.alt = randomProfile[1];
		// swap the href of the link(on the image) to the correct profile
		var profile_link = document.getElementById("profile_image_link");
		profile_link.href = randomProfile[3];
		
		$("#big_profile_photo").fadeIn(fadeTime);
		
		$(".profile_top_img").fadeOut(0,function(){ });
		//alert('#' + categoryWord + '_profile_top_img');
		$('#' + categoryWord + '_profile_top_img').fadeIn(buttonTime);
		
		fadeTime = 1500;
	});
	
	//alert(lastTab + ' = ' + categoryWord + '_profile');
	
	lastTab = categoryWord;
	
	//alert('X mode: ' + mode + ' alertFlag: ' + alertFlag + ' warningFlag: ' + warningFlag );
	if ( (mode == "all" || mode == "homepage") && alertFlag === 0 ) 
	{
		recallTime = (fadeTime * 2) + (buttonTime * 3) + waitTime;
		setTimeout('showRandomProfile("homepage")',recallTime);
	}
	waitTime = 2000;
}


function sortBySortString(a, b) {
    var x = a[0];
    var y = b[0];
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	}else{
		parent.insertBefore(newElement,targetElement.nextSibling);	
	}
}


/* showCategoryList()
 * generates the list shown on a category index page
 * mode = student, faculty, alumni, research
 */
function showCategoryList(mode) {
	if (!document.getElementById('pmenu_header')) { return false; }
	
	// these arrays from /profiles/profile_data.shtml page
	var profiles = new Array (studentArray, alumniArray, facultyArray, researchArray);
	var thisProfileArray;
	
	switch(mode) {
		case "student":
			thisProfileArray = profiles[0];
			break;
		case "alumni":
			thisProfileArray = profiles[1];
			break;
		case "faculty":
			thisProfileArray = profiles[2];
			break;
		case "research":
			thisProfileArray = profiles[3];
			break;
		default:
			thisProfileArray = profiles[0];			
	}
	
	thisProfileArray.sort(sortBySortString);
	
	var header = document.getElementById('pmenu_header');
	//create the ul
	var nav_list = document.createElement("ul");
	
	if ( thisProfileArray.length > 0 ) {
		
		var column1 = Math.floor(thisProfileArray.length/2) + thisProfileArray.length%2 ;

		for ( var i=0; i<column1; i++ ) {
			
			if ( thisProfileArray[i].length > 0 )	{
				var li = document.createElement("li");
				var li_text = document.createTextNode(thisProfileArray[i][1]);
				var li_a = document.createElement("a");
				li_a.setAttribute('href',thisProfileArray[i][3]);
				
				
				li_a.appendChild(li_text);
				li.appendChild(li_a);
				nav_list.appendChild(li);
				
				
			}
			
		}
	
		if (nav_list.childNodes.length < 1) return false;
		insertAfter(nav_list,header);
		
		var nav_list2 = document.createElement("ul");
		
		for ( var i=column1; i<thisProfileArray.length; i++ ) {
			
			if ( thisProfileArray[i].length > 0 )	{
				var li2 = document.createElement("li");
				var li_text2 = document.createTextNode(thisProfileArray[i][1]);
				var li_a2 = document.createElement("a");
				li_a2.setAttribute('href',thisProfileArray[i][3]);
				
				
				li_a2.appendChild(li_text2);
				li2.appendChild(li_a2);
				nav_list2.appendChild(li2);
				
				
			}
				
		}
		
		
		insertAfter(nav_list2,nav_list);
		
		
		
	}else{
		return false;	
	}
	
}

/* showAllCategorisList()
 * generates the list shown on the allprofiles index page
 * the list is two columns and show/hide for each category
 */
function showAllCategoriesList() {
	
	if (!document.getElementById('pmenu_header')) { return false; }
	
	// these arrays from /profiles/profile_data.shtml page
	var profiles = new Array;
	profiles['student'] = studentArray.sort(sortBySortString);;
	profiles['faculty'] = facultyArray.sort(sortBySortString);
	profiles['research'] = researchArray.sort(sortBySortString);
	profiles['alumni'] = alumniArray.sort(sortBySortString);
	
	var lastElement = document.getElementById('pmenu_header');
	
	for (var key in profiles ) {
		//alert(	key );
		
		//create category header 
		var h2 = document.createElement("h2");
		h2.setAttribute('class','category_trigger');
		h2.className = 'category_trigger';
		var h2_a = document.createElement("a");
		h2_a.setAttribute('class','closed');
		h2_a.className = 'closed';
		var h2_text = document.createTextNode(key.charAt(0).toUpperCase() + key.slice(1) + " Profiles");
		h2_a.appendChild(h2_text);
		h2.appendChild(h2_a);
		
		
		//create new div for content
		var contentDiv = document.createElement("div");
		contentDiv.setAttribute('class', 'profiles_category_list');
		contentDiv.className = 'profiles_category_list';
		
		
		insertAfter(h2,lastElement);
		insertAfter(contentDiv,h2);
		
		//create the two column list
		var thisProfileList = document.createElement("ul");
	
		if ( profiles[key].length > 0 ) {
			
			//column 1
			var column1 = Math.floor(profiles[key].length/2) + profiles[key].length%2 ;
	
			for ( var i=0; i<column1; i++ ) {
				if ( profiles[key][i].length > 0 )	{			
					var li = document.createElement("li");
					var li_text = document.createTextNode(profiles[key][i][1]);
					var li_a = document.createElement("a");
					li_a.setAttribute('href',profiles[key][i][3]);
								
					li_a.appendChild(li_text);
					li.appendChild(li_a);
					thisProfileList.appendChild(li);
				}
	
			}
			
			//column 2
			var thisProfileList2 = document.createElement("ul");
		
			for ( var i=column1; i<profiles[key].length; i++ ) {
				
				if ( profiles[key][i].length > 0 )	{
					var li2 = document.createElement("li");
					var li_text2 = document.createTextNode(profiles[key][i][1]);
					var li_a2 = document.createElement("a");
					li_a2.setAttribute('href',profiles[key][i][3]);
					
					
					li_a2.appendChild(li_text2);
					li2.appendChild(li_a2);
					thisProfileList2.appendChild(li2);
					
					
				}
					
			}
		}
	
		contentDiv.appendChild(thisProfileList);
		contentDiv.appendChild(thisProfileList2);
		lastElement = contentDiv;

	}
}


/*quicklinks*/
$(document).ready(function() {
	$(".jumpmenu").change(function(e) {
		window.location.href = $(this).val();
	});
});







