// ********** 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;

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(aLevel) {
//Store tree to a String with aLevel as the requested rootlevel
//If aLevel is not specified a rootlevel of 0 is default.

  var i, resultStr = "";
  if (aLevel==null) {aLevel=0;}
  aLevel++;
  for (i = 0; i < this.items.length; i++) {
    resultStr += aLevel + 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(aLevel);
    }
  }
  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.toLowerCase() == aUrl.toLowerCase()) {
        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( aLevel, aIdList )
{
	var i,j;
	if (aLevel==null)
		aLevel=0;
	aLevel++;

	for (i = 0; i < this.items.length; i++)
	{
		if (this.items[i].url == "")
			this.items[i].url = CurrentFilename();

		menuItemActive = (aIdList[0]==this.items[i].id);
		menuItemHigh = false;

		aIdFoundInList = false;
		for (j = 0; j < aIdList.length ; j++)
		{
			if ((aIdList[j]==this.items[i].id) && (!menuItemActive)) {
				menuItemHigh = true;
			}
			
			if (this.items[i].parent.parentMenuItem!=null)
				if (aIdList[j] == this.items[i].parent.parentMenuItem.id)
				{
					aIdFoundInList = true;
					break;
				}
		  }

    if ((this.items[i].submenu != null) && (aLevel == 1)) {
    	documentStyle = "menufolder";
    }
    else {
    	documentStyle = "menudocument";
    }	
    
    if (menuItemActive || menuItemHigh) {documentStyle += "open"}
    
      
		if (aLevel == 1)
			indentationStr = "<div class='menuindent1 " + documentStyle + "' style='margin-right:10px;height:24px;width:30px;'></div>";
		else if (aLevel == 2)
			indentationStr = "<div class='menuindent2 " + documentStyle + "' style='margin-right:10px;height:24px;width:40px;'></div>";
		else if (aLevel == 3)
			indentationStr = "<div class='menuindent3 " + documentStyle + "' style='margin-right:10px;height:24px;width:50px;'></div>";

		if ( (aLevel == 1) || aIdFoundInList )
		{
			document.write('<tr>');
			if ( this.items[i].text == "")
				document.write('<td class="menuitem'+ ' menulevel' + aLevel +'" height="24"></td>');
			else if ( menuItemActive )
			{
				if ( this.items[i].url.toLowerCase().indexOf("javascript:") == 0 )
					document.write('<td class="menuitemactive'+ ' menulevel' + aLevel +'" height="24" onclick="WriteCookie(\''+this.items[i].id+'\');if (!script_executed)'+this.items[i].url.substr(11)+';script_executed=false;return false;" onmouseover="style.cursor=\'pointer\';"><ilayer width="100%"><layer width=100% height="24" class="menuitemactive"><TABLE CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="top" class="menuitem">'+indentationStr+'</TD><TD class="menuitem"><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="menuitemactive">'+this.items[i].text+'</a></TD></TR></TABLE></layer></ilayer></td>');
				else
					document.write('<td class="menuitemactive'+ ' menulevel' + aLevel +'" height="24" onclick="WriteCookie(\''+this.items[i].id+'\');location.href=\''+this.items[i].url+'\';" onmouseover="style.cursor=\'pointer\';"><ilayer width="100%"><layer width=100% height="24" class="menuitemactive"><TABLE CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="top" class="menuitem">'+indentationStr+'</TD><TD class="menuitem"><a target="'+this.items[i].target+'" href="'+this.items[i].url+'" onclick="WriteCookie(\''+this.items[i].id+'\')" class="menuitemactive">'+this.items[i].text+'</a></TD></TR></TABLE></layer></ilayer></td>');
			}
			else if ( menuItemHigh )
			{
				if ( this.items[i].url.toLowerCase().indexOf("javascript:") == 0 )
					document.write('<td class="menuitemhigh'+ ' menulevel' + aLevel +'" height="24" onclick="WriteCookie(\''+this.items[i].id+'\');if (!script_executed)'+this.items[i].url.substr(11)+';script_executed=false;return false;" onmouseover="style.cursor=\'pointer\';this.className=\'menuitemactive menulevel' + aLevel + '\';" onmouseout="this.className=\'menuitemhigh menulevel' + aLevel + '\';"><ilayer width="100%"><layer width=100% height="24" onMouseover="this.className=\'menuitemactive\';" onMouseout="this.className=\'menuitem\';" class="menuitem"><TABLE CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="top" class="menuitem">'+indentationStr+'</TD><TD class="menuitem"><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">'+this.items[i].text+'</a></TD></TR></TABLE></layer></ilayer></td>');
				else
					document.write('<td class="menuitemhigh'+ ' menulevel' + aLevel +'" height="24" onclick="WriteCookie(\''+this.items[i].id+'\');location.href=\''+this.items[i].url+'\';" onmouseover="style.cursor=\'pointer\';this.className=\'menuitemactive menulevel' + aLevel + '\';" onmouseout="this.className=\'menuitemhigh menulevel' + aLevel + '\';"><ilayer width="100%"><layer width=100% height="24" onMouseover="this.className=\'menuitemactive\';" onMouseout="this.className=\'menuitem\';" class="menuitem"><TABLE CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="top" class="menuitem">'+indentationStr+'</TD><TD class="menuitem"><a target="'+this.items[i].target+'" href="'+this.items[i].url+'" onclick="WriteCookie(\''+this.items[i].id+'\')" class="menuitem">'+this.items[i].text+'</a></TD></TR></TABLE></layer></ilayer></td>');
			}			
			else
			{
				if ( this.items[i].url.toLowerCase().indexOf("javascript:") == 0 )
					document.write('<td class="menuitem'+ ' menulevel' + aLevel +'" height="24" onclick="WriteCookie(\''+this.items[i].id+'\');if (!script_executed)'+this.items[i].url.substr(11)+';script_executed=false;return false;" onmouseover="style.cursor=\'pointer\';this.className=\'menuitemactive menulevel' + aLevel + '\';" onmouseout="this.className=\'menuitem menulevel' + aLevel + '\';"><ilayer width="100%"><layer width=100% height="24" onMouseover="this.className=\'menuitemactive\';" onMouseout="this.className=\'menuitem\';" class="menuitem"><TABLE CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="top" class="menuitem">'+indentationStr+'</TD><TD class="menuitem"><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">'+this.items[i].text+'</a></TD></TR></TABLE></layer></ilayer></td>');
				else
					document.write('<td class="menuitem'+ ' menulevel' + aLevel +'" height="24" onclick="WriteCookie(\''+this.items[i].id+'\');location.href=\''+this.items[i].url+'\';" onmouseover="style.cursor=\'pointer\';this.className=\'menuitemactive menulevel' + aLevel + '\';" onmouseout="this.className=\'menuitem menulevel' + aLevel + '\';"><ilayer width="100%"><layer width=100% height="24" onMouseover="this.className=\'menuitemactive\';" onMouseout="this.className=\'menuitem\';" class="menuitem"><TABLE CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="top" class="menuitem">'+indentationStr+'</TD><TD class="menuitem"><a target="'+this.items[i].target+'" href="'+this.items[i].url+'" onclick="WriteCookie(\''+this.items[i].id+'\')" class="menuitem">'+this.items[i].text+'</a></TD></TR></TABLE></layer></ilayer></td>');
			}
			document.write('</tr><tr><td class="menuline"><img src="media/huisstijl/spacer.gif" width="170" height="1" alt=""></td></tr>');
		}

		if ( this.items[i].submenu != null)
		{
			this.items[i].submenu.TeamWriter_GenerateMenu( aLevel, aIdList );
		}
	}
}
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) )
		{
			document.write( " <span class='breadcrumbs'>&gt;</span> <A HREF='" + TeamWriter_MenuItemById[aIdList[i]].url + "' onclick='WriteCookie(" + TeamWriter_MenuItemById[aIdList[i]].id + ");' class='breadcrumbs'>" + 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;
}
