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

 Browse.js, Version 3.5, 17-Mar-2009
 Copyright Northgate Information Solutions UK Limited, 2002-2009

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

/********************
 Browse class
 
 Constructor:
	Browse()
 Properties:
	parent				Inherited from DataObject class.
	window				Inherited from DataObject class.
	target				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)	Handle a mouse event.
	
 ********************/
 
// *** Constructor for Browse class ***
function Browse() {}

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

// *** Browse instance methods ***
Browse.prototype.mouseEvent = function brsMouseEvent(e) {
	/* Handle a mouse event.
	   Returns true if the event was handled; otherwise, false. */
	var obj;
	var children;
	var topicData = "";
	var objParent = null;
	
	if (e.type.toLowerCase() != "click")
		return(false);
	
	// Get the reference to the browse sequence container.
	// Only handle the event if the container has a parent.
	if (document.all) {		// IE
		obj = e.srcElement;
		if (obj.parentElement == null)
			return(false);
		// Get a reference to the parent element.
		objParent = obj.parentElement;
		// Get a reference to the child elements.
		children = objParent.children;
		if (objParent.className == "") {
			if (!!objParent.parentElement) {
				objParent = objParent.parentElement;
			}
		}
	}
	else {					// Other browsers
		obj = e.target;
		if (obj.parentNode == null 
				|| obj.parentNode.tagName == "HTML"
				|| obj.parentNode.nodeType == 9)
			return(false);
		// Get a reference to the parent node.
		objParent = obj.parentNode;
		// Get a reference to the child nodes.
		children = objParent.childNodes;
		if (objParent.className == "") {
			if (!!objParent.parentNode) {
				objParent = objParent.parentNode;
			}
		}
	}
	//if (objParent.className != "browseButtons")
	if (objParent.className != "widgetBar")
		return(false);
	if (obj.className == "browseDisable")
		return(false);

	// Get the id of the parent element to use as a key into the browse data.
	var dataKey = objParent.id.toLowerCase();
	if (dataKey == "") {
		return(false);
	}
	// Get a reference to the required file list.
	var fileList = this.data[dataKey];
	// While there is no matching data element and the key ends with a digit...
	while (!fileList && dataKey.search(/\d$/) != -1) {
		// Remove the digit.
		dataKey = dataKey.slice(0, -1);
		// Try again.
		fileList = this.data[dataKey];
	}

	fileList = fileList.split("^^");
	var eIndex = this.findElement(obj);
	var currentElement = this.findCurrentElement(children);
	var childCount = this.countChildTags(children);
	if (eIndex == 0) {
		eIndex = currentElement - 1;
	}
	else if (eIndex == childCount - 1) {
		eIndex = currentElement + 1;
	}

	var newTopic = fileList[eIndex - 1];
	if (newTopic) {
		newTopic = newTopic.match(/href=\"?([^\"]*)"?/)[1];
		newTopic = newTopic.split(/[\/\\]/).slice(2).join("/");
		topicData = newTopic.split(/[\/\\]/).slice(-1)[0];
	}

	// If not already loaded, load the required topic.
	var oldTopic = this.target.location.href.split("/").slice(-1)[0];
	if (oldTopic != topicData) {
		this.target.location.href = this.topicFolder + newTopic;
	}

	return(true);
}

Browse.prototype.findElement = function brsFindElement(obj) {
	// Find the index of the clicked element.
	var e = -1;		// Element counter.

	var objParent = (document.all ? obj.parentElement : obj.parentNode);
	var children = (document.all ? objParent.children : objParent.childNodes);

	// For each browse button...
	for (var i = 0; i < children.length; i++) {
		var tag = (document.all ? children[i].tagName : children[i].nodeName).toLowerCase();
		// Ignore untagged text (Firefox);
		if (this.getTagName(children[i]).toLowerCase() == "span")
			e++;

		// If we've reached the clicked element...
		if (obj == children[i]) {
			// Return its index and the arrow index.
			return(e);
		}
	}
}

Browse.prototype.findCurrentElement = function brsFindCurrentElement(nodes) {
	// Find the index of the current element.
	var e = -1;
	for (var i = 0; nodes.length; i++) {
		// We're only interested in span tags.
		if (this.getTagName(nodes[i]).toLowerCase() == "span")
			e++;
		if (nodes[i].className == "browseCurrent") {
			return(e);
		}
	}
	return(-1);
}

Browse.prototype.countChildTags = function brsCountChildTags(nodes) {
	// Return the number of tags in the specified nodes collection.
	var c = 0;
	for (var i = 0; i < nodes.length; i++) {
		if (this.getTagName(nodes[i]).toLowerCase() == "span")
			c++;
	}
	return(c);
}

Browse.prototype.getTagName = function brsGetTagName(element) {
	return((document.all ? element.tagName : element.nodeName).toLowerCase());
}

