/* Version: CVS $Id: navutil.js,v 1.4 2008/01/21 13:26:47 perros Exp $ */

var CBE_LEVEL1_ID_PREFIX = 'l1.lbl.';
var CBE_SUBLEVEL_ID_PREFIX = 'sub.lbl.';

var CBE_LISTEN_TO_CLICK = true;
var CBE_LISTEN_TO_MOUSEMOVE = false;

var CLASS_L1_ACTIVE = 'navLevelOneOn';
var CLASS_L1_ACTIVE_PARENT = 'on';
var CLASS_L1_INACTIVE = 'navLevelOneOff';
var CLASS_L1_INACTIVE_PARENT = '';
var CLASS_SUBLEVEL_WITH_CHILD_ACTIVE = 'sublevelChild_on';
var CLASS_SUBLEVEL_WITH_CHILD_INACTIVE = 'sublevelChild_off';

var CLASS_SUBLEVEL_ACTIVE = 'sublevel_on';
var CLASS_SUBLEVEL_INACTIVE = 'sublevel_off';

var ZINDEX_ACTIVE = 2002;

var NAV_DIV_ID = 'navDiv';
var NAV_BGCOLOR = '#FFF';
var NAV_BOX_BGCOLOR = '#D9D9D9';
var NAV_TEXTCOLOR = '#000';
var NAV_BGCOLOR_HOVER = '#000';
var NAV_TEXTCOLOR_HOVER = '#000';
var NAV_BGCOLOR_SELECTED = '#D9D9D9';
var NAV_MENU_MIDDLE_XPOS = 400; // at what px a children-box should open to left or right of parent

function newCBEMenu() {
	
	// move children of level1 200px to the left unless we have 5 menus (VSA).
	var startLeftPos = 200; // this should be margin-left-1 of css-class ".externNav"
	if (cbeGetElementById('l1.lbl.5') != undefined) {
		startLeftPos = -1;
	}

    return new cbeDropdownMenu(
//    navDiv = cbeGetElementById('navDiv').cbe;
//    navDiv.pageX(), navDiv.pageY(), // coord of first label
      startLeftPos, 67,         // SUB-LEVEL start left, top
      -1, 20, 10,     // LEVEL-ONE label width, height, L-padding
      210, 2, 20, 0,  // SUB-LEVEL box width, item L/R fillpad, item height, sub-box L/R space
      NAV_BGCOLOR,
      NAV_BOX_BGCOLOR,
      NAV_TEXTCOLOR,
      NAV_BGCOLOR_HOVER,
      NAV_TEXTCOLOR_HOVER);
}

function cbeAfterOnload() {
  initTrackmark();
}

function highlightL1(e) {
  if (!e) return;
  e.setClass(CLASS_L1_ACTIVE);
  if (e.parentElement && _def(e.parentElement.className)) {
    e.parentElement.className = CLASS_L1_ACTIVE_PARENT;
  }
}

function lowlightL1(e) {
  if (!e) return;
  e.setClass(CLASS_L1_INACTIVE);
  if (e.parentElement && _def(e.parentElement.className)) {
    e.parentElement.className = CLASS_L1_INACTIVE_PARENT;
  }
}

function lowlight(e) {
  if (!e) return;
  updateClass(e, false);
}

function lowlightChildren(e) {
  if (!e) return;
  var itm = e.firstChild;
  while (itm) {
    lowlight(itm);
    itm = itm.nextSibling;
  }
}


// params: 1) object: cbeMenu, 2) object: L1 div
function cbeL1PaintHook(cbeMenu, div) {
  with (div) {
    color(cbeMenu.txtColor);
    //background(cbeMenu.bgColor);
    zIndex(ZINDEX_ACTIVE);
    if (cbeMenu.lblW != -1) {
      resizeTo(cbeMenu.lblW, cbeMenu.lblH);
    }
    moveTo(cbeMenu.mnuX, cbeMenu.mnuY);
    show();
  }
}

// params: 1) object: cbeMenu, 2) object: cbe box, 3) object: cbe parent, 4) int: x, 5) int: y, 6) int: #labels
function cbePaintBoxHook(cbeMenu, box, parent, x, y, lblCount) {
  var mx=0, my=0, itmCount=0;

  //box.background(cbeMenu.bgColor);
  box.width(cbeMenu.boxW);
  box.moveTo(x, y);
  box.zIndex(ZINDEX_ACTIVE);
  var itm = box.firstChild;
  while (itm) {
    if (itm.id.indexOf(CBE_SUBLEVEL_ID_PREFIX) == 0) {
      itm.color(cbeMenu.txtColor);
      //itm.background(cbeMenu.boxBgColor);
      itm.resizeTo(cbeMenu.boxW - 2*cbeMenu.itmLRpad, cbeMenu.itmH);
      //itm.moveTo(mx + cbeMenu.itmLRpad, my);
      itm.show();
      my += itm.height();
      ++itmCount;
    }
    else {
      itm.previousSibling.childBox = itm;
      itm.previousSibling.parentLabel = parent;

      var subX,subY;
      if (x < NAV_MENU_MIDDLE_XPOS) {
          subX = mx + itm.parentNode.width() - cbeMenu.itmLRpad + cbeMenu.subItmSpace; // "TURN RIGHT"
      } else {
          subX = mx - itm.parentNode.width() - cbeMenu.itmLRpad + cbeMenu.subItmSpace + 2; // "TURN LEFT"
      }
      subY = my - itm.previousSibling.height() - 1;
      cbeMenu.paintBox(itm, itm.previousSibling, subX, subY);
    }
    itm = itm.nextSibling;
  }
  box.height(itmCount * cbeMenu.itmH + 3);
}

