/*******************************

 Topic.js, Version 4.3.2, 15-Jun-2009
 Copyright Northgate Information Solutions UK Limited, 2002-2009

 *******************************/

/***********************
 Topic class

 Constructor:
	Topic(parent)
 Properties:
	contentsPage
	document
	globalSearchFiles
	globalSearchIndex
	glossaryFrameset
	indexPage
	linksFile
	linksFrame
	navTool
	parent
	searchFiles
	searchIndex
	topicId
	url
 Methods:
	update()
	toggleDisplay(targetId{, source{, prompt}})

 ***********************/

// *** Constructor for Topic class ***
function Topic(parent) {
	this.parent = ((typeof parent == "object") 
		&& (parent.constructor == HelpSystem)
		? parent : null);
	if (this.parent == null)
		return(null);

	this.contentsPage = "";
	this.document = null;
	this.glossaryFrameset = "";
	this.indexPage = "";
	this.linksFile = "";
	this.linksFrame = "";
	this.navTool = "";
	this.searchPage = "";
	this.searchFiles = "";
	this.searchIndex = "";
	this.topicId = "";
	this.url = "";
	this.displayPrompts = new Object();
}

// *** Topic instance methods ***
	// Update object's properties.
Topic.prototype.update = function tpcUpdate() {
	/* Figure out which navigation tool and, if appropriate, links 
	   file to load. */

	var settings = new NavSettings();

	// Get the url of the current topic.
	var p = this.parent;
	/* If the URL includes a search string, but no bookmark, 
	   treat the search string as a bookmark.
	   (workaround for IE5.5 feature/bug) */
	var l = p.topicFrame.location;
	if (l.search != "" && l.hash == "")
		l.hash = "#" + l.search.substring(1);
	this.document = p.topicFrame.document;
	this.url = p.relativeToRoot(p.topicFrame.location.href);
	this.remote = !p.normaliseUrl(p.topicFrame.location.href).startsWith(p.rootFolder, true);
	
	// Start with the global settings.
	var c = this.parent.configuration;
	settings = c.linksSettings.global.copy();
	if (settings.linksFile == "") {
		var temp = this.url.split("/");
		if (!c.navigationFiles.siteMap)
			temp[0] = c.linksFolder.slice(0, -1);
		else
			temp[2] = c.linksFolder.slice(0, -1);
		settings.linksFile = temp.join("/");
	}

	// If a navigation tool was specified on the command line, use it.
	if (this.parent.urlParams[HelpSystem.NAVIX])
	{
		settings.index = parseInt(this.parent.urlParams[HelpSystem.NAVIX]);
		if (isNaN(settings.index))
		{
			alert("Invalid navigation index - zero used.");
			settings.index = 0;
		}
	}

	var tool = ""
	// If a navigation tool was specified on the command line, use it.
	if (this.parent.loading)
	{
		if (this.parent.urlParams[HelpSystem.NAVTOOL])
		{
			tool = this.parent.urlParams[HelpSystem.NAVTOOL];
		}
		else if (this.parent.urlParams[HelpSystem.INDEXREF])
		{
			tool = "index";
		}
		if (!!tool)
		{
			// Check that tool refers to a valid navigation tool.
			for (var t in this.parent.navigationTools)
			{
				if (t != "none")
				{
					if (tool.isEqualIgnoreCase(this.parent.navigationTools[t].toString()))
					{
						settings.tool = tool;
						break;
					}
				}
			}
			if (settings.tool != tool)
			{
				alert("Invalid navigation tool - default used.");
			}
		}
	}

	// Get the topic settings.
	var tf = this.parent.topicFrame;
	// For each setting, if set, use it...
	if (typeof tf.linksIndex != "undefined")
		settings.index = tf.linksIndex;
	// Get the linksFile setting.
	var navTool = (typeof tf.linksFile == "undefined" ? "" : tf.linksFile);

	// If the linksFile setting specifies a tool other than links...
	if (navTool.isIn("contents", "index", "search", "*") != -1) {
		// Set the tool.
		settings.tool = navTool;
		// No links file.
		settings.linksFile = "";
	}
	// Otherwise, if a links file is specified...
	else if (navTool != ""){
		// Use the links tool with the specified links file.
		settings.tool = "links";
		var temp = settings.linksFile.split("/");
		temp[temp.length - 1] = navTool;
		settings.linksFile = temp.join("/");
	}
	// Otherwise, go with the global settings for now.
	
	// If topic is dynamic or remote...
	if (this.parent.topicFrame.dynamic || this.remote)
		settings = c.linksSettings["default"].copy();
	
	// If loading the help system and the navigation tool is "*"...
	if (this.parent.loading && settings.tool == "*") {
		temp = settings.linksFile;
		var niTemp = settings.index;
		settings = c.linksSettings.initial.copy();
		settings.linksFile = temp;
		settings.index = niTemp;
	}
	
	// If using local site maps and the navigation tool is "*"...
	if (c.navigationFiles.siteMap && settings.tool == "*") {
		// If current navigation tool is contents or index...
		if (p.currentNavTool.toString().isIn("Contents", "Index") != -1)
			// Set navigation tool to current navigation tool.
			settings.tool = p.currentNavTool.toString().toLowerCase();
	}
	
	// Set topic properties.
	this.linksFile = settings.linksFile;
	this.navTool = settings.tool;

	var nf = this.parent.configuration.navigationFiles;
	nf.index = settings.index;
	nf.setControlPath(this.url);
	this.contentsPage = nf.getContents();
	this.indexPage = nf.getIndex();
	this.searchPage = nf.getSearch();
	this.searchData = nf.getSearchData();
	this.linksFrame = nf.getLinksFrame();
	this.glossaryFrameset = nf.getGlossary();
	
	this.topicId = this.getTopicId();

	// If the comment form module is loaded...
	if (this.parent.commentForm) {
		this.parent.commentForm.addCommentLinks();
	}
	
	this.addExitButton();
}

