var DUSPEED = 5;
var DUTIMER = 15;

// main function to handle the mouse events //
function duMenu(id, d) {

  var h = document.getElementById(id + '-duheader');
  var c = document.getElementById(id + '-ducontent');
  clearInterval(c.timer);
  if(d == 1 && (c.offsetHeight < c.maxh || typeof c.maxh == "undefined")){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.top = '-' + c.offsetHeight + 'px';
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){duSlide(c,1)},DUTIMER);
  }else{
    h.timer = setTimeout(function(){duCollapse(c)},50);
  }
}

// collapse the menu //
function duCollapse(c){
  c.timer = setInterval(function(){duSlide(c,-1)},DUTIMER);
}

// cancel the collapse if a user rolls over the dropdown cent //
function duCancelHide(id){
  var h = document.getElementById(id + '-duheader');
  var c = document.getElementById(id + '-ducontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){duSlide(c,1)},DUTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function duSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DUSPEED));
  }else{
    dist = (Math.round(currh / DUSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.top = parseInt(c.style.top.replace('px','')) - parseInt(dist * d) + 'px';
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = (currh / c.maxh)-0.1;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}
