// ********** tree routines *****************
var TeamWriter_Item = new Array();         //is filled from [0..menudepth]
var TeamWriter_MenuItemById = new Array(); //is filled from [0..]
var TeamWriter_id = 0;
var aItemSeparator = "\x09";
var aRowSeparator = "#@#";

var script_executed=false;

var cookieNm = "TeamWriter_Menu_state";
function WriteCookie(aVal)
{
	document.cookie=cookieNm+"="+aVal+";path=/";
}
function ReadCookie()
{
	if (document.cookie=='')
		return "";
	else
	{
		var firstChar,lastChar;
		var theBigCookie=document.cookie;
		firstChar=theBigCookie.indexOf(cookieNm);
		var NN2Hack=firstChar+cookieNm.length;
		if ((firstChar!=-1)&&(theBigCookie.charAt(NN2Hack)=='='))
		{
			firstChar+=cookieNm.length+1;
			lastChar=theBigCookie.indexOf(';',firstChar);
			if (lastChar==-1) lastChar=theBigCookie.length;
			return unescape(theBigCookie.substring(firstChar,lastChar));
		}
		return "";
	}
}
function CurrentFilename()
{
	var aStr = location.href;
	slashpos = aStr.indexOf("/");
	while (slashpos >= 0)
	{
		aStr = aStr.substring(slashpos+1)
		slashpos = aStr.indexOf("/");
	}
	return aStr.substring(slashpos+1);
}

function TeamWriter_MenuItem(text, url, target) {
  this.text      = text ? text : "";
  this.url       = url ? url : "";
  this.target    = target ? target : "";

  this.itemindex = null;
  this.parent    = null;
  this.submenu   = null;
  this.id        = TeamWriter_id++

  TeamWriter_MenuItemById[this.id]  = this;

  this.TeamWriter_AddSubmenuItem   = TeamWriter_AddSubmenuItem;
  this.TeamWriter_DeleteItem       = TeamWriter_DeleteItem;
  this.TeamWriter_InsertItemBefore = TeamWriter_InsertItemBefore;
  this.TeamWriter_InsertItemAfter  = TeamWriter_InsertItemAfter;
}

function TeamWriter_Menu() {
  this.items   = new Array();
  this.parentMenuItem = null;
  this.TeamWriter_AddItem = TeamWriter_AddItem;
  this.TeamWriter_SwapItemByIndex  = TeamWriter_SwapItemByIndex;

  this.TeamWriter_AddItemsFromString = TeamWriter_AddItemsFromString;
  this.TeamWriter_StoreToString    = TeamWriter_StoreToString;
  this.TeamWriter_FindUrl          = TeamWriter_FindUrl;
  this.TeamWriter_GenerateMenu     = TeamWriter_GenerateMenu;
}

function TeamWriter_AddSubmenuItem(menuitem) {
  if (this.submenu == null) {
    this.submenu = new TeamWriter_Menu();
    this.submenu.parentMenuItem = this;
  }
  return this.submenu.TeamWriter_AddItem(menuitem)
}

function TeamWriter_DeleteItem() {
//bubble this item to the end of array and then discard
  var temp;
  for (var i = 0; i < this.parent.items.length-1; i++) {
    if (this.parent.items[i] == this) {
      TeamWriter_SwapItem(this.parent.items[i], this.parent.items[i+1])
    }
  }
  temp = this;
  this.parent.items.length--;
  return temp;
}

function TeamWriter_AddItem(menuitem) {
  this.items[this.items.length] = menuitem;
  menuitem.itemindex = this.items.length-1;
  menuitem.parent = this;
  return menuitem;
}

function TeamWriter_InsertItemBefore(menuitem) {
// add menu item to the end of array and then bubble to the preferred position
  this.parent.TeamWriter_AddItem(menuitem);
  for (i = this.parent.items.length-2; i>=0 ; i--) {
    TeamWriter_SwapItem(this.parent.items[i], this.parent.items[i+1])
    if (this.parent.items[i+1] == this) {
      break;
    }
  }
  return menuitem.id;
}

