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

 IndexSelector.js, Version 1.1, 16-Sep-2010
 Copyright Northgate Information Solutions UK Limited, 2010

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

/********************
 IndexSelector class
 
 Constructor:
	IndexSelector()
 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:
	load()				Inherited from DataObject class.
	initialise()		Inherited from DataObject class.
	mouseEvent(event)	Handle a mouse event.
	
 ********************/
 
// *** Constructor for IndexSelector class ***
function IndexSelector() {
	this.items = null;
	this.popup = null;
	this.visible = false;
	this.previousTopic = null;
	this.indexTool = null;
}

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

// *** IndexSelector instance methods ***
IndexSelector.prototype.mouseEvent = function ixsMouseEvent(e) {
	/* Handle a mouse event.
	   Returns true if the event was handled; otherwise, false. */
	   
	// Get a reference to the element that generated the event.
	if (document.all)	// IE
		obj = e.srcElement;
	else {				// Netscape, Mozilla.
		obj = e.target;
		// In Netscape the target is a Text node.
		if (obj.nodeType == Node.TEXT_NODE)
			 obj = e.target.parentNode;
	}
	
	if (obj.tagName.search(/option/i) != -1)
		obj = obj.parentNode;

	// If the click occurred outside the popup, hide the popup.
	if (!obj.id.startsWith("indexSelector") && this.visible) {
		this.hide();
		return(true);
	}
	else
		return(false);
}

IndexSelector.prototype.createPopup = function ixsCreatePopup() {
	var doc = this.target.document;
	
	// Outer div.
	var divOuter = doc.createElement("div");
	divOuter.id = "indexSelector";
	divOuter.style.width = "400px";
	if ((DHTML.browserType == "IE"
			&& DHTML.compareVersions(DHTML.browserVersion, "7.0") != -1
			&& !DHTML.isWordTopic(this.target.document))
			|| DHTML.browserType == "FF") {
		divOuter.style.position = "fixed";
		divOuter.style.left = "50px";
		divOuter.style.top = "50px";
	}
	else {
		divOuter.style.position = "absolute";
		divOuter.style.left =
			(50 - parseInt(DHTML.getComputedStyle(this.target.document, 
				this.target.document.body, "marginLeft"))
				+ DHTML.getScrollLeft(this.target)) + "px";
		divOuter.style.top = 
			(50 - parseInt(DHTML.getComputedStyle(this.target.document, 
				this.target.document.body, "marginTop"))
				+ DHTML.getScrollTop(this.target)) + "px";
	}
	divOuter.style.border = "outset 3px ActiveBorder";
	divOuter.style.padding = "0";
	divOuter.style.backgroundColor = "ThreeDFace";
	divOuter.style.visibility = "hidden";
	divOuter.style.zIndex = "100";

	// Title bar.
	var el = doc.createElement("p");
	el.id = "indexSelectorTitle";
	//el.style.color = "CaptionText";
	//el.style.backgroundColor = "ActiveCaption";
	el.style.margin = "0";
	el.style.padding = "5px 10px";
	el.style.fontFamily = "'Trebuchet MS', Arial, Helvetica, san-serif";
	el.style.fontSize = "9pt";
	el.style.fontWeight = "bold";
	var txt = doc.createTextNode("Topics found:");
	el.appendChild(txt);
	divOuter.appendChild(el);
	
	// Inner div.
	var divInner = doc.createElement("div");
	divInner.id = "indexSelectorInner";
	divInner.style.margin = "10px";
	divOuter.appendChild(divInner);
	
	// Prompt.
	el = doc.createElement("p");
	el.id = "indexSelectorPrompt";
	el.style.fontFamily = "'Trebuchet MS', Arial, Helvetica, san-serif";
	el.style.fontSize = "9pt";
	el.style.marginTop = "0pt";
	el.style.marginBottom = "6pt";
	txt = doc.createTextNode("Click a topic, then click Display.");
	el.appendChild(txt);
	divInner.appendChild(el);
	
	// Form.
	var formChoose = doc.createElement("form");
	formChoose.name = "indexSelectorTopics";
	formChoose.style.marginTop = "0pt";
	formChoose.style.paddingTop = "0pt";
	this.form = formChoose;

	// List box.
	var pSelect = doc.createElement("p");
	el.id = "indexSelectorListBox";
	pSelect.style.fontFamily = "'Trebuchet MS', Arial, Helvetica, san-serif";
	pSelect.style.fontSize = "9pt";
	pSelect.style.marginTop = "0pt";
	pSelect.style.marginBottom = "6pt";
	
	el = doc.createElement("select");
	el.id = "indexSelectorList";
	el.name = "indexSelectorList";
	el.size = "8";
	el.style.marginTop = "0pt";
	el.style.marginBottom = "6pt";
	el.style.fontFamily = "'Trebuchet MS', , Helvetica, san-serif";
	el.style.fontSize = "9pt";
	el.style.position = "relative";
	el.style.width = "100%";
	this.listBox = el;
	pSelect.appendChild(el);
	formChoose.appendChild(pSelect);
	
	// Buttons.
	var pButtons = doc.createElement("p");
	pButtons.id = "indexSelectorButtons";
	pButtons.style.fontFamily = "'Trebuchet MS', Arial, Helvetica, san-serif";
	pButtons.style.fontSize = "9pt";
	pButtons.style.marginTop = "0pt";
	pButtons.style.marginBottom = "6pt";
	pButtons.style.textAlign = "right";

	el = doc.createElement("input");
	el.id = "indexSelectorDisplay";
	el.name = "indexSelectorDisplay";
	el.type = "button";
	el.value = "Display";
	el.style.fontFamily = "'Trebuchet MS', Arial, Helvetica, san-serif";
	el.style.fontSize = "9pt";
	this.displayButton = el;
	pButtons.appendChild(el);

	el = doc.createElement("input");
	el.id = "indexSelectorCancel";
	el.name = "indexSelectorCancel";
	el.type = "button";
	el.value = "Cancel";
	el.style.fontFamily = "'Trebuchet MS', Arial, Helvetica, san-serif";
	el.style.fontSize = "9pt";
	this.cancelButton = el;
	pButtons.appendChild(el);
	formChoose.appendChild(pButtons);
	divInner.appendChild(formChoose);

	this.popup = divOuter;	
}

