var menuNotPositioned = true;
var menuDisplayed = false;
var timerTenths;
var timerID = null;
var timerRunning = false;
var oneTenth = 100;
var mouseInterval = setInterval("checkMousePosition()", 750);
var mouseIsOut = true;

// ******** actions

function clickNavigation() {
  if (document.getElementById) {
    if (menuDisplayed) {
      hideNavigation();
    } else {
      showNavigation();
    }
  }
}

function showNavigation() 
{
  if (document.getElementById) 
  {
    if (menuNotPositioned == true) 
    {
      menuNotPositioned = false;

	  var top = 0;
	  var parentElement;
	  var element = document.getElementById("searchbar");
	  var parentElement = element.offsetParent;
	  
	  top += element.offsetTop;
	  while (parentElement != null)
	  {
	    top += parentElement.offsetTop;
	    parentElement = parentElement.offsetParent;
	  }
      buttonPositionY = top + 3;
      buttonWidth = document.getElementById("searchbar").clientWidth - 4;
      document.getElementById("flyout").style.cssText = "top: " + buttonPositionY + ";";
      document.getElementById("flyout").className = "changeEdition";   
    }
    else 
    {
      document.getElementById("flyout").className = "changeEdition";          
    }
    menuDisplayed = true;
    mouseIsOut = false;
  }
}

function hideNavigation() {
  stopTimer();
  if (document.getElementById) {
    document.getElementById("flyout").className = "changeEdition hidden";
  }
  menuDisplayed =false;
  mouseIsOut = true;
}

function checkMousePosition() {
  if (mouseIsOut && menuDisplayed) {
    hideNavigation();
  }
}

function setMouseIn() {
  mouseIsOut = false;
}

function setMouseOut() {
  mouseIsOut = true;
}

// ******** Mouseover timer stuff

function initializeTimer() {
  timerTenths = 5;
  stopTimer();
  startTimer();
}

function stopTimer() {
  if (timerRunning) {
    clearTimeout(timerID);
  }
  timerRunning = false;
}

function startTimer() {
  if (timerTenths == 0) {
    stopTimer();
    showNavigation();
  } 
  else {
    timerTenths -= 1;
    timerRunning = true;
    timerID = self.setTimeout("startTimer()", oneTenth);
  }
}

function clearMouseInterval() {
  clearInterval(mouseInterval);
}