function TeamWriter_InsertItemAfter(menuitem) {
// add menu item to the end of array and then bubble to the preferred position
  this.parent.TeamWriter_AddItem(menuitem);
  for (i = this.parent.items.length-2; (i>=0) && (this.parent.items[i] != this) ; i--) {
    TeamWriter_SwapItem(this.parent.items[i], this.parent.items[i+1])
  }
  return menuitem.id;
}

function TeamWriter_AddItemsFromString(aString) {

  function TeamWriter_SyncMenuItemById(menu){
    for (var i = 0; i < menu.items.length; i++) {
        TeamWriter_MenuItemById[menu.items[i].id] = menu.items[i]
        if(menu.items[i].submenu!=null) {
          TeamWriter_SyncMenuItemById(menu.items[i].submenu);
        }
      }
  }

  var TeamWriter_temp_id = -1;
  var i, k, aFirstLevel;
  var aMenuItem = new Array();

  var aRow = aString.split(aRowSeparator);
  var aCol;
  for (i = 0; i < aRow.length-1; i++) {
    aCol = aRow[i].split(aItemSeparator);

    theLevel =  aCol[0];
    theId =     aCol[1];
    theText =   aCol[2];
    theUrl =    aCol[3];
    theTarget = aCol[4];

    if (i==0) {
      aFirstLevel = theLevel;
    }

    if (theLevel > aFirstLevel) {
      aMenuItem[theLevel] = aMenuItem[theLevel-1].TeamWriter_AddSubmenuItem(new TeamWriter_MenuItem(theText, theUrl, theTarget));
    }
    else {
      aMenuItem[theLevel] = this.TeamWriter_AddItem(new TeamWriter_MenuItem(theText, theUrl, theTarget));
    }
    aMenuItem[theLevel].id = (1*theId);

    if (TeamWriter_temp_id < ((1*theId) + 1)) {
      TeamWriter_temp_id = (1*theId) + 1; //trick to make sure we add to an integer and not a string
    }
  }

  if (TeamWriter_temp_id != -1){
    // Resync the id administration;
    for (i=0; i<=TeamWriter_temp_id; i++) {
      TeamWriter_MenuItemById[i] = null;
    }
    TeamWriter_SyncMenuItemById(this);

    TeamWriter_id = TeamWriter_temp_id;
  }
}

function TeamWriter_StoreToString(aLvl) {
//Store tree to a String with aLvl as the requested rootlevel
//If aLvl is not specified a rootlevel of 0 is default.

  var i, resultStr = "";
  if (aLvl==null) {aLvl=0;}
  aLvl++;
  for (i = 0; i < this.items.length; i++) {
    resultStr += aLvl + aItemSeparator + this.items[i].id + aItemSeparator + this.items[i].text + aItemSeparator + this.items[i].url + aItemSeparator + this.items[i].target + aRowSeparator;
    if(this.items[i].submenu!=null) {
      resultStr += this.items[i].submenu.TeamWriter_StoreToString(aLvl);
    }
  }
  return resultStr;
}

function TeamWriter_SwapItemByIndex(a, b) {
  var temp = this.items[a];
  var idx1 = this.items[a].itemindex;
  var idx2 = this.items[b].itemindex;
  this.items[a] = this.items[b];
  this.items[a].itemindex = idx1;
  this.items[b] = temp;
  this.items[b].itemindex = idx2;
}

function TeamWriter_SwapItem(menuitem1, menuitem2) {
  var temp = menuitem1.parent.items[menuitem1.itemindex];
  var aIdx1 = menuitem1.parent.items[menuitem1.itemindex].itemindex;
  var aIdx2 = menuitem2.parent.items[menuitem2.itemindex].itemindex;

  menuitem1.parent.items[aIdx1] = menuitem2.parent.items[menuitem2.itemindex];
  menuitem1.parent.items[aIdx1].itemindex = aIdx1;
  menuitem2.parent.items[aIdx2] = temp;
  menuitem2.parent.items[aIdx2].itemindex = aIdx2;
}

function TeamWriter_FindUrl(aUrl){
  var i, aResult;
  aResult = -1;
  for (i = 0; (i < this.items.length) && (aResult==-1); i++) {
    if (this.items[i].url == aUrl) {
        aResult = this.items[i].id;
    }
    else if(this.items[i].submenu!=null) {
        aResult = this.items[i].submenu.TeamWriter_FindUrl(aUrl);
    }
  }
  return aResult;
}

