var DDSPEED = 10;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id);
  var c = document.getElementById(id);
  var a = document.getElementById(id + '-a');
  clearInterval(c.timer);
  if(d == 1){
    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';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
	a.style.color = "#207EC6";
  }else{
    h.timer = setTimeout(function(){ddCollapse(a,c)},50);

  }
}

// collapse the menu //
function ddCollapse(a,c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
  a.style.color = "#363636";
  c.style.visibility = 'hidden';
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id);
  var c = document.getElementById(id);
  var a = document.getElementById(id + '-a');

  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
  a.style.color = "#207EC6";
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){

  c.style.visibility = 'visible';
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round((currh+30) / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  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);
  }
}

function addHighlight(id){
	var a = document.getElementById(id);
	a.style.color = "#207EC6";
}

function removeHighlight(id){
	var a = document.getElementById(id);
	a.style.color = "#363636";
}

function openAward(id){
	
	var i = 1;
	
	for (i = 1; i <= 3; i++)
	{
		var element = document.getElementById('award'+i);
		var link = document.getElementById('award' + i + '_link');
		
		if (i == id)
		{
			element.style.visibility = 'visible';
			element.style.zIndex = '1';
			link.style.color = '#999';
		}
		else
		{
			element.style.visibility= 'hidden';
			element.style.zIndex = '0';
			link.style.color = '#207EC6';
		}
	}
}

var timeout	= 0;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

	addHighlight(id +'-a');
}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime(id)
{
	closetimer = window.setTimeout(mclose, timeout);
		removeHighlight(id +'-a');
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 

