//DROPDOWN NAVIGATION MENUS
//Height of a single state of the three possible sprite states
var HSPRITE = 21;
//Total number of items in the main navigation
var NAVNUM = 5;
//Drop Speeds
var DDSPEED = 7;
var DDTIMER = 2;

//highlight the appropriate tab depending on the page
function pageTab(){
var str = this.window.location.toString();
var thisPage = "";
for(var i=1; i<= NAVNUM; i++){
    var n = document.getElementById('navLink' + i);
    var navLink = n.getAttribute("href");
    //IE gets href att with entire domain, FF does not
    if(navLink.indexOf(".com/") > 0){
    navLink = navLink.substr(navLink.indexOf(".com/")+5);
    }
    navLink = navLink.replace(/\//g,"");

        if(str.indexOf(".com/") > 0){
            thisPage = str.substr(str.indexOf(".com/")+5);
            thisPage = thisPage.substring(0,thisPage.indexOf("/"));
		    if(navLink == thisPage){
		    n.style.backgroundPosition = "0 -" + HSPRITE + "px";
		    return i;
		    } 
        }
    }
}
//page must load first
window.onload = pageTab;

//handle mouse events //
function ddMenu(id,d){
  var h = document.getElementById('navParent' + id);
  var r = document.getElementById('navLink' + id);
  var c = document.getElementById('navChildren' + id);
  var currTab = pageTab();
  clearInterval(c.timer);
  if(d == 1){
    if(currTab != id){
      r.style.backgroundPosition = "0 -" + HSPRITE + "px";
    }
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
	  c.style.position = 'absolute';
	  c.style.zindex = '800';
      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);
        if(currTab != id){
        r.style.backgroundPosition = "0px 0px";
        }
    }
}

// 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('navParent' + id);
  var r = document.getElementById('navLink' + id);
  var c = document.getElementById('navChildren' + id);
  var currTab = pageTab();
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(currTab != id){
    r.style.backgroundPosition = "0 -" + HSPRITE + "px";
  }
  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;
  }
  c.style.height = currh + (dist * d) + 'px';
  // squared this to make opacity fade more slowly
  //c.style.opacity = (currh / c.maxh) * (currh / c.maxh);
  //c.style.filter = 'alpha(opacity=' + ((currh / c.maxh) * (currh / c.maxh) * 100) + ')';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh / c.maxh) * 100 + ')';
  
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }  
}


function snapclose(id){
	c =  document.getElementById('navChildren' + id);
	c.style.height = 0;
	c.style.opacity = 0;
	c.style.filter = 'alpha(opacity=0)';
	return true;
}