// CBE Cascading Drop-down Menu
// copyright (c) 2002 Mike Foster
// get CBE at cross-browser.com
// CBE and cbeDropdownMenu are licensed under the LGPL

// v1.5 30Oct02 - bug fix for when label with no box is the last label
// v1.4 29Oct02 - added support for main labels with no boxes
// v1.3 01Oct02 - utilizes the update to CBE v4.15 which allows object methods to be used as event listeners
// v1.2 27Aug02 - now licensed under LGPL
// v1.1 20Aug02 - added optional parameters to the paint() method, for re-painting on win resize
// v1.0 17Aug02 - initial release

var
  cbeMenu,
  mnuMarker,
  downgrade = true,
  ua = navigator.userAgent.toLowerCase();    

if (navigator.userAgent.indexOf('Opera') > -1) {
	var op = true;
}

  
if (
  ua.indexOf('msie') != -1 && parseInt(navigator.appVersion) >= 4  // IE4 up
  || ua.indexOf('gecko') != -1                                     // Gecko
  || ua.indexOf('konqueror') != -1                                 // Konquerer
  || window.opera                                                  // Opera
) {  
  document.write("<link rel='stylesheet' type='text/css' href='/ptarl/styles/"+cssFile+"'>");
  document.write("<script type='text/javascript' src='/ptarl/files/cbe_core.js'></script>");
  document.write("<script type='text/javascript' src='/ptarl/files/cbe_event.js'></script>");
  downgrade = false;
}



function winResizeListener() {
  mnuMarker = cbeGetElementById('menuMarker').cbe;
  cbeMenu.paint(mnuMarker.pageX(), mnuMarker.pageY());
}

// begin class cbeDropdownMenu

