var DDSPEED = 10;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');  
	  
  c.style.left = '3px';
  c.style.top = (12 + h.offsetHeight) + 'px';
  
  if (!c.maxh)
  {
    c.style.opacity = '0';
    c.style.filter = 'alpha(opacity=0)';
    c.style.display = 'block';
    c.style.height = 'auto';
    checkOverflow(h, c, id);
    c.maxh = c.offsetHeight;
    c.maxw = c.offsetWidth;
    c.style.height = '0px';    
    c.style.display = 'none';
    c.style.opacity = '1.0';
    c.style.filter = 'alpha(opacity=100)';  
  }

  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      checkOverflow(h, c, id);
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
  checkOverflow(h, c, id);
}

// collapse the menu //
function ddCollapse(c){
  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');
  c.style.display = "block";
  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;
  var speed = DDSPEED;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / 4));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.display = 'block';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 10 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    if (d != 1)
    {
      c.style.opacity = 0;
      c.style.filter = 'alpha(opacity=0)';
    }
    clearInterval(c.timer);
  }
}

// This is to get the absolute position of a given element.
function getPosition(obj)
{
  var curleft = curtop = 0;
  if (obj.offsetParent) 
  {
    do 
    {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  return [curleft, curtop];
}

function checkOverflow(h, c, id)
{
  var tabberDimensions = getPosition(document.getElementById('tabberlive'));
  
  var tabberWidth = (document.getElementById('tabberlive').offsetWidth < document.getElementById('top').offsetWidth) ? 
    document.getElementById('top').offsetWidth : document.getElementById('tabberlive').offsetWidth;
  var tabberEdge = tabberWidth + tabberDimensions[0];
  var dimensions = getPosition(c);
  
  var cwidth = c.maxw ? c.maxw : h.offsetWidth;
  if (c.offsetWidth && c.offsetWidth > 0) 
  {
    cwidth = c.offsetWidth;
  }
  
  var tabDimensions = getPosition(document.getElementById(id));
  var overflowTab = false;
  if ((tabDimensions[0] + document.getElementById(id).offsetWidth ) > tabberWidth)
  {
    overflowTab = true;
  }
  
  
  var headerEdge = dimensions[0] + cwidth;
  var widthOverflow = false;
  if (overflowTab && headerEdge > tabberEdge)
  {
    // We have overflowed the left edge, we need to wrap it around...  
    widthOverflow = true;
  }
  
  // Check the header
  var heightOverflow = false;
  if (widthOverflow)
  {
    var homeElement = document.getElementById('home');
    var homeDimensions = getPosition(homeElement);
    if (dimensions[1] < (homeDimensions[1] + homeElement.offsetHeight + homeElement.offsetHeight - 15))
    {
      heightOverflow = true;
    }
    
    c.style.left = '-' + (getPosition(h)[0] - homeDimensions[0] - 12) + 'px';
    
    if (heightOverflow)
    {
      c.style.top = (12 + h.offsetHeight + h.offsetHeight) + 'px';
    }
  }
  
}