/* *******************************************************************
MenuItem object: Object representing a menu item with accessors for the main data 
                        (name, url, key, parent).
******************************************************************* */
function MenuItem(name, key, url, target, parent) {
	this.pName = name;
	this.pURL = url;
	this.pKey = key;
	this.pTarget = target;
	this.pParent = parent;
	this.pChildren = new Array();
	this.pLoopIndex = 0;
}
MenuItem.prototype.getName = function() {
	return this.pName;
}
MenuItem.prototype.getKey = function() {
	return this.pKey;
}
MenuItem.prototype.getURL = function() {
	return this.pURL;
}
MenuItem.prototype.getTarget = function() {
	return this.pTarget;
}
MenuItem.prototype.addChild = function(name, key, url, target) {
	var m = new MenuItem(name, key, url, target, this);
	this.pChildren[this.pChildren.length] = m;
	return m;
}
MenuItem.prototype.getParent = function() {
	return this.pParent;
}
MenuItem.prototype.getNextChild = function() {
	if (this.pChildren.length == 0) {
		return null;
	} else {
		if (this.pLoopIndex < this.pChildren.length) {
			return this.pChildren[this.pLoopIndex++];
		} else {
			return null;
		}
	}
}
MenuItem.prototype.getFirstChild = function() {
	if (this.pChildren.length == 0) {
		return null;
	} else {
		this.pLoopIndex = 0;
		return this.pChildren[this.pLoopIndex++];
	}
}
MenuItem.prototype.hasChildren = function() {
	return this.pChildren.length > 0;
}
/* **************************************************************
 * CurrentDocument object: Abstracts away dealing with the menu structure and 
 *                                    allows you to get the menu item title etc.
 * 
 *                                    The constructor takes a key that represents the current 
 *                                    document - this key can either be a document key used 
 *                                    to find the "real" menuitem key through lookup or used 
 *                                    directly as a pointer into the menuitem structure.
 *
 *                                    The key can come from the following places:
 *                                      - passed as an argument: the user is reading a document or is reading a view. If the user 
 *                                        is reading a document the menuitem key is found found by lookup in the 
 *                                        document-to-menuitem hashtable before the menuitem object 
 *                                        is extracted. If the user is reading a view the passed key is the menu item 
 *                                        key that can be used as a pointer into the menuitem structure.
 *                                      - found as a cookie: the user is reading a document after navigating a view - the key of  
 *                                        the menuitem to select is the value of the cookie.
 */
function CurrentDocument(key) {
	/* the key */
	this.pKey = null;
	/* used when backtracking through the menuitem hierarchy */
	this.pMenuStructurePointer = null;
	// if we are passed a key we use that one - otherwise we look at the cookie
	if (null != key && key != "") {
		this.pKey = key;
	} else {
		// look for a cookie
		var cookie_value = document.cookie;
		var start = cookie_value.indexOf("m0=");
		if (start > -1) {
			var stop = cookie_value.indexOf(";", start+3);
			if (stop > -1) {
				this.pKey = cookie_value.substring(start + 3, stop);
			} else {
				this.pKey = cookie_value.substring(start + 3);
			}
		}
	}
}
CurrentDocument.prototype.getKey = function() {
	return this.pKey;
}
CurrentDocument.prototype.getMenuItemTitle = function() {
	var m = this.findMenuItemForKey();
	if (null != m) {
		this.pMenuStructurePointer = m;
		return m.getName();
	} else {
		return "";
	}
}
CurrentDocument.prototype.backtrackHierarchy = function() {
	if (null == this.pMenuStructurePointer) {
		return null;
	} else {
		var parent = this.pMenuStructurePointer.getParent();
		var key = this.pMenuStructurePointer.getKey();
		this.pMenuStructurePointer = parent;
		return key;
	}
}
CurrentDocument.prototype.findMenuItemForKey = function() {
	var menuitem_key = document_translation.get(this.pKey);
	if (null == menuitem_key) {
		// no key returned - use the key directly
		menuitem_key = this.pKey;
	}
	
	for (var i=0; i<webcm_sitemap.length; i++) {
		var pmi = webcm_sitemap[i];
		if (pmi.getKey() == menuitem_key) {
			return pmi;
		} else {
			// loop children
			var child = pmi.getFirstChild();
			while (null != child) {
				var m = this.traverseChildren(child, menuitem_key);
				if (null != m) {
					return m;
				}
				child = pmi.getNextChild();
			}
		}
	}
}
CurrentDocument.prototype.traverseChildren = function(m, key) {
	if (m.getKey() == key) {
		return m;
	} else {
		var child = m.getFirstChild();
		while (null != child) {
			var o = this.traverseChildren(child, key);
			if (null != o) {
				return o;
			}
			child = m.getNextChild();
		}
	}
}
