var DDSPEED = 5;
var DDTIMER = 10;
var CURRENT;
function getUserAgent()
{
    if ( navigator.userAgent.indexOf('IE')!= -1 )
        return true;
    else
        return false;

}
// main function to handle the mouse events //
function ddMenu(id,d,action){

  var h = document.getElementById(id + '-header');
  var c = document.getElementById(id + '-content');

  clearInterval(c.timer);
  if(d == 1){
     if(action=='over'&&id!=CURRENT ){
        hide(CURRENT);
     }
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){
       return
    }
 else if(!c.maxh){
       c.style.display = 'inline';
       c.style.height = 'auto';
       c.maxh = c.offsetHeight;
       c.style.height = '0px';
   }
    h.style.height='30px';
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
  else if(d==-1){

   if(CURRENT!=id){
       if (getUserAgent()) {
            h.removeAttribute('className');
       }else{
            h.removeAttribute('class');
       }

       h.timer = setTimeout(function(){ddCollapse(c)},50);
       stayMenu();
      }



}
}

// 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,action,status){

  var h = document.getElementById(id + '-header');
  var c = document.getElementById(id + '-content');
   if(status==3&&id!=CURRENT){
    hide(CURRENT);
   }
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }

   if ( getUserAgent() ) {
            h.setAttribute('className', 'active');
        }
        else{
            h.setAttribute('class', 'active');
        }





}
function hide(id){
var h = document.getElementById(id + '-header');
var c = document.getElementById(id + '-content');

    clearInterval(c.timer);
    clearTimeout(h.timer);

c.style.height = '0px';
h.timer = setTimeout(function(){ddCollapse(h,c)},50);

  if (getUserAgent()) {
         h.removeAttribute('className');

       }else{
         h.removeAttribute('class');
       }


}




// 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;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  if (!getUserAgent()) {
          c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
       }


    if(c.offsetHeight<3){
        c.style.height=0+'px';
      }
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}
 function  stayMenu(){
   ddMenu(CURRENT,1,'out');
   cancelHide(CURRENT,'out',0);
}

function setCurrentPage(page){
  CURRENT=page;
}
