/***********************************************************************************
*
* $Workfile: index.js $ 
* SafeFile : $Archive: /ContentSystem/HelpSystem/Source/HelpSystemWeb/Scripts/index.js $
* 
* $Revision: 0 $ 
*
* Purpose : 
*
* Modified : $Author: $ $Modtime: $
*
* Initial version by Alexey Kropotin (Hound) on Oct, 2004
* Copyright 2005 Saxo Bank. All rights reserved.
***********************************************************************************/

//------------------------ Index class definition ------------------------------------

function Index()
{
	this.keyWords = new Array
	this.nkeyWords = 0
	
	this.AddKey = addKey
	this.Initialize = initializeIndex
	
	this.ScrollToObject = setScroll
	this.SearchKeyword = searchKeyword
	this.GetOffset = getOffset
}

//------------------------- Index class methods --------------------------------------

function addKey(keyWord)
{
	this.keyWords[this.nkeyWords] = keyWord
	this.nkeyWords++	
}

function initializeIndex()
{	
	for (i = 0 ; i < this.nkeyWords; i++)  
		this.keyWords[i].Initialize()
	//recalcLayout();
}

function setScroll(obj)
{
	indexDiv.scrollTop = obj.offsetParent.offsetTop;
}

function searchKeyword(obj)
{
	// HS#Prev.ID#2716
	// obj.value = obj.value.replace(/(^ +| +$)/g, '')
	obj.value = obj.value.replace(/(^ +)|( (?= ))/g, '')

	for(i = 0; i <= index.nkeyWords - 1; i++)
	{
		if (index.keyWords[i].keyName &&
			index.keyWords[i].keyName.toUpperCase().indexOf(obj.value.toUpperCase()) == 0)
		{
			setScroll(document.all['keyword' + index.keyWords[i].keyId]);
			return;
		}
	}
}

function getOffset(obj)
{
	totalOffset = 0;
	par = obj;
	prevOffset = 0
	currentOffset = 0
	
	while (par != null)
	{
		currentOffset = par.offsetTop
		if(prevOffset != currentOffset)
		{
			totalOffset += currentOffset;
			prevOffset = currentOffset
		}
		par = par.parentElement;
	}
	return totalOffset;
}

//------------------------ KeyWord class definition ---------------------------------

function KeyWord(keyName, id)
{
	this.keyName = keyName
	this.keyId = id
	this.keyLinks = new Array
	this.nkeyLinks = 0
	
	this.AddLink = addLink
	this.Initialize = initializeKeyWord
	this.DrawKeyWord = drawKeyWord
}

//---------------------- KeyWord class methods --------------------------------------

function addLink(keyLink)
{
	this.keyLinks[this.nkeyLinks] = keyLink
	keyLink.key = this
	this.nkeyLinks++
}

function initializeKeyWord()
{
	this.DrawKeyWord()
	
	for (k = 0 ; k < this.nkeyLinks; k++)  
		this.keyLinks[k].DrawKeyLink()
}

function drawKeyWord()
{
	template = "<tr class='IndexKeyWordStyle' align='center'><td colspan='2' align='left'><span class='IndexKeyWordStyle' id='keyword" +
	 + this.keyId + "' href=''>" + this.keyName + "</span></td></tr>"
	 
	 document.write(template)
}

//------------------------ KeyLink class definition ---------------------------------

function KeyLink(keyLinkId, topicName, topicId, anchor)
{
	this.id = keyLinkId
	this.topicName = topicName
	this.topicId = topicId
	this.anchor = anchor
	this.key = null
	
	this.DrawKeyLink = drawKeyLink
	this.TopicLink = topicLink
	this.IconLink = iconLink
	this.IconSrc = iconSrc
}

function clickItem(i)
{
	t = document.getElementById("span"+i)
	if (t != null && t != currentLink)
	{
		t.className = 'Selected'
		if (currentLink != null)
		{
			currentLink.className = 'IndexLinkStyle'
		}
		currentLink = t
	}
}
//---------------------- KeyLink class methods --------------------------------------

function drawKeyLink()
{
	template = "<tr onclick='javascript:clickItem(" + this.id + ")' class='IndexLinkStyle' align='center' valign='middle'>"
	
	template += "<td align='right'>" + this.IconLink() + "</td>"	
	template +="<td class='IndexLinkStyle' align='left'><span id='span" + this.id + "' class='IndexLinkStyle'>" + this.TopicLink() + "</span></td></tr>"

	document.write(template)
}

function topicLink()
{
	var param = "Topic=" + this.topicId;
	var search = window.location.search;
	if (search.match(/topic=/i))
		search = search.replace(/topic=\d+/i, param);
	else
		search = search + "&" + param;

	var ref = contentpageref + search + "#" + this.anchor
	return "<a class='IndexLinkStyle' href='" + ref + "' target='" + contentframename + "'>" + this.topicName + "</a>"
}

function iconLink()
{
	ref = this.IconSrc()
	
	if(ref.length != 0)	
		return "&nbsp&nbsp&nbsp&nbsp<img src='" + ref + "'>"
	else
		return "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"
}

function iconSrc()
{
	try
	{
		return eval("item" + this.id + "icon")
	}
	catch(e) { return "" }	
}

//------------------------ Init functions -------------------------------------------

function CreateKeyWord(keyName, id)
{
	return new KeyWord(keyName, id)
}

function CreateKeyLink(keyLinkId, topicName, topicId, anchor)
{
	return new KeyLink(keyLinkId, topicName, topicId, anchor)
}

function AddKeyWord(key)
{
	index.AddKey(key)
	return key
}

function AddKeyLink(key, keyLink)
{
	key.AddLink(keyLink)
}

//----------------------- Global Variables -------------------------------------------

index = new Index()

contentpageref = "Content.aspx"
contentframename = "content"
currentLink = null
