   /*
	* Copyright (C) 2005, Workflow Studios
	* 
	*  This library is free software; you can redistribute it and/or
 	*  modify it under the terms of the GNU Lesser General Public
 	*  License as published by the Free Software Foundation; either
 	*  version 2 of the License, or (at your option) any later version.
 	*
 	*  This library is distributed in the hope that it will be useful,
 	*  but WITHOUT ANY WARRANTY; without even the implied warranty of
 	*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 	*  Lesser General Public License for more details.
 	*/

	// functions for MenuCreator
	
	// adds a menuitem to the menu object
	function addMenuItem(x) {
		this.items[this.items.length] = x;
	}
	
	// returns the current number of menuitems in the menu object
	function getNumberOfMenuItems() {
		return this.items.length;
	}
	
	// constructor and prototypes for MenuCreator
	function MenuCreator() {
		this.items = new Array();
	}
	MenuCreator.prototype.add=addMenuItem;
	MenuCreator.prototype.getItemLength=getNumberOfMenuItems;
	
	
	
	// functions for ItemCreator
	
	// determines where in the menu object this item should be added
	function attachItem(topmenu) {
		levels = this.level.split('_');
		numLevels = levels.length;
		if (numLevels == 1) {		
			// we're at the root of the menu, so add here
			topmenu.add(this);
			return;
		}
		
		// this may seem a bit strange, but build a string from the end to the beginning
		// this is the last element 
		var menuString='.add(this)';
		
		// start at the lowestlevel and work your way up
		for (i=numLevels-1; i>0; i--) {
			currentLevel = levels[i-1];
			if (isNaN(currentLevel)) {
				return;
			}
			index = parseInt(currentLevel);
			currentString = '.items[' + index + ']';
			menuString = currentString + menuString;
		}
		// now put all the pieces of the string together and evaluate it to
		// add the item to the appropriate place in the menu object
		eval('topmenu' + menuString);
	}
	
	// constructor and prototypes for ItemCreator
	function ItemCreator() {
		this.items = new Array();
		this.label = '';
		this.url = '';
		this.level='';
		this.target='';
	}
	ItemCreator.prototype.attach=attachItem;
	ItemCreator.prototype.add=addMenuItem;
	
	
	function convertLevel(level) {
		// data comes in as 1 or 1.1 and needs to go out as 0 or 0_0
		// actually navigator data will always start with 1.1.  The first 2 digits need to be stripped
		level2 = level.substring(2, level.length);
		var levels = level2.split('.');
		var newLevels = new Array(levels.length);
		for (var i=0; i<levels.length; i++) {
			newLevels[i] = levels[i] -1;
		}
		
		return newLevels.join('_');
	}
	
	
	// after the menu object has been built, we need to parse it
	// and produce the output that was in the menusettings file
	// myArray 	is our custom data structure
	// backend	the menuing system object
	function processMenuTree(myArray,backend, offset) {
		var len=myArray.length;
		for (var i=0; i<len; i++) {
			var index = myArray[i].level;
			var cindex = convertIndex(index, offset);
			if (index.length>1) {
				// we have a submenu
				subIndex = cindex.substring(0,index.length-2);
				eval('backend.menu_xy' + subIndex + ' = "-100,20"');
				eval('backend.menu_width' + subIndex + ' =170');
			}	
			
			eval('backend.item' + cindex + '="' + myArray[i].label + '"');
			eval('backend.icon_rel' + cindex + '=0');
			setPreviousLevelIcon(cindex, backend);
			
			eval('backend.url' + cindex +'="' + myArray[i].url + '"');
			if (myArray[i].target != '') {
				eval('backend.url_target' + cindex +'="' + myArray[i].target + '"');
			}

						
			if (myArray[i].items.length > 0) {
				processMenuTree(myArray[i].items, backend, offset);
			}
		}
	}
	
	function convertURL(domain, url, target) {
		return (url.indexOf('http') > -1) ? url : 'http://' + domain + url;
	}
	
	/*
	* convertIndex
	* the level that the menu structure originally has is not correct
	* when the splice function is used to subdivide the menu
	* this function takes the current level and converts it to something
	* the backend can use
	*/
	function convertIndex(index, offset) {
		// get the first digit and substract the offset from it
		
		// var firstNumber = index.substring(0,1);
		var firstNumberArray = index.split('_');
		var fNum = parseInt(firstNumberArray[0]);
		var fNumLen = firstNumberArray[0].length;
		var convertedNumber = fNum - offset;
		
		/******************** need real function *************/
		var restOfNumber = index.substring(fNumLen, index.length);
		var finalnumber = convertedNumber + restOfNumber;
		return finalnumber;
	}	
	
	function setPreviousLevelIcon(cindex, backend) {
		var numLevels = cindex.split('_');
		if (numLevels.length > 2) {
			var prevLevel='';
			for (var i=0; i<numLevels.length; i++) {
				prevLevel += numLevels[i] + '_';
			}
			// remove last _ character
			var prevLevelIndex = prevLevel.substring(0,prevLevel.length-3);
			var temp = 'backend.icon_rel' + prevLevelIndex + '=1';
			eval(temp);
		}
	}