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

 DataObjects.js, Version 3.2, 17-Mar-2009
 Copyright Northgate Information Solutions UK Limited, 2002-2009

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

/***********************
 DataObjects class

 Constructor:
	DataObjects(helpSystem)
 Properties:
	children
	controlFolder
	helpSystem
	index
	target
	topicFolder
	window
 Methods:
	addNext(obj)
	load()
	initialise()

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

// *** Constructor for DataObjects class ***
function DataObjects(helpSystem) {
	this.helpSystem = helpSystem;
	
	var c = helpSystem.configuration;
	this.window = helpSystem.dataFrame;
	this.target = helpSystem.topicFrame;
	this.controlFolder = helpSystem.rootFolder + c.controlFolder;
	this.topicFolder = helpSystem.rootFolder + c.topicFolder;
	
	this.children = new Array();
	this.index = 0;
}

// *** DataObjects instance methods ***
DataObjects.prototype.addNext = function dosAddNext(obj) {
	this.children[this.children.length] = obj;
	var c = this.children[this.children.length - 1];
	c.parent = this;
	c.window = this.window;
	c.target = this.target;
	c.controlFolder = this.controlFolder;
	c.topicFolder = this.topicFolder;
}

DataObjects.prototype.load = function dosLoad() {
	this.children[this.index].load();
}

DataObjects.prototype.initialise = function dosInitialise() {
	if (this.children[this.index].initialise()) {
		while (++this.index < this.children.length) {
			if (this.children[this.index].load())
				break;
		}
	}
}

DataObjects.prototype.mouseEvent = function dosMouseEvent(e) {
	// Route the event to each of the data objects in turn.
	for (var obj in this.children) {
		if (this.children[obj].mouseEvent(e))
			return(true);
	}
	return(false);
}


/***********************
 DataObject class
	prototype for children of DataObjects object.

 Constructor:
	DataObject()
 Properties:
	controlFolder
	data
	files
	index
	parent
	target
	topicFolder
	window
 Methods:
 	setup(config)
	load()
	initialise()

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

// *** Constructor for DataObject class ***
function DataObject() {
	this.parent = null;
	this.window = null;
	this.target = null;
	this.controlFolder = "";
	this.topicFolder = "";
	this.filename = null;
	// Create an array to hold the data.
	this.data = new Array();
	this.index = 0;
}

// *** DataObject instance methods ***
DataObject.prototype.setup = function doSetup(files) {
	this.files = files;
}

DataObject.prototype.load = function doLoad() {
	// Load the first data file.
	var hs = this.parent.helpSystem;
	
	if (this.files) {
		var f = hs.absoluteUrl(this.controlFolder + this.files[0]);
		this.window.location.replace(unescape(f));
		return true;
	}
	else
		return false;
}

DataObject.prototype.initialise = function doInitialise() {
	// Get the data - called by data file's onLoad event handler.
	var hs = this.parent.helpSystem;
    var aText;
	var e;

	var d = this.window.document;
	if (d.getElementsByTagName) {
		e = d.getElementsByTagName("span");
	}
	else {
		e = d.body.all.tags("span");
	}
	
	// For each item of data...
	for (var i = 0; i < d.anchors.length; i++) {
		aText = (document.all ? d.anchors[i].innerText : d.anchors[i].text);

		// Create a data entry.
		this.data[d.anchors[i].name.toLowerCase()] = aText;
	}
	
	// For each item of data...
	for (var i = 0; i < e.length; i++)
		if (e[i].className == "data")
			// Create a data entry.
			this.data[e[i].id.toLowerCase()] = e[i].innerHTML;

	if (++this.index < this.files.length) {
		var f = hs.absoluteUrl(this.controlFolder + this.files[this.index]);
		this.window.location.replace(unescape(f));
		return(false);
	}
	else {
		this.window.location.replace(HelpSystem.BLANKPAGE);
		return(true);
	}
	
	this.loaded = true;
}

	// Dummy mouse event handler.
DataObject.prototype.mouseEvent = function doMouseEvent(e){return(false);};


/***********************
 TopicMap class

 Constructor:
	TopicMap()
 Properties:
	parent						Inherited from DataObject class.
	window						Inherited from DataObject class.
	controlFolder				Inherited from DataObject class.
	topicFolder					Inherited from DataObject class.
	files						Inherited from DataObject class.
	data						Inherited from DataObject class.
 Methods:
 	setup(files)				Inherited from DataObject class.
	load()						Inherited from DataObject class.
	initialise()				Inherited from DataObject class.
	mouseEvent(event)			Inherited from DataObject class.
	findTopicId(topicID)
	findTopicFilename(filename)

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

// *** Constructor for TopicMap class ***
function TopicMap() {}

// *** TopicMap inherits from DataObject ***
TopicMap.prototype = new DataObject();
TopicMap.prototype.constructor = TopicMap;

// *** TopicMap instance methods ***
TopicMap.prototype.findTopicId = function tmFindTopicId(topicId) {
	// Find a topic id in the topic map.
    /* Parameters:
          topicId - the topic id to find.
       If found, returns the corresponding file name; otherwise,
       returns null. */

	// Get the path corresponding to the supplied topic id.
	var path = this.data[topicId.toLowerCase()];
	if (path)
		/* The path returned is relative to the control folder;
		   we need it relative to the topics folder. */
		path = path.split("/").slice(2).join("/");
	return(path);
}

TopicMap.prototype.findTopicFilename = function tmFindTopicFilename(filename) {
	// Find a filename in the topic map.
    /* Parameters:
          filename - the name of the file to find.
       If found, returns the corresponding file name; otherwise,
       returns null. */

    var aText;

    // Comparisons will be case-insensitive.
    filename = filename.toLowerCase();

	// For each element in the map...
    for (var n in this.data) {
        aText = this.data[n].toLowerCase();

        if (filename == aText || aText.indexOf("/" + filename) != -1)
            return(this.data[n]);
    }

    return(null);       // Not found - return null.
}

