// first initialization
function navInit()
{
	if (parent.frames.length > 0)
	{
		// location of frames
		navBrain			=	parent.navBrain
		topMenu				=	parent.navMenu	
		defaultTargetFrame	=	parent.navContent
		setPopID = ''
	}
	// open the very first page
	if (dataObj.top.childs[0])
	{openPage(dataObj.top.childs[0])}
	else
	{openPage('top')}
	// or set active page
	//	setActive('ncsDB_82')
	// write the menu
	//writeViewMenu()
	document.getElementById('siteMap').innerHTML	=	writeLinks('top','view','childs','<a href="#" onClick="javascript:{navBrain.openPage(\'<navObj:ID/>\',parent.navContent);}" onMouseover="window.status=\'<navObj:fullName/>\'; return true;" onMouseOut="window.status=\'\'; return true;"><img src="/<navObj:linkImg/>" width="119" height="22" border="0" alt="<navObj:fullName/>"></a>')
}

/// configure options: ////////////////////////////////////////////////////////
dbName					=	'navDB' // default value for which DB to use - used in administration
lastDBname				=	'navDB' // default value for which DB that is last in the DB listing - used in administration
useIndentString 		=	'no'	// options are 'yes', 'no' or 'allways' ('allways' even overrides customized input)
useIcons				=	false	// options are: 'yes',false or '[level]' where level is the level sent to writeLinks, example: allChilds(tree-structures)
all_expanded			=	'no'	// options are 'yes' or 'no'
useTreeLevels			=	'yes'	// options are: 'yes' or false - use special classes for tree-structures?
skipLevel				=	'top'		//  set level to skip when doing allParents-listing - normally : 'top', to skip the current active page: 'current'
useFirstChildForFolders	=	'yes'	// options are 'yes' or 'no' - ? open first child as default for folders ?
webPath					=	''		//
imgPath					=	'/sys_images/'	
defaultLinkDesc			=	'click to view'	//
defaultAction			=	doNothing // set the normal click action (for IE5+)
// html markup for menu's
// html markup for menu's
connect_doc			=	''//'<img src="'+imgPath+'continue_line.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
connect_last_doc	=	''//'<img src="'+imgPath+'end_line.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
connect_rows		=	''//'<img src="'+imgPath+'connect_line.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
connect_rows		=
empty_image			=	'<img src="'+imgPath+'pix.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
icons = new Array() // icons in array should be named : [open/closed_][templatename]_icon
icons['file_icon']			=	'<img src="'+imgPath+'fileDoc.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
icons['image_icon']			=	'<img src="'+imgPath+'imageDoc.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
icons['URL_icon']			=	'<img src="'+imgPath+'webDoc.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
icons['default_icon']		=	'<img src="'+imgPath+'paperDoc.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
icons['closed_folder_icon']	=	'<img src="'+imgPath+'closed_folder.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
icons['open_folder_icon']	=	'<img src="'+imgPath+'open_folder.gif" width="15" height="18" alt="" border="0" hspace="0" vspace="0" align="absmiddle">'
icons['open_sys_folder_icon']		=	'<img src="'+imgPath+'open_sys_folder.gif" width="15" height="18" alt="" border="0" align="absmiddle">'
icons['closed_sys_folder_icon']		=	'<img src="'+imgPath+'closed_sys_folder.gif" width="15" height="18" alt="" border="0" align="absmiddle">'
icons['open_top_icon']		=	'<img src="'+imgPath+'open_folder.gif" width="15" height="18" alt="" border="0" align="absmiddle">'
icons['closed_top_icon']	=	'<img src="'+imgPath+'closed_folder.gif" width="15" height="18" alt="" border="0" align="absmiddle">'
icons['open_pop_icon']		=	''//'<img src="'+imgPath+'open_pop_arrow.gif" width="9" height="9" alt="" border="0" align="absmiddle">'
icons['close_pop_icon']		=	''//'<img src="'+imgPath+'close_pop_arrow.gif" width="9" height="9" alt="" border="0" align="absmiddle">'
linklist_delimiter			=	'<br>' // default delimiter 
linkListEnd					=	'<br>'	// default listings end
allchilds_delimiter			=	'<br>'	// delimiter for allChilds listings (tree structures)
allparents_delimiter		=	'<span class="allparents_delimiter"> - </span>'	// delimiter for allParents listings (path structures)
///////////////////////////////////////////////////////////////////////////////
///    Navigation Object by niclas@nomedia.net 2002       /////////////////////
///////////////////////////////////////////////////////////////////////////////