// params: 1) object: cbeMenu, 2) object: cbe-event
function cbeMouseclickHook(cbeMenu, e) {
  if (cbeMenu.lblActive &&
      (e.cbeTarget != cbeMenu.lblActive.childBox &&
      e.cbeTarget != cbeMenu.lblActive &&
      e.cbeTarget.parentNode != cbeMenu.lblActive.childBox))
  {
    while (cbeMenu.lblActive.parentLabel != null) {
      var cBox = cbeMenu.lblActive.childBox;
      if (cBox) {
        lowlight(cbeMenu.lblActive);
        lowlightChildren(cBox);
        cBox.hide();
      }
      cbeMenu.lblActive = cbeMenu.lblActive.parentLabel;
    }
    if (cbeMenu.lblActive.childBox != e.cbeTarget.parentNode) {
      if (cbeMenu.lblActive.childBox) {
        cbeMenu.lblActive.childBox.hide();
        lowlightL1(cbeMenu.lblActive);
      }
    }
  }
}

// params: 1) object: cbeMenu, 2) object: cbe-event
function cbeMousemoveHook(cbeMenu, e) {
  if (
    cbeMenu.lblActive &&
    (e.cbeTarget != cbeMenu.lblActive.childBox &&
    e.cbeTarget != cbeMenu.lblActive &&
    e.cbeTarget.parentNode != cbeMenu.lblActive.childBox)
  ) {
    if (cbeMenu.lblActive.childBox) cbeMenu.lblActive.childBox.hide();
    cbeMenu.lblActive = cbeMenu.lblActive.parentLabel;
  }
  else if (e.cbeTarget.childBox || e.cbeTarget.id.indexOf(CBE_LEVEL1_ID_PREFIX)==0) {
    cbeMenu.lblActive = e.cbeTarget;
    if (cbeMenu.lblActive.childBox) cbeMenu.lblActive.childBox.show();
  }
}

// params: 1) object: cbeMenu, 2) object: event target
function cbeL1ClickHook(cbeMenu, eTarget) {
  if (cbeMenu.lblActive == eTarget) {
    if (cbeMenu.lblActive.childBox) {
      cbeMenu.lblActive.childBox.hide();
      lowlightL1(cbeMenu.lblActive);
      cbeMenu.lblActive = null;
    }
  } else {
    if (cbeMenu.lblActive) {
      if (cbeMenu.lblActive.childBox) {
        cbeMenu.lblActive.childBox.hide();
      }
      lowlightL1(cbeMenu.lblActive);
      cbeMenu.lblActive = null;
    }
    cbeMenu.lblActive = eTarget;
    if (cbeMenu.lblActive.childBox) {
      cbeMenu.lblActive.childBox.show();
      highlightL1(cbeMenu.lblActive);
    }
  }
}

// params: 1) object: cbeMenu, 2) object: event target
function cbeLSubClickHook(cbeMenu, eTarget) {
  if (cbeMenu.lblActive == eTarget) {
    // lowlight
	updateClass(eTarget, false);

    // inactivate + hide
    if (cbeMenu.lblActive.childBox) {
      cbeMenu.lblActive.childBox.hide();
      if (cbeMenu.lblActive.parentLabel) {
        cbeMenu.lblActive = cbeMenu.lblActive.parentLabel;
      } else {
        cbeMenu.lblActive = null;
      }
    }
  } else {
    // highlight
	updateClass(eTarget, true);

    // set active + show
    cbeMenu.lblActive = eTarget;
    if (cbeMenu.lblActive.childBox) {
      cbeMenu.lblActive.childBox.show();
      //cbeMenu.lblActive.background(NAV_BGCOLOR_SELECTED);
    }
  }
}


/*
 * Takes the current class of the element and 
 * if ends with "_off" and isActive=true, then make it end with "_on"
 * else if ends with "_on" and isActive=false, then make it end with "_off"
 * else nothing
 */
function updateClass(element, isActive) {
	var currClass = element.getClass();
	var newClass;
	
	var lastUnderscore = currClass.lastIndexOf("_");

	// This should not occur (if the classes are labeled *_on and *_off)
	if (lastUnderscore == -1) {
		return;
	}

	var baseClassName = currClass.substring(0, lastUnderscore);
	var currValue = currClass.substring(lastUnderscore);
	
	if (currValue == "_on" && !isActive) {
		element.setClass(baseClassName + "_off");
	} else if (currValue == "_off" && isActive) {
		element.setClass(baseClassName + "_on");
	}
}
