
function ckDropmenu(uniqueID) {

	this.type = "ckDropmenu";
	this.version = "03.30.2009 [ckDropmenu; ckDropmenu.js]";
	this.uniqueID = uniqueID;
	
	this.label = (arguments.length >= 2 ? arguments[1] : "root"); //label only needed for child menus
	this.setWidth(arguments.length >= 3 ? arguments[2] : 0); //default to auto-width
	this.menuItemHeight = (arguments.length >= 4 ? arguments[3] : 0); //default height of 19px
	
	this.fontSize = 12; //not used to set visible font size, but used in calculations for vertical alignment and menu width
	this.menuBorder = 1;
	this.menuBgOpaque=true; 
	this.menuItemBorder = 1; //width of the border
	this.menuItemIndent = 0; 

	this.menuItemVAlign = "middle"; //middle/top/bottom
	this.menuItemHAlign = "left"; 
	this.menuItemPadding = 5; 
	this.menuItemSpacing = 0; 

	this.childMenuIcon = "arrows.gif";
	this.submenuXOffset = 0; 
	this.submenuYOffset = 0; 
	this.submenuRelativeToItem = true; 
	this.vertical = true; 
	
	this.oActivatorEl = null;
	this.bActivatorSelected = false; 
	this.selectedItemIndex = -1;
	
	this.aItems = new Array();
	this.aActions = new Array();
	this.childMenus = new Array();
	
	this.bHideOnMouseOut = true;
	this.hideTimeout = 1000; //how long to keep menu on screen after mouseout;

	this.className = ""; //outer border
	this.classNameBackground = ""; //foreground...interior
	this.classNameHighlight = ""; //left and top border
	
	this.classNameItemDefault = "";
	this.classNameItemOver = "";
	this.classNameItemSelected = "";
	
	this.bHideOnActivatorClick= false; //hide the menu when activator clicked?
	this.setActivator("dropmenuHit." + this.uniqueID);//by default. Can always be over-ridden. The activator element is the HTML control that causes the dropdown menu to appear
	
	this.classNameActivator = "";
	this.classNameActivatorOver = "";
	this.classNameActivatorSelected = "";
}

ckDropmenu.prototype.setWidth = function(width) {
	this.menuWidth = width;
	this.bAutoWidth = (this.menuWidth==0);	
}
ckDropmenu.prototype.activatorClickSelectFirstItem = function(bClick) {
	//does clicking on the activator do nothing, or activate the first item in the menu
	this.activatorClickSelectFirstItem = bClick;
}

ckDropmenu.prototype.setItemHAlign = function(halign) {
	this.menuItemHAlign = halign; 
}

ckDropmenu.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]);
	}
}

ckDropmenu.prototype.addMenuItem = function(label) { //optional params: action,bSelected,uniqueID (in case smallTalkVar impractical)
	var itemIndex = this.aItems.length;
	if (this.bAutoWidth) {
		if (typeof(label)=="string") label = label.replace(/ /g,"&nbsp;");
		else if (label.label) label.label = label.label.replace(/ /g,"&nbsp;");
	}

	var action = ((arguments.length >= 2) && (arguments[1]) ? arguments[1] : "");
	var bSelected = ((arguments.length >=3) & (arguments[2])); 
	var uniqueID = (arguments.length >=4 ? arguments[3] : "");
	
	if (typeof(label)=="string") this.aItems[itemIndex] = new ckDropmenuItem(label,action,bSelected,uniqueID);
	else this.aItems[itemIndex] = label; //childMenu

	if (bSelected) {
		this.selectedItemIndex = itemIndex;
		this.bActivatorSelected = true;
	}
	
}

ckDropmenu.prototype.setClassNames = function(cn,cnBack,cnHigh,itemDefault,itemOver,itemSelected) {
	this.className = cn;
	this.classNameBackground = cnBack;
	this.classNameHighlight = cnHigh;
	this.classNameItemDefault = itemDefault;
	this.classNameItemOver = itemOver;
	this.classNameItemSelected = itemSelected;
	this.classNameActivator = (arguments.length >=7 ? arguments[6] : "");
	this.classNameActivatorOver = (arguments.length >=8 ? arguments[7] : "");
	this.classNameActivatorSelected = (arguments.length >=9 ? arguments[8] : "");
}
ckDropmenu.prototype.setActivator = function(elementID) {
	this.oActivatorEl = document.getElementById(elementID);
	if (arguments.length >= 2) this.classNameActivator = arguments[1];
	if (arguments.length >= 3) this.classNameActivatorOver = arguments[2];
	
}
ckDropmenu.prototype.getActivatorID = function() {
	return (this.oActivatorEl ? this.oActivatorEl.id : "");
}

ckDropmenu.prototype.setSelected = function(uniqueID) {
	if (this.uniqueID==uniqueID) this.bActivatorSelected = true; //catches menus with no children
	
	for (var index=0; index < this.aItems.length; index++) {
		if (this.aItems[index].uniqueID==uniqueID) {
			this.selectedItemIndex = index;
			this.aItems[index].bSelected = true;
			this.bActivatorSelected = true;
		}
	}
}


function ckDropmenuItem(label) { //optional params: action,bSelected
	this.type = "ckDropmenuItem";	
	this.label = label;
	this.action = (arguments.length >= 2 ? arguments[1] : "");
	this.bSelected = (arguments.length >=3 ? arguments[2] : false);
	this.uniqueID = ( ((arguments.length >=4) && (arguments[3] != "")) ? arguments[3] : this.smallTalkVar(this.label)); //automatically create the uniqueID as the smallTalkVar format of the label
	

	//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;
}

ckDropmenuItem.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("");

}