Topic.prototype.getTopicId = function tpcGetTopicId() {
	var m;
	
	if (this.document.getElementsByTagName)
		m = this.document.getElementsByTagName("meta");
	else
		m = this.document.all.tags("meta");
		
	for (var i = 0; i < m.length; i++) {
		if (m[i].name.toLowerCase() == "topicid") {
			return (m[i].content);
		}
	}
}

	/* Event handler for toggle display events.
		Parameters:
			targetId - The id of the element whose visibility is to be toggled.
			source - Optional. A reference to the element that was the source
				of the event.
			prompt - Optional. The text to display on the button while the
				target element is displayed. The default is "Hide result".
	*/
Topic.prototype.toggleDisplay = function tpcToggleDisplay(targetId, source, prompt) {
	var target;
	var defaultPrompt;
	var newPrompt;

	// Get a reference to the target element.	
	if (document.getElementById)
		target = this.document.getElementById(targetId);
	else
		target = this.document.all[targetId];

	// If the target is hidden...
	if (target.style.visibility == "hidden") {
		// Make it visible.
		target.style.position = "static";
		target.style.visibility = "visible";
		defaultPrompt = Topic.PROMPTHIDE;
	}
	else {
		// Hide it.
		target.style.position = "absolute";
		target.style.visibility = "hidden";
		defaultPrompt = Topic.PROMPTSHOW;
	}

	// If a prompt has been supplied, use it; otherwise, use the default.
	newPrompt = (typeof prompt == "string" ? prompt : defaultPrompt);
			
	// If a source was specified...
	if (typeof source == "object") {
		// If we started from visible...
		if (!this.displayPrompts[source.id]) {
			// If the source is a button...
			if (source.value) {
				// Save the button text.
				this.displayPrompts[source.id] = source.value;
				// Set the button text.
				source.value = newPrompt;
			}
			else {
				// Save the current prompt.
				this.displayPrompts[source.id] = source.innerHTML;
				// Set the new prompt
				source.innerHTML = newPrompt;
			}
		}
		else {
			// Restore the previous prompt.
			// If the source is a button...
			if (source.value)
				// Set the button text.
				source.value = this.displayPrompts[source.id];
			else
				// Otherwise, set the inner HTML.
				source.innerHTML = this.displayPrompts[source.id];
			// Clear the saved prompt.
			delete this.displayPrompts[source.id];
		}
	}
}

Topic.prototype.addExitButton = function tpcAddExitButton() {
	var c = this.parent.configuration;
	if (c.exitButtonCaption != "") {
		var para = this.document.createElement("p");
		para.className = "exitButton";
		var btn = this.document.createElement("button");
		btn.value = exitButtonCaption;
		btn.onclick = function () { top.close(); };
		btn.className = "exitButton";
		para.appendChild(btn);
		this.document.body.insertBefore(para, this.document.body.firstChild);
	}
}


// *** Set Topic class variables ***
Topic.PROMPTHIDE = "Hide result";
Topic.PROMPTSHOW = "Show result";

