function CommandMenuManager() {
  this.CommandMenuIds = Array();
  this.init();
  this.openMenuIdStem = false;
}

// Note the use of killEvent()  to prevent the onmousedowns and onclicks from propagating down to the row,  where they would normally open vintage popups, which is no good
CommandMenuManager.prototype.init = function() {
  if (document.onmousedown) {
    alert("careful with the onmousedown there- you might interfere with CommandMenuManager");
  }
  else {
    document.onmousedown = closeCommandMenus;
  }
  divCollection = document.getElementsByTagName("DIV");
  
  for (var i=0; i<divCollection.length; i++) {
    if (divCollection[i].className.indexOf("commandMenu")!=-1) {
			
      this.CommandMenuIds[this.CommandMenuIds.length] = divCollection[i].id;

      var idStem = divCollection[i].id.replace("Menu","");
			
      var triggerElement = document.getElementById(idStem + "Trigger");
		
      // attaching anonymous event handlers
      eval("triggerElement.onclick = function(mozEvent) { openCommandMenu('"+idStem+"'); goOpenState(this);cmm.killEvent(mozEvent)}");
			
      // if this trigger element is not the open menu.  (if it is, you want the image to stay 'open',not roll on and off.
			triggerElement.onmouseover= function(mozEvent) { if (this.parentNode.id.indexOf(cmm.openMenuIdStem)==-1) goOverState(this);cmm.killEvent(mozEvent)}

      // basically onmouseout will only change the image src only if this menu isnt open..
      triggerElement.onmouseout= function() {  if (this.parentNode.id.indexOf(cmm.openMenuIdStem)==-1) goClosedState(this);}
      
      // goes through each of the menuItem divs and attaches event handlers there.
      for (var j=0; j< divCollection[i].childNodes.length; j++) {
        var childDiv =  divCollection[i].childNodes[j]
        childDiv.onmouseover = function(mozEvent) {this.className = "over";cmm.killEvent(mozEvent)};
        childDiv.onmouseout  = function() {this.className = ""};
        childDiv.onmousedown = function(mozEvent) { cmm.killEvent(mozEvent)};
        eval("childDiv.onclick = function(mozEvent) {cmm.doAction(this);cmm.closeCommandMenus();cmm.killEvent(mozEvent)}");
      }
    }
  }
}
CommandMenuManager.prototype.killEvent = function(mozEvent) {
  if (document.all) window.event.cancelBubble=true;
  else mozEvent.stopPropagation();
}  
// just hides them all. Always. it's not as slow as you might think.
CommandMenuManager.prototype.closeCommandMenus = function() {
  for (var i=0; i<this.CommandMenuIds.length; i++) {
    document.getElementById(this.CommandMenuIds[i]).style.visibility = "hidden";
  }
  if (this.openMenuIdStem) {
    var triggerElement = document.getElementById(this.openMenuIdStem + "Trigger");
    //triggerElement.src = "/images/button-options.gif";
  }
	// js type-blindness allows us to just use false here, although this is normally a string.
	//this.openMenuIdStem = false;
}

CommandMenuManager.prototype.openCommandMenu = function(id) {
  var CommandMenu = document.getElementById(id + "Menu") ;  
  var triggerElement = document.getElementById(id + "Trigger");

	if (CommandMenu.offsetLeft <0 ){ 
		addClassName(CommandMenu,"openToTheRight");
	}
	else if ((CommandMenu.offsetLeft >180 ) && (CommandMenu.className.indexOf("openToTheRight")!=-1)) {
		stripClassName(CommandMenu,"openToTheRight");
	}
  CommandMenu.style.visibility = "visible";

  this.openMenuIdStem = id;
}

CommandMenuManager.prototype.doAction = function(commandItem) {
	var actionLink = commandItem.getAttribute("actionlink");
	var action = commandItem.getAttribute("action");
	var actionExecute = commandItem.getAttribute("actionexecute");
	if (actionLink) {
		window.parent.location = actionLink;
	}
	else if (actionExecute) {
		eval(actionExecute);
	}
	else if (action){

	
		var commandMenu = commandItem.parentNode;

		var requestPath = commandMenu.getAttribute("requestpath");	
		var contentBoxId = commandMenu.getAttribute("contentboxid");
		var putAfter = commandMenu.getAttribute("putafter");
		var column = commandMenu.getAttribute("column");
		if (action=="edit") {
			window.parent.location = "_editBox.php?requestPath="+requestPath+"&contentBoxId="+contentBoxId+"&putAfter="+putAfter+"&column="+column;
		}
		else if (action=="remove") {
			window.parent.location="_editPage.php?remove=1&requestPath="+requestPath+"&contentBoxId="+contentBoxId+"&column="+column;
		}
		else if (action=="addNew") {
			window.parent.location="_addBox.php?requestPath="+requestPath+"&putAfter="+putAfter+"&column="+column+"&createNew=1";
		}
		else if (action=="addExisting") {
			window.parent.location="_addBox.php?requestPath="+requestPath+"&putAfter="+putAfter+"&column="+column;
		}
	}
}


// globals, just to make sure cmm is defined. 
function openCommandMenu(id) {
  if (cmm) cmm.openCommandMenu(id);
	else alert("bigger problems. CommandMenuManager cmm is undefined");
}
function closeCommandMenus() {
  if (cmm) cmm.closeCommandMenus();
}

function goOpenState() {

}
function goClosedState() {

}
function goOverState() {

}
