var DDSPEED = 5;		//
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id,d){				//d is +1 or -1.
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');	//c is the whole list
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}	//if meun is already expanded and you roll back up to hover over dt title so don't have to do anything because menu is already slid out.
    else if(!c.maxh){				//menu is not slid out
      c.style.display = 'block';	//display list as block
      c.style.height = 'auto';		//let content be as high as needed
      c.maxh = c.offsetHeight;		//once slide out height determined, assign a variable to it.
      c.style.height = '0px';		//set height to 0 before send to ddSlide to slide out.
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);		//every 15ms (DDTIMER) slide out another pixel.
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);			//if d = -1 and mouse is out and not over dd content, start collapse in 50 ms
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);		//every 15 ms slide up and hide another list item
}

// cancel the collapse if a user rolls over the dropdown or rolls out and back on very fast//
function cancelHide(id){		//only called from mouseout event
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);		//clear the timeout started when mouse left the list header
  clearInterval(c.timer);		//clear rollup timer because it has to be reset to roll out.
  if(c.offsetHeight < c.maxh){			//e.g. if cursor goes off menu and back fast enough so menu has slid halfway up. Catch it and slide it back down.
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);	//set timer to slide the partially folded menu back out. Same setting as above in ddMenu.
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;		//determine how much of slider has rolled out or in.
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));		//This is an arbitray way to define distance to add to slide out at each interval. It gets smaller each time (starts around 14 px and goes down to 1.
  }else{
    dist = (Math.round(currh / DDSPEED));					//this is opossite:  currh gets smaller and smaller going toward 0, since there is less and less to rollup.
  }
  if(dist <= 1 && d == 1){
    dist = 1;				//if d = 1-, then dist continues to decline to 0, which is good for closing a menu.
  }
  c.style.height = currh + (dist * d) + 'px';		//dist goes down to 1 so adding one pixel at a time in the end
  c.style.opacity = currh / c.maxh;					
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';		//opacity is a css property: filter:
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){		//when currh is within two px of fully expanded or folded up, terminate the interval loop.
    clearInterval(c.timer);
  }
}