function TeamWriter_GenerateMenu( aLvl, aIdList )
{
	var i,j,mainLvlCnt;
	mainLvlCnt=0;
	if (aLvl==null)
		aLvl=0;
	aLvl++;

	for (i = 0; i < this.items.length; i++)
	{
//		if (this.items[i].url == "")
//			this.items[i].url = CurrentFilename();

		menuItemHighlighted = (aIdList[0]==this.items[i].id);
		aIdFoundInList = false;
		for (j = 0; j < aIdList.length ; j++)
		{
			if (this.items[i].parent.parentMenuItem!=null)
				if (aIdList[j] == this.items[i].parent.parentMenuItem.id)
				{
					aIdFoundInList = true;
					break;
				}
		}

		if (aLvl == 1)
		{
			indentationStr = "&nbsp;";
			mainLvlCnt++;
		}
		else if (aLvl == 2)
			indentationStr = "";
		else if (aLvl == 3)
			indentationStr = "";
		if ( (aLvl == 1) || aIdFoundInList )
		{
			document.write('<TR>');
			if ( this.items[i].text == "")
				document.write('<TD CLASS="menuitem menuitemlvl'+aLvl+' menuitem'+mainLvlCnt+'"><TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="100%" BORDER="0"><TR><TD CLASS="menuitem menucellleft'+aLvl+' menuitemlvl'+aLvl+'">&nbsp;</TD><TD CLASS="menuitem menucellmiddle'+aLvl+' menuitemlvl'+aLvl+'">&nbsp;</TD><TD CLASS="menuitem menucellright'+aLvl+' menuitemlvl'+aLvl+'">&nbsp;</TD></TR></TABLE></TD>');
			else if ( menuItemHighlighted )
			{
				if ( this.items[i].url.toLowerCase().indexOf("javascript:") == 0 )
					document.write('<TD CLASS="menuitemhigh menuitemlvl'+aLvl+' menuitem'+mainLvlCnt+'" onclick="WriteCookie(\''+this.items[i].id+'\');if (!script_executed)'+this.items[i].url.substr(11)+';script_executed=false;return false;"><ILAYER WIDTH="100%"><LAYER WIDTH="100%" CLASS="menuitemhigh"><TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="100%" BORDER="0"><TR><TD CLASS="menuitemhigh menucellleft'+aLvl+' menuitemlvl'+aLvl+'">'+indentationStr+'</TD><TD CLASS="menuitemhigh menucellmiddle'+aLvl+'"><A TARGET="'+this.items[i].target+'" HREF="#" onclick="WriteCookie(\''+this.items[i].id+'\');script_executed=true;'+this.items[i].url.substr(11)+';return false;" CLASS="menuitemhigh menuitemlvl'+aLvl+'">'+this.items[i].text+'</A></TD><TD CLASS="menuitem menucellright'+aLvl+' menuitemlvl'+aLvl+'">&nbsp;</TD></TR></TABLE></LAYER></ILAYER></TD>');
				else
					document.write('<TD CLASS="menuitemhigh menuitemlvl'+aLvl+'high menuitem'+mainLvlCnt+'high" onclick="WriteCookie(\''+this.items[i].id+'\');location.href=\''+this.items[i].url+'\';"><ILAYER WIDTH="100%"><LAYER WIDTH="100%" CLASS="menuitemhigh"><TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="100%" BORDER="0"><TR><TD CLASS="menuitemhigh menucellleft'+aLvl+'high menuitemlvl'+aLvl+'high">'+indentationStr+'</TD><TD CLASS="menuitemhigh menucellmiddle'+aLvl+'high"><A TARGET="'+this.items[i].target+'" HREF="'+this.items[i].url+'" onclick="WriteCookie(\''+this.items[i].id+'\')" CLASS="menuitemhigh menuitemlvl'+aLvl+'high">'+this.items[i].text+'</A></TD><TD CLASS="menuitemhigh menucellright'+aLvl+'high menuitemlvl'+aLvl+'high">&nbsp;</TD></TR></TABLE></LAYER></ILAYER></TD>');
			}
			else
			{
				if ( this.items[i].url.toLowerCase().indexOf("javascript:") == 0 )
					document.write('<TD CLASS="menuitem menuitemlvl'+aLvl+' menuitem'+mainLvlCnt+'" onclick="WriteCookie(\''+this.items[i].id+'\');if (!script_executed)'+this.items[i].url.substr(11)+';script_executed=false;return false;" onmouseover="this.className=this.className+\'high\';" onmouseout="this.className=this.className.substr(0,this.className.length-4);"><ILAYER WIDTH="100%"><LAYER WIDTH="100%" onmouseover="this.className=this.className+\'high\';" onmouseout="this.className=this.className.substr(0,this.className.length-4);" CLASS="menuitem"><TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="100%" BORDER="0"><TR> <TD CLASS="menuitem"><TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="100%" BORDER="0"><TR> <TD CLASS="menuitem menucellleft'+aLvl+' menuitemlvl'+aLvl+'">'+indentationStr+'</TD><TD CLASS="menuitem menucellmiddle'+aLvl+'"><A TARGET="'+this.items[i].target+'" HREF="#" onclick="WriteCookie(\''+this.items[i].id+'\');script_executed=true;'+this.items[i].url.substr(11)+';return false;" CLASS="menuitem menuitemlvl'+aLvl+'">'+this.items[i].text+'</A></TD><TD CLASS="menuitem menucellright'+aLvl+' menuitemlvl'+aLvl+'">&nbsp;</TD> </TR></TABLE></TD> </TR></TABLE></LAYER></ILAYER></TD>');
				else
					document.write('<TD CLASS="menuitem menuitemlvl'+aLvl+' menuitem'+mainLvlCnt+'" onmouseover="SetClassNames(this.parentNode.firstChild,'+aLvl+','+mainLvlCnt+');" onmouseout="UnSetClassNames(this.parentNode.firstChild,'+aLvl+','+mainLvlCnt+');" onclick="WriteCookie(\''+this.items[i].id+'\');location.href=\''+this.items[i].url+'\';"><ILAYER WIDTH="100%"><LAYER WIDTH="100%" onmouseover="SetClassNames(this.parentNode.firstChild,'+aLvl+','+mainLvlCnt+');" onmouseout="UnSetClassNames(this.parentNode.firstChild,'+aLvl+','+mainLvlCnt+');" CLASS="menuitem menuitemlvl'+aLvl+' menuitem'+mainLvlCnt+'"><TABLE CELLPADDING="0" CELLSPACING="0" WIDTH="100%" BORDER="0"><TR><TD CLASS="menuitem menucellleft'+aLvl+' menuitemlvl'+aLvl+'">'+indentationStr+'</TD><TD CLASS="menuitem menucellmiddle'+aLvl+'"><A TARGET="'+this.items[i].target+'" HREF="'+this.items[i].url+'" onclick="WriteCookie(\''+this.items[i].id+'\')" CLASS="menuitem menuitemlvl'+aLvl+'">'+this.items[i].text+'</A></TD><TD CLASS="menuitem menucellright'+aLvl+' menuitemlvl'+aLvl+'">&nbsp;</TD></TR></TABLE></LAYER></ILAYER></TD>');
			}
			document.write('</TR>');
		}

		if ( this.items[i].submenu != null)
		{
			this.items[i].submenu.TeamWriter_GenerateMenu( aLvl, aIdList );
		}
	}
}
function SetClassNames(aObj,aLvl,mainLvlCnt)
{
	// Modify element's childrens' class
	for (var i=0;i<aObj.childNodes.length;i++)
	{
		// Modify element's class
//		if (aObj.childNodes[i].tagName != "A")
//			if (aObj.childNodes[i].className) aObj.childNodes[i].className='menuitemhigh menuitemlvl'+aLvl+'high menuitem'+mainLvlCnt+'high';
//		else
			if (aObj.childNodes[i].className)
			{
				extraStyle = "";
				if (aObj.childNodes[i].className.indexOf("menucellleft") >= 0)
					extraStyle = aObj.childNodes[i].className.substr( aObj.childNodes[i].className.indexOf("menucellleft"), 13);
				else if (aObj.childNodes[i].className.indexOf("menucellmiddle") >= 0)
					extraStyle = aObj.childNodes[i].className.substr( aObj.childNodes[i].className.indexOf("menucellmiddle"), 15);
				else if (aObj.childNodes[i].className.indexOf("menucellright") >= 0)
					extraStyle = aObj.childNodes[i].className.substr( aObj.childNodes[i].className.indexOf("menucellright"), 14);
				aObj.childNodes[i].className='menuitemlvl'+aLvl+'hover menuitem'+mainLvlCnt+'hover'+' '+extraStyle;
			}
		SetClassNames(aObj.childNodes[i],aLvl,mainLvlCnt)
	}
	// Modify element's siblings' class
	while (aObj.nextSibling != null)
	{
		aObj = aObj.nextSibling
		SetClassNames(aObj,aLvl,mainLvlCnt)
	}
}
function UnSetClassNames(aObj,aLvl,mainLvlCnt)
{
	// Modify element's childrens' class
	for (var i=0;i<aObj.childNodes.length;i++)
	{
		// Modify element's class
//		if (aObj.childNodes[i].className) aObj.childNodes[i].className='menuitem menuitemlvl'+aLvl+' menuitem'+mainLvlCnt;
			if (aObj.childNodes[i].className)
			{
				extraStyle = "";
				if (aObj.childNodes[i].className.indexOf("menucellleft") >= 0)
					extraStyle = aObj.childNodes[i].className.substr( aObj.childNodes[i].className.indexOf("menucellleft"), 13);
				else if (aObj.childNodes[i].className.indexOf("menucellmiddle") >= 0)
					extraStyle = aObj.childNodes[i].className.substr( aObj.childNodes[i].className.indexOf("menucellmiddle"), 15);
				else if (aObj.childNodes[i].className.indexOf("menucellright") >= 0)
					extraStyle = aObj.childNodes[i].className.substr( aObj.childNodes[i].className.indexOf("menucellright"), 14);
				aObj.childNodes[i].className='menuitem menuitemlvl'+aLvl+' menuitem'+mainLvlCnt+' '+extraStyle;
			}
		UnSetClassNames(aObj.childNodes[i],aLvl,mainLvlCnt)
	}
	// Modify element's siblings' class
	while (aObj.nextSibling != null)
	{
		aObj = aObj.nextSibling
		UnSetClassNames(aObj,aLvl,mainLvlCnt)
	}
}
function GenerateBreadCrumbs( aIdList )
{
	// Generate breadcrumbs from a list of breadcrumb IDs
	for (var i = aIdList.length-1; i>=0 ; i--)
	{
		if ( (TeamWriter_MenuItemById[aIdList[i]] != null) && (TeamWriter_MenuItemById[aIdList[i]].url!="") && (TeamWriter_MenuItemById[aIdList[i]].url.indexOf("#")!=0) )
		{
			if (i != aIdList.length-1) document.write( "<SPAN CLASS='hiddenbreadcrumb'> > </SPAN>");
			document.write( "<A HREF='" + TeamWriter_MenuItemById[aIdList[i]].url + "' onclick='WriteCookie(" + TeamWriter_MenuItemById[aIdList[i]].id + ");' CLASS='hiddenbreadcrumb'>" + TeamWriter_MenuItemById[aIdList[i]].text + "</A>")
		}
	}
}
var idCount = 0;
function TeamWriter_FindParents(aIdList, aId)
{
	// Find the parents of aId and put them in aIdList
	if (aId>=0)
	{
		aIdList[idCount++] = aId;
		if (TeamWriter_MenuItemById[aId] != null)
			if (TeamWriter_MenuItemById[aId].parent != null)
				if (TeamWriter_MenuItemById[aId].parent.parentMenuItem != null)
					if (TeamWriter_MenuItemById[aId].parent.parentMenuItem.id != null)
						TeamWriter_FindParents(aIdList, TeamWriter_MenuItemById[aId].parent.parentMenuItem.id);
	}

	// Reset counter
	idCount=0;
}