function cbeDropdownMenu(mnuX, mnuY, lblW, lblH, boxW, itmH, itmPadL, itmPadR) {
  // Properties

  this.mnuX = mnuX;
  this.mnuY = mnuY;
  this.lblW = lblW;
  this.lblH = lblH;
  this.boxW = boxW;
  this.itmH = itmH;
  this.itmPadL = itmPadL;
  this.itmPadR = itmPadR;
  this.lblCount = 0;
  this.lblActive = null;
  
  this.imgY = 0;

  // Methods
	
  this.paint = function(mnuX, mnuY) { // this is the only public method
    if (arguments.length > 0) this.mnuX = mnuX;
    if (arguments.length > 1) this.mnuY = mnuY;
    var lbl = null; // of type Element            
    
    var box = null; // of type CBE
    var mX = this.mnuX;
	var mY = this.mnuY;	
	//Onderstaande lijn is blijkbaar overbodig geworden in de nieuwe Opera versies
	//if (op) mX -= 15;
	var i = 0;
    this.lblCount = 0;
    do {
      ++this.lblCount;
      lbl = cbeGetElementById('label' + this.lblCount)
      
      if (lbl) {
        with (lbl.cbe) {
          zIndex(2002);
          /*
          Subtract the sum of the right and left padding of the element 
          from the element width and make the result the new element width. 
          This way the height of the element gets changed accordingly.
          This is a fix because in some cases, due to the padding, the element
          text was divided over 2 lines while its height wasn't changed thus not 
          showing the last line of the element.
          */
          lbl.cbe.width(lbl.cbe.width() - (itmPadL + itmPadR));
          
          //De volgende check is in de nieuwe versies van Opera blijkbaar overbodig geworden
          //if (op) resizeTo(this.lblW, lbl.cbe.height()+6);		  
          //else 
          resizeTo(this.lblW, lbl.cbe.height());  
          moveTo(mX, mY);
          show();
        }        
        //if (lbl.cbe.nextSibling && lbl.cbe.nextSibling.id.indexOf('label')==-1) box = lbl.cbe.nextSibling;
        if (lbl.cbe.nextSibling && lbl.cbe.nextSibling.id.indexOf('b') == 0 && lbl.cbe.nextSibling.ele.className.indexOf('mBox') != -1) box = lbl.cbe.nextSibling;
        else box = null;
        
        lbl.cbe.childBox = box;
        lbl.cbe.parentLabel = null;
		
        if (box ){//& cbeGetParentElement(lbl.cbe.nextSibling).cbe.id == lbl.cbe.id) {        
        
			/*
			* positioning values below are offsets of the first submenu box only
			* the offset of the other boxes can be changed further down        
			*/
			
        	this.paintBox(box, lbl.cbe, mX + (menuOffset?menuOffset[0]:25), mY + (menuOffset?menuOffset[1]:lbl.cbe.height()));
        	with(lbl.cbe.childBox)
        	{
        		zIndex(2003);
        	}
        }
        
		i++;
        mY += lbl.cbe.height();
      }
    } while(lbl);
    --this.lblCount; 
    	this.imgY = mY;       
    	this.paintImage(this.imgY);
  }

  //afstand tss <div> met afbeelding en menu hier aanpassen
  //specifiek voor Haviland
  this.paintImage = function(mY){
    var img = null; // of type Element
  	img = cbeGetElementById('img');
    
    if (img) {
    	with (img.cbe){
    		moveTo(this.mnuX, mY + 20); 
    		width(200); //area as wide as the menu
    		zIndex(2002);   	
    		show();
    	}
    }
  }
	
  this.paintBox = function(box, parent, x, y) {
    var mx=0, my=4, itmCount=0, wdth=120;hght = 20;
    var itm = box.firstChild;
    if (op) {mx = -2;}
    box.width(this.boxW);
    while (itm) {
      if (itm.id.indexOf('i') != -1 && itm.width()>wdth) {
      	wdth = itm.width();	
      }
      itm = itm.nextSibling;
    }

    //width of element smaller than width of parent box
    if (wdth < this.boxW) box.width(wdth);   
    //width of element bigger than width of parent box 
    else {box.width(this.boxW); box.height()}
    
    box.moveTo(x, y);
    box.zIndex(2002);
    itm = box.firstChild;
    
    //Submenu-items in de submenu-box zetten
    while (itm) {
              
      if (itm.id.indexOf('i') != -1) {

        if (op) itm.resizeTo(box.width() + 3, itm.height()+1);
        else itm.resizeTo(box.width() - 4, itm.height());
        
        itm.moveTo(mx + this.itmPadL, my);
        itm.show();
        my += itm.height();
        ++itmCount;
      }
      else {
        itm.previousSibling.childBox = itm;
        itm.previousSibling.parentLabel = parent;
        
        /*
        * positioning values below are offsets of all the submenu boxes except the first one
		* the offset of the first box can be changed further up 
		*/

        this.paintBox(itm, itm.previousSibling, mx + (menuOffset?menuOffset[2]:25), my + (menuOffset?menuOffset[3]:0));
      }
      
      itm = itm.nextSibling;      
    }
    box.height(my + 4);
  }

  this.mousemoveListener = function(e) {        
     
    if (this.lblActive &&
      (e.cbeTarget != this.lblActive.childBox &&
      e.cbeTarget != this.lblActive &&
      e.cbeTarget.parentNode != this.lblActive.childBox)) {
      	if (this.lblActive.childBox && this.lblActive.childBox.id != 'img') this.lblActive.childBox.hide();
      	this.lblActive = this.lblActive.parentLabel;
    }
    else if (e.cbeTarget.childBox || e.cbeTarget.id.indexOf('label')!=-1) {
      e.cbeTarget.color(this.hvrTColor);
      this.lblActive = e.cbeTarget;
      if (this.lblActive.childBox) this.lblActive.childBox.show();
    }
  }
  
  // Constructor Code

  this.paint();
  document.cbe.addEventListener('mousemove', this.mousemoveListener, false, this);

} // end class cbeDropdownMenu