// Add some functionality to Arrays
Array.prototype.addTo	=	addToArray
// Create initial active pages array
activePages = new Array()
// Create dataObject
createDataObj()

// dataObject ///////
function createDataObj()
{
	for (count = 0; count < dataList.length ; count++)
	{
		if (dataList[0].split('\t')[0] == 'ID')
		{
			dataObj[dataList[count].split('\t')[0]] = new dataObj(dataList[count].split('\t'))
			dataObj[dataList[count].split('\t')[0]].currPos =	count
		}
		else
		{
			dataObj[count]	=	new dataObj(dataList[count].split('\t'))
			dataObj[count].ID	=	count
			dataObj[count].currPos	=	count
		}
	}
	if (dataObj[dataList[1].split('\t')[0]].ID == 'top') //handle standalone top-level doc without parentID
	{
		dataObj['topParent']	=	new dataObj(dataList[1].split('\t'))
		dataObj['topParent'].ID = 'topParent'
		dataObj.top.parentId	=	'topParent'
		//dataObj.topParent.childs.addTo('top')
	}
	if (dataObj[dataList[1].split('\t')[0]].parentId)
	{
		for (count = 1; count < dataList.length ; count++)
		{
			if (dataList[0].split('\t')[0] == 'ID')
			{
				if (dataObj[dataObj[dataList[count].split('\t')[0]].parentId])
				{
					dataObj[dataObj[dataList[count].split('\t')[0]].parentId].childs.addTo([dataList[count].split('\t')[0]])
					dataObj[dataObj[dataList[count].split('\t')[0]].parentId][dataList[count].split('\t')[0]] =  dataObj[dataList[count].split('\t')[0]]
				}
				else if (dataObj[dataList[count].split('\t')[0]].ID == 'top')
				{
					// do something with the root document
				}
				else if (dataList[count].split('\t')[0] == 'system')
				{
					dataObj.system.parentId	=	'topParent'
					dataObj.topParent.childs.addTo('system')
										dataObj[dataObj[dataList[count].split('\t')[0]].parentId][dataList[count].split('\t')[0]] =  dataObj[dataList[count].split('\t')[0]]
				}
				else
				{
					alert('Error:\ndocument "'+[dataList[count].split('\t')[0]]+'" refers to "'+dataObj[dataList[count].split('\t')[0]].parentId+'" as parent\n\n : "'+dataObj[dataList[count].split('\t')[0]].parentId+'" does not exist!')
				}
			}
			else
			{
				if (dataObj[dataObj[count].parentId])
				{
					dataObj[dataObj[count].parentId].childs.addTo(count)
					dataObj[dataObj[count].parentId][count]	=	dataObj[count]
				}
				else
				{
					alert('Error:\ndocument "'+count+'" refers to "'+dataObj[count].parentId+'" as parent\n\n : "'+dataObj[count].parentId+'" does not exist!')	;
				}
			}
		}
	}
}
// the actual navigation object - creates properties named by headings in dataList and values from input(row from dataList)
function dataObj(inData)
{
	for (var rows = inData.length ; rows >= 0  ; rows--)
	{
		this[dataList[0].split('\t')[rows]]	=	inData[rows]
	}
		if (((this.ID)&&(this.template))&&(!this.href))
	{
		// edit this line to fit your system
		this.href	= '/cgi-bin/docSpinner.pl?&ID='+this.ID+'&DB='+dbName;
	}
	this.childs	=	new Array
}
// end dataObject ///