IndexSelector.prototype.show = function ixsShow() {
	if (this.visible)
		this.hide();
	this.createPopup();
	var b = this.target.document.body;
	b.insertBefore(this.popup, b.firstChild);
	this.popup.style.visibility = "visible";
	this.populateList();
	this.listBox.focus();
	this.previousTopic = this.target.document;
	this.visible = true;
}

IndexSelector.prototype.hide = function ixsHide() {
	if (!!this.popup) {
		if (this.previousTopic == this.target.document)
			this.popup.parentNode.removeChild(this.popup);
		this.popup = null;
		this.visible = false;
	}
}

IndexSelector.prototype.populateList = function ixsPopulateList() {
	/* items parameter must be a string containing a list of name/value pairs.
	   In each pair, the name and the value are separated by three carets,
	   and the pairs are separated by three semicolons. */
	   
	var item;
	var opt;

	this.listBox.options.length = 0;
		
	var itemsAry = this.items.split(/;;;/);
	for (var i = 0; i < itemsAry.length; i++) {
		item = itemsAry[i].split(/\^\^\^/);
		item[0] = item[0].unescapeCharacterEntities();
		opt = new Option(item[0], item[1], i == 0, i == 0);
		this.listBox.options[this.listBox.options.length] = opt;
	}
	this.listBox.selectedIndex = 0;
}

IndexSelector.prototype.display = function ixsDisplay() {
	this.hide();
	// The topic path is relative to the control folder.
	var path = this.listBox[this.listBox.selectedIndex].value;
	// See or See also entry.
	if (path.search(/~[as]/) != -1) {
		path = path.substring(2);
		this.indexTool.scanEntries(path, true);
		this.indexTool.find.value = path.replace(/\\,/, ",");
	}
	else {
	// Make it relative to the root of the help system.
		path = path.replace(/^\.\.\//, "");
		/* That's fine for IE, but on Firefox, it's assumed to be relative
		   to the current page. */
		if (DHTML.browserType == "FF") {
			path = this.parent.helpSystem.rootFolder.pathConcat(path);
			path = this.parent.helpSystem.relativeToTopic(path);
		}
		this.target.document.location.href = path;
	}

	return false;
}

IndexSelector.prototype.keypressHandler = function ixsKeypressHandler(e) {
	if (typeof e == "undefined")
		e = this.target.event;

//	var k = (document.all ? e.keyCode : e.which);
	var k = e.keyCode;
	switch (k) {
	case 13:
		this.display();
		break;
	case 27:
		this.hide();
		break;
	default:
		break;
	}
}

