
function ckVertmenu(sPageID,label) { //opt: action,altLabel
	this.type = "ckVertmenu";
	this.version = "03.30.2009 [ckVertmenu; ckVertmenu.js]";
	
	this.pageID = sPageID;
	this.label = label.replace(/ /g,"&nbsp;");
	this.action = (arguments.length >= 3 ? arguments[2] : "#");
	this.altLabel = (arguments.length >=4 ? arguments[3] : "");
	this.uniqueID = smallTalkVar(this.pageID)
	
	this.selectedItemIndex = -1
	this.bSelected=false;
	
	this.aItems = new Array();
	this.aActions = new Array();
	
	this.bHideOnMouseOut = true;
	this.bExpanded = true; 
	
	this.subItemsName =  "ckVertmenu." + this.uniqueID + ".subItem"
	this.menuItemID = "ckVertmenu." + this.uniqueID + ".menuItem"
	this.ctrlImgID = "ckVertmenu." + this.uniqueID + ".ctrlImg";

}

ckVertmenu.prototype.hasItems = function() {
	return (this.aItems.length > 0);
}

ckVertmenu.prototype.addMenuItems = function(aMenuItems) {
	//incoming parameter is an array of arrays.
	var aItem;
	for (var index=0; index < aMenuItems.length; index++) {
		aItem = aMenuItems[index];
		this.addMenuItem(aItem[0],aItem[1],aItem[2]);
	}
}

ckVertmenu.prototype.addMenuItem = function(vItem) {
	var args = ckVertmenu.prototype.addMenuItem.arguments;

	var oItem;
	if (typeof(vItem)=="string") {
		var sPageID = vItem;
		label = args[1];
		var action = ((args.length >= 3) ? args[2] : "#");
		oItem = new ckVertmenuItem(sPageID,label,action);
	} else if (uniqueID.type=="ckVertmenuItem") {
		oItem = vItem;
	}
	
	this.aItems[this.aItems.length] = oItem;
}

ckVertmenu.prototype.setSelected = function(sPageID) {
	var oEl;
	
	//if menus are already written and we're doing this dynamically, need to deslect all then select as necessary, otherwise just
	// setting the bSelected property will handle everything when menu is written
	this.bSelected = (this.pageID==sPageID); 
	if(oEl = document.getElementById(this.menuItemID)) oEl.className = "menuItem" + (this.bSelected ? " selected" : "");
	if(oEl = document.getElementById(this.menuItemID + ".altText")) oEl.className = "menuItemAltText" + (this.bSelected ? " selected" : "");
	
	for (var index=0; index < this.aItems.length; index++) {
		if (this.aItems[index].setSelected(sPageID)) {
			this.selectedItemIndex = index;
			if (!this.bExpanded) this.toggleExpand();  //make sure this item is expanded if one of it's children are selected
		}
	}
}

ckVertmenu.prototype.getItemHTML = function(oMenu) {
	var sHTML = "";

	if (this.hasItems()) {
		var sTemp = "Click to " + (this.bExpanded ? "collapse" : "expand");
		sHTML = '<a id="' + this.menuItemID + '" class="menuItem' + (this.bSelected ? " selected":"") + '" href="#" onclick="oVertmenuManager.toggleExpand(\'' + this.uniqueID + '\');return false">';
		sHTML += '<img id="' + this.ctrlImgID + '" src="/_ckcommon/images/icons/' + (this.bExpanded ? "treeCollapse.gif" : "treeExpand.gif") + '" border="0" alt="' + sTemp + '" title="' + sTemp + '"/>';
		sHTML += this.label + '</a>';
	} else {
		if (this.altLabel != "") sHTML += '<span id="' + this.menuItemID + '.altText" class="menuItemAltText' + (this.bSelected ? " selected":"") + '">' + this.altLabel + '</span>';
		sHTML += '<a id="' + this.menuItemID + '" class="menuItem' + (this.bSelected ? " selected":"") + '" href="' + this.action + '">' + this.label;
		
		sHTML += '</a>';
	}
	sHTML += this.getItemsHTML();
	
	return sHTML;
}
ckVertmenu.prototype.getItemsHTML = function() {
	var oMenuItem;
	var sHTML = "";
	if (this.hasItems()) {
		for (var index=0; index < this.aItems.length; index++) {
			oMenuItem = this.aItems[index];
			sHTML += oMenuItem.getHTML(this);
			
		}
	}
	return sHTML;
}

ckVertmenu.prototype.toggleExpand = function() {
	var oEl;
	var aEl = document.getElementsByName(this.subItemsName);
	for (var index=0; index < aEl.length; index++) {
		if (oEl = aEl[index]) {
			oEl.style.display=(this.bExpanded ? "none" : "block");
			if (oImg = document.getElementById(this.ctrlImgID)) {
				oImg.src="/_ckcommon/images/icons/" + (this.bExpanded ? "treeExpand.gif" : "treeCollapse.gif");
				oImg.alt = "Click to " + (this.bExpanded ? "expand" : "collapse");
				oImg.title = oImg.alt;
			}
		}
	}
	this.bExpanded = !this.bExpanded;
}

ckVertmenu.prototype.smallTalkVar = function(str) {
	str = str.replace(/^ */,"").replace(/ *$/,"");  //trim
	str = unescape(str); //unescape special characters so they can be stripped
	str = str.replace(/[\.\,\/\\\-\'\&]/g,""); //remove special characters
	
	var word;
	var aWords = str.split(" ");
	for (var index=0; index < aWords.length; index++) {
		word = aWords[index];
		aWords[index] = (index==0 ? word.slice(0,1).toLowerCase() : word.slice(0,1).toUpperCase()) + word.slice(1,word.length);
	}
	return aWords.join("");

}















function ckVertmenuItem(sPageID,label) {
	var args = ckVertmenuItem.arguments;

	this.type = "ckVertmenuItem";	
	this.pageID = sPageID
	this.label = label;
	this.uniqueID = smallTalkVar(this.pageID)
	this.action = (args.length >= 3 ? args[2] : "#");
	this.bSelected = (args.length >=4 ? args[3] : false);

	this.menuItemID = "ckVertmenu." + this.uniqueID + ".menuItem"
	
	//action is "eval"ed so prefix urls as necessary
	if (this.action != "")  {
		//if (this.action.indexOf("javascript") !=0) this.action = "document.location.href='" + this.action + "'";
	}
	
	return this;
}
ckVertmenuItem.prototype.setSelected = function(sPageID) {
	this.bSelected = (this.pageID==sPageID);
	if(oEl = document.getElementById(this.menuItemID)) oEl.className = "menuSubItem" + (this.bSelected ? " selected" : "");
	return this.bSelected;
}
ckVertmenuItem.prototype.getHTML = function(oParent) {
	var sHTML = '<a id="' + this.menuItemID + '" name="' + oParent.subItemsName +'" class="menuSubItem' + (this.bSelected ? ' selected' : '') + '" href="' + this.action + '" style="display:' + (oParent.bExpanded ? "block" : "none") + '">';
	sHTML += '<div class="menuSubItem-icon"></div>';
	sHTML += this.label + '</a>';
	
	return sHTML;
}
