/**
 * pgIndexNewsProfilesAndResearch.js
 * @description JavaScript for pgIndexNewsProfilesAndResearch.cfm renderhandler
 * @author Rebecca Younes
 * @date November 2007
 */

ILR.namespace("renderhandlers");

/**
 * @namespace ILR.renderhandlers
 * @class pgIndexNewsProfilesAndResearch
 */

ILR.renderhandlers.pgIndexNewsProfilesAndResearch = {

	/** 
	 * @description Initialize the object
	 * @method init
	 */
	init: function(){

		this.showRandomProfile();

	},
	
	/**
	 * @description Display a randomly-selected profile of the 3 output by the renderhandler.
	 * Use JavaScript for the rotation to enable CommonSpot element caching on the renderhandler.
	 * Implementation issue: Tried outputting all profiles in ul#profileList, then having JavaScript
	 * unhide the randomly-selected li and hide the first. For some undiscovered reason, this doesn't 
	 * work in IE6; it may be related to the content of the li elements, since in test cases with simple
	 * text in the li it works fine. In the current solution, we generate all but the first profile
	 * in a hidden div outside the ul, then copy in the innerHTML of the randomly-selected profile. 
	 * @method showRandomProfile
	 */
	showRandomProfile : function() {
		
		var	numHiddenProfiles = document.getElementById("hiddenProfiles").getElementsByTagName("LI").length,
			profile0 = document.getElementById("profile0"),
			profile,
			r;	
		
		// If the displayed profile is the only one, do nothing
		if (numHiddenProfiles) {
			r = ILR.util.number.randomIntBetween(0, numHiddenProfiles);

			// ColdFusion displays the 0th profile, so if r == 0, no need to do anything
			if (r) {		
				profile = document.getElementById("profile" + r);
				profile0.innerHTML = profile.innerHTML;
			}
		}
	}
	
};

// NB When this code is put inside ILR.renderhandlers.pgIndexNewsProfilesAndResearch.init(), the script loads but the function 
// isn't recognized when called. This works in /library/customcf/guides/logging/eventLogger.js - the critical difference 
// is probably that in the eventLogger, the included scripts are needed only when an event fires, not on page load.
// The whole thing prevents the page from loading in IE6.
//var requiredScripts = [ILR.js.ilr + "util/number.js"];
//ILR.util.scriptLoader.loadScripts(requiredScripts);
		
YAHOO.util.Event.addListener(window, "load", ILR.renderhandlers.pgIndexNewsProfilesAndResearch.init, ILR.renderhandlers.pgIndexNewsProfilesAndResearch, true);
