// Sourced from: http://www.leigeber.com/2008/04/sliding-javascript-dropdown-menu/

var DDSPEED = 5;
var DDTIMER = 10;
var MAX_OPACITY = 95;


function findPosition(oElement) {
    if (typeof (oElement.offsetParent) != 'undefined') {
        for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
            if (oElement.id == 'wrapper')
                return [posX, posY];

            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        return [posX, posY];
    } else {
        return [oElement.x, oElement.y];
    }
}



// main function to handle the mouse events //
function ddMenu(id,d,w){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  //alert(c.style.zIndex);

  //var s = document.getElementById('the_shadow');
  //s.style.zIndex = 1;
    
  //h.style.zIndex = 200;
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if (!c.maxh) {
    //c.style.visibility = 'hidden';
    c.style.display = 'block';
    c.style.position = 'absolute';
    if (w)
        c.style.width = w;
    //alert(findPosition(h));
    //c.style.height = 'auto';
    //alert((c.style.borderWidth));
    c.style.minWidth = h.offsetWidth + h.style.marginLeft + h.style.marginRight + 'px';
      c.maxh = c.offsetHeight;
      c.maxw = c.offsetWidth;
      c.style.left = (findPosition(h)[0] + (h.offsetWidth / 2) - (c.maxw / 2)) + 'px';
      c.style.height = '0px';
      c.style.width = c.maxw;
    }
      h.className = 'selected';
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
} else {

    h.timer = setTimeout(function(){ddCollapse(c,h)},50);
  }
}

// collapse the menu //
function ddCollapse(c, h) {
    if (h)
        h.className = '';
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  h.className = 'selected';
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1){// && d == 1){
    dist = 1;
  }

  newHeight = (currh + (dist * d));
  c.style.height = newHeight > -1 ? newHeight + "px" : "0px";

  var a = 100 - MAX_OPACITY;
  
  c.style.opacity = (currh / c.maxh) - (a > 0 ? (a / 100) : 0);
  c.style.filter = 'alpha(opacity=' + ((currh * 100 / c.maxh) - a) + ')';
  
  if ((currh < 2 && d != 1)) {
      clearInterval(c.timer);
      c.style.opacity = 0;
      c.style.filter = 'alpha(opacity=0)';
      c.style.height = "0px";
  } else if ((currh > (c.maxh - 2) && d == 1)) {
      clearInterval(c.timer);
      c.style.height = c.maxh + "px";
      //c.style.opacity = 0.9;
      //c.style.filter = 'alpha(opacity=90)';
  }
}