// build navigation
function writeLinks(input,action,level,linkString,custom_delimiter,custom_linkListStart,custom_linkListEnd) 
{
	linkList	=	new Array()
	var docList		=	0
	if (custom_delimiter)
	{
		var delimiter	=	custom_delimiter
	}
	else
	{
		var delimiter	=	linklist_delimiter
	}
	if (level == 'childs')
	{
		docList = 	dataObj[input].childs
	}
	else if (level == 'parent')
	{
		docList = 	dataObj[dataObj[dataObj[input].parentId].parentId].childs
	}
	else if (level == 'singleParent')
	{
		docList = 	new Array(dataObj[input].parentId)
	}
	else if (level == 'singleCurrent')
	{
		docList = 	new Array(dataObj[input].ID)
	}
	else if (level == 'allParents')
	{
		docList = 	new Array
		parentTest = input
		testCount=100
		while (dataObj[parentTest].parentId)
		{
			if ((parentTest != skipLevel)&&((skipLevel != 'current')||(parentTest != input)))
			{
			docList[testCount]	=	parentTest
			}
			parentTest	=	dataObj[parentTest].parentId
			testCount--
		}
		delimiter	=	allparents_delimiter
	}
	else if (level == 'allChilds')
	{
		docList = findChilds(input)
		delimiter	=	allchilds_delimiter
	}
	else
	{
		docList = 	dataObj[dataObj[input].parentId].childs
	}
	if (docList)
	{
		var rows = docList.length - 1
		OBJITEMS:	for (var i = 0 ; i <= rows;i++)
		{
			if (level == 'allChilds')
			{
				var childLevel	=	docList[i].split('::')[1]
				var activeLevels	=	docList[i].split('::')[2].split('\t')
				docList[i]	=	docList[i].split('::')[0]
			}
			//if ((docList[i])&&(dataObj[docList[i]].ID!='system')) // används i navObj
			if (docList[i])
			{
				if ((dataObj[docList[i]].parentId == 'top')&&(level == 'allChilds'))
				{
					continue OBJITEMS;
				}
				// manage target
				if (dataObj[docList[i]].target == '_top')
				{
					targetFrame =	'parent'
				}
				else if (dataObj[docList[i]].target == '_new')
				{
					targetFrame	=	'window.open()'
				}
				else if (dataObj[docList[i]].target)
				{
					targetFrame	=	'parent.'+[dataObj[docList[i]].target]
				}
				else
				{
					targetFrame	=	'parent.'+defaultTargetFrame.name
				}
				// set class
				(dataObj[docList[i]].parentId == 'top')	?	
					preRefClass = 'top'
				:	
					((level == 'allChilds')&&(useTreeLevels))	?	
						(dataObj[docList[i]].parentId == 'top')	?	
							preRefClass = 'treeTop'
						:
							preRefClass = 'treeSub'
					:
						preRefClass = 'sub'	
				;
				(setPopID)	?
					(docList[i] == setPopID)		?	
						refClass	=	preRefClass+'NavActiveAction'	
						:
						(activePages[docList[i]])		?	
						refClass	=	preRefClass+'NavActive'
						:
						refClass	=	preRefClass+'Nav'
					:
					(activePages[docList[i]])		?	
						refClass	=	preRefClass+'NavActive'
						:
						refClass	=	preRefClass+'Nav'
				;
				
				//handle fullName and linkdescription
				(dataObj[docList[i]].fullName)	?	
				fullName	=	dataObj[docList[i]].fullName
				:	
					(dataObj[docList[i]].template == 'static')?	
					fullName = dataObj[docList[i]].ID	
					:
					fullName = dataObj[docList[i]].href
					;
				;
				
				// use expansion icon when childs are present in allChilds-trees
				(dataObj[docList[i]].linkDesc)	?	linkDesc	=	dataObj[docList[i]].linkDesc	:	linkDesc = fullName	;
				
				if ((level == 'allChilds')&&(dataObj[docList[i]].childs.length >= 1))
				{
					expand_ico = 'pop_icon'
				}
				else
				{
					expand_ico = ''
				}
				
				// indent for child level - loop through indent-columns(levels)
				var indentString = ''
				for (var count = 0;count <= childLevel; count++)
				{
					if (activeLevels[count] == 'y')	// should there be a connector downwards in this column?
					{
						if (count == childLevel)	// the last column for this Item
						{
							indentString = indentString+connect_doc
						}
						else	 // the other columns for this Item
						{
							indentString = indentString+connect_rows
						}
					}
					else	
					{
						// ful-hack för webspinner
						if ((dataObj[docList[i]].ID == 'top')&&(dbName != lastDBname))
						{
							indentString = indentString+connect_doc
						}
						else if (count == childLevel)	// the last column for this Item
						{
							indentString = indentString+connect_last_doc
						}
						// ful-hack för webspinner
						else if ((count == 0)&&(dbName != lastDBname))
						{
							indentString = indentString+connect_rows
						}
						else	// the other columns for this Item
						{
							indentString = indentString+empty_image
						}
					}
				}
				// set icons
				if ((useIcons == level)||(useIcons == 'yes'))
				{
					var iconName = dataObj[docList[i]].template.replace(/_none/,'')
					if (icons[iconName+'_icon'])
					{
						use_icon	=	icons[iconName+'_icon']
					}
					else if ((icons['open_'+iconName+'_icon'])&&(icons['closed_'+iconName+'_icon']))
					{
						use_icon	=	(activePages[docList[i]])		?	icons['open_'+iconName+'_icon'] : icons['closed_'+iconName+'_icon'] ;
					}
					else
					{
						use_icon	=	icons['default_icon']
					}
				}
				else
				{
					use_icon	=	''
				}
				// use indent, icon etc with expansion 
				if  (expand_ico) 
				{
					(activePages[docList[i]])	?
					popAction = 'close'
					:
					popAction = 'open'
					;
					expand_start	=	'<span class="pop_expand">'
					expand_end	=	'</span>'
					if (useIndentString!='no')
					{
						indentString = indentString+expand_start+'<a ID="'+dataObj[docList[i]].ID+'" href="javascript:{parent.navBrain.popPage(\''+dataObj[docList[i]].ID+'\',\''+popAction+'\')}" class="expand" onMouseover="window.status=\''+linkDesc+'\'; return true;" onMouseOut="window.status=\'\'; return true;">'+icons[popAction+'_'+expand_ico]+'</a><img src="'+imgPath+'pix.gif" border="0" width="6" height="18" alt="" align="absmiddle">'
					}				}
				else
				{
				expand_end	=	''
				}
				// add to links list
				if (action == 'view') // for compability
				{
					if (linkString)
					{
						dbHeadings = dataList[0].split('\t')
						var headings = dbHeadings.length - 1
						var itemLinkString =	linkString
						for (var inte = 0 ; inte <= headings;inte++)
						{
							//var myTestStr	=	"\\$"+dbHeadings[inte]+"\\"
							var myTestStr	=	"<navObj:"+dbHeadings[inte]+"/>"
							
							var myPattern	=	new RegExp(myTestStr,"g")
							itemLinkString	=	itemLinkString.replace(myPattern, dataObj[docList[i]][dbHeadings[inte]]) + expand_end
						}
						if (useIndentString == 'allways')
						{
							linkList.addTo(indentString+itemLinkString)
						}
						else
						{
							linkList.addTo(itemLinkString)
						}
					}
					else
					{
						// edit this line as you please
						linkList.addTo(indentString+'<a href="javascript:{navBrain.openPage(\''+dataObj[docList[i]].ID+'\','+targetFrame+');}" class="'+refClass+'" onMouseover="window.status=\''+linkDesc+'\'; return true;" onMouseOut="window.status=\'\'; return true;">'+use_icon+fullName+'</a>'+ expand_end)
					}
				}
			}
		}
	}
	//return links
	var linkListStart =	(custom_linkListStart)?custom_linkListStart:'';
	var linkListEnd =	(custom_linkListEnd)?custom_linkListEnd:'';
	return(linkListStart+linkList.join(delimiter)+linkListEnd)
}
function setActive(input)
{
// kolla ev extrarader här !!!
	activePage	=	input
	while(input)
	{
		activePages[input]	=	true
		if (dataObj[input])
		{
			if (activePages[dataObj[input].parentId])
			{
				input = false
			}
			else if (dataObj[input].parentId)
			{
				input = dataObj[input].parentId
			}
			else
			{
				input = false
			}
		}
		else
		{
			input = false
		}
	}
	activate()
}
function activate()
{
	var	i = 0
	while(i < activePages.length)
	{
		//here you add any common tasks for all active pages
		i++
	}
}
function findChilds() // loop through all childs and return a complete list
{
	var input	=	findChilds.arguments[0]
	var level	=	findChilds.arguments[1]
	var count	=	0
	if (!level)
	{
		levelFind = new Array()
		allChildsList = new Array()
		levelCount = 0
	}
	for (count = 0; count < dataObj[input].childs.length ; count++)
	{
		if(count < dataObj[input].childs.length-1)
		{
			levelFind[levelCount] = 'y'
		}
		else
		{
			levelFind[levelCount] = 'n'
		}
		allChildsList.addTo(dataObj[input].childs[count]+'::'+levelCount+'::'+levelFind.join('\t'))
		if (dataObj[dataObj[input].childs[count]].childs) // dig deeper
		{
			if ((all_expanded =='yes')||(activePages[dataObj[input].childs[count]]))
			{
				levelCount++
				this.findChilds(dataObj[input].childs[count],levelCount)
			}
		}
	}
	levelCount--
	return allChildsList
}
/// addon function for Arrays ///
function addToArray(input)
{
	this[this.length]	=	input
}
/// open Page ///
function openPage(ID,target)
{
	activePages	=	new Array()
	setActive(ID)
	setPopID = ID
	if (dataObj[ID].template == 'folder')
	{
		if (useFirstChildForFolders == 'yes')
		{
			openPage(dataObj[ID].childs[0])
		}
	}
	else
	{
		if (target)
		{
			if (target.location)
			{
				target.location = dataObj[ID].href
			}
		}
		else
		{
			defaultTargetFrame.location = dataObj[ID].href
		}
	}
	//writeViewMenu()
	//siteMap.innerHTML = 	writeLinks('top','view','allChilds')
}
/// pop Page ///
function popPage(ID,action)
{
	activePages	=	new Array();
	(action == 'close')	?
	setActive(dataObj[ID].parentId)	:
	setActive(ID)	;

//	writePopMenu('pop')
	siteMap.innerHTML = 	writeLinks('top','view','allChilds')
}
/// print menu ///
function writeViewMenu()
{
topMenu.document.open("text/html")
topMenu.document.write('\n\
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">\n\
	\n\
	<html>\n\
	<head>\n\
		<title> </title>\n\
		<link rel="stylesheet" type="text/css" href="'+webPath+'webStyles.css">\n\
		<script language="JavaScript1.2" src="'+webPath+'pageScript.js" type="text/javascript">\n\
		</script>\n\
	</head>\n\
	\n\
	<body class="topNav">\n\
	<div id="topNavDiv">'+ writeLinks('top','view','childs') + '</div>\n\
	</body>\n\
	</html>\n\
')
topMenu.document.close()
}

function doNothing()
{
	return false
}
