﻿// ========== HOME Page Drop Down Menus ========== 

var DDSPEED = 10;  //10;
var DDTIMER = 15;  //15;
var BROWSER = getBrowser();
//window.alert("Browser=" + BROWSER);

function getBrowser()
{
    var br = navigator.userAgent.toLowerCase();
      
    if (br.indexOf('msie') > -1)
    {
        var v = br.charAt(br.indexOf('msie') + 5);
        if (v > "6")
        {
            return 'ie7';
        }
        else  
        {  
            return 'ie';
        }
    }
  
    if (br.indexOf('safari') > -1) return 'safari'; 
    if (br.indexOf('opera') > -1) return 'ie7';   //return 'opera'; ==>ie7 logic works for opera
    if (br.indexOf('firefox') > -1 || br.indexOf('mozilla') > -1)
    {
        DDSPEED = 8;
        DDTIMER = 10;
        return 'mozilla';
    }
    
    return "ie"; 
}


// main function to handle the mouse events //
function ddMenu(id,sid,d,sz)
{
  var h = document.getElementById(id);
  var c = document.getElementById(sid);
  var p;
  
  if(BROWSER != "ie") clearInterval(c.timer);
  
  if(d == 1)
  {
    if (BROWSER != "ie") clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight) 
       return;
    else 
    {
        if(!c.maxh)
        {
          c.style.display = 'block';
          c.style.height = 'auto';
          c.maxh = c.offsetHeight;
          c.style.height = '0px';
        }  
    }
    var z = document.getElementById(sz);
    p = findPos(z);
    c.style.left = p[0] + "px";
    c.style.visibility = "visible";
    if(BROWSER == "ie")  //< ie7
    {
       c.style.height = c.maxh + "px";   
       c.style.opacity = 100;
       c.style.filter = 'alpha(opacity=100)';   
    }
    else 
    {  
       c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
    }
  }
  else
  {
    if(BROWSER == "ie")  //< ie7
       ddCollapse(c);
    else 
    {
       h.timer = setTimeout(function(){ddCollapse(c)},50);
    }
  }
}

function findPos(o)
{
    var x = y = 0;

    if (o.offsetParent)
    {
        while(1) 
        {
            x += o.offsetLeft;
            y += o.offsetTop;
            if (!o.offsetParent)
                break;
            o = o.offsetParent;    
        }
    }
    else
    {
        if (o.x) x += o.x;
        if (o.y) y += o.y;
    }
    
    return [x,y];
}


// collapse the menu //
function ddCollapse(c)
{
    if(BROWSER == "ie")  //< ie7
    {
        c.style.height = "0px";
        c.style.visibility = "hidden";
    }
    else
    {
        c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
    }
}

// cancel the collapse if a user rolls over the dropdown menu //
function cancelHide(id,sid)
{
  var h = document.getElementById(id);
  var c = document.getElementById(sid);
  
  if(BROWSER == "ie")  //< ie7
  {  
     c.style.height = c.maxh + "px";
     if (c.style.visibility == "hidden") c.style.visibility = "Visible"; 
  }
  else
  {  
     clearTimeout(h.timer);
     clearInterval(c.timer);
     if(c.offsetHeight < c.maxh)
     {
        c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
     }
  }
}

// incrementally expand/contract the dropdown menu 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';
  
  if(BROWSER == "mozilla")
  {
      c.style.MozOpacity = currh / c.maxh;
  }
  else
  {
      c.style.opacity = currh / c.maxh;
      c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  }
  
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1))
  {
    clearInterval(c.timer);
  }
  
  if (c.offsetHeight < 10) 
  { 
      c.style.visibility = "hidden"; 
  }
  else
  {
      if (c.style.visibility == "hidden") c.style.visibility = "Visible";
  }
}
