/*
 * Name:
 *	load_forum.js
 *
 * Description:
 *	Javascript functions for the RSS box.
 *
 * Pre-conditions:
 *	None
 *
 * Post-conditions:
 *	The following functions are created:
 *	- loadForumThread
 *
 * Log:
 *	Randall Betta		08/04/2006
 *		- Creation
 *
 */

 
 

/*
 * Name:
 *	loadForumThread
 *
 * Description:
 *	Loads the specified forum thread into the first ForumBox found on the webpage, otherwise loads
 *	the requisite forum topic page.
 *
 * Pre-conditions:
 *	forumThreadId		: REQUIRED		The forumThreadId field of the ForumThread record.
 *
 * Post-conditions:
 *	The specified forum is loaded. Returns true on success, or false otherwise.
 *
 * Log:
 *	Randall Betta		08/04/2006
 *		- Creation
 *	Randall Betta		09/14/2006
 *		- Now handles any difference in iframe levels between the invoking code and a target iframed forum box.
 *
 */
function loadForumThread(forumThreadId) {
	var thisWindow;
	var iFrameElts;
	var iFrame;
	var iFrameCount;
	var iFrameIndex;
	var iFrameClass;
	var iFrameSrc;
	var forumFound;
	var questionMarkPos;
	var newWinSrc;

	// Initialize loop's sentinel variable.
	forumFound = false;

	// Obtain a listing of all iframes in the document hierarchy.
	iFrameElts = getRelatedElements('iframe', '^iFrameContentsForum$', false, true);
	iFrameCount = iFrameElts.length;
	
	// Iterate through the iframe tags returned.
	for (iFrameIndex = 0; iFrameIndex < iFrameCount; iFrameIndex++) { // For: iterate through page's iframes.
		
		// Obtain  reference to the current iFrame.
		try { // Try: manipulate iframes.
			iFrame = iFrameElts[iFrameIndex];

			// IE and W3C-compliant browsers have slightly different ideas on what the name of the "class" HTML property
			// should be. Missing attributes should be returned as empty strings, but non-compliant browsers will return
			// null instead sometimes, so ensure the class name returned is not null before performing comparisons on it.
			iFrameClass = iFrame.getAttribute('class');
			if ((iFrameClass) ? iFrameClass == 'iFrameContentsForum' : false) { // Is this a forum?
				forumFound = true;
			}
			else { // Else: this may be a browser that uses the "className" attribute rather than the "class" attribute.
				iFrameClass = iFrame.getAttribute('className');
				if ((iFrameClass) ? iFrameClass == 'iFrameContentsForum' : false) { // Is this a forum?
					forumFound = true;
				}
			} // End else: this may be a browser that uses the "className" attribute rather than the "class" attribute.
			
			//
			// If execution reaches this point and the variable "forumFound" is true, the contents of the variable
			// "iFrame" constitute a forum box. Load the desired thread into it.
			//
			
			if (forumFound) { // If: an iframed Forum box was found.
						
				// Discover whether the forum box has a query string (i.e. has already had a forum to load specified
				// in its URL at least once) by finding the position of its beginning question mark.
				iFrameSrc = iFrame.src;
				questionMarkPos = iFrameSrc.indexOf('?');
				if (questionMarkPos >= 0) { // If: there is a query string.
					// Strip the query string.
					iFrameSrc = iFrameSrc.substring(0, questionMarkPos);
				} // End if: there is a query string.
				
				// Add our new query string to the iframe src property.
				iFrameSrc = iFrameSrc + '?forumThreadId=' + forumThreadId;
				//Reload the iframe.
				iFrame.src = iFrameSrc;
				
				// Indicate success.
				return true;
			
			} // End if: a forum box iframe was found.
		} // End try: manipulate iframes.
		catch (exc) { // Catch: a security exception occurred.
			// Cross-frame scripting security violation occurred; skip this frame, since we don't have
			// proper permissions to access it.
			continue;
		} // End catch: a security exception occurred.
	} // End for: iteration through page's iframes.
	
	// If execution reaches this point, no iframed forum box was found.
	// Load the forum in its topic page, in this top-level window.
			
	return loadNewWindow(forumThreadId);
	
} // End function: loadForumThread


/*
 * Name:
 *	loadNewWindow
 *
 * Description:
 *	Loads the specified forum thread into the station's Forum topic (i.e. specialty) page, in a
 *	new window.
 *
 * Pre-conditions:
 *	forumThreadId		: REQUIRED		The forumThreadId field of the ForumThread record.
 *
 * Post-conditions:
 *	The specified forum is loaded in a new window. Returns true on success.
 *
 * Log:
 *	Kripa Shenai		08/07/2006
 *		- Creation
 *
 */
function loadNewWindow(forumThreadId) {

	//in case forum already exists but if we have a 'dont open in forum box' clause use this script
	
	var newWinSrc;
				
	// Define the URL for the forum topic page, specifying the desired forum thread.
	newWinSrc = '/topic/forum.php?forumThreadId=' + forumThreadId;
	
	//open Topic in current top-level window.
	top.location.href = (newWinSrc);
	// Indicate success.
	return true;
}
