
window.rootDir = '';
window.lightUpButtons = new Array();
window.lightDownButtons = new Array();
window.timeoutActive = false;
window.inactiveButtonRed = 64;
window.inactiveButtonGreen = 64;
window.inactiveButtonBlue = 128;
window.activeButtonRed = 242;
window.activeButtonGreen = 242;
window.activeButtonBlue = 255;
window.maxLightIndex = 25;
window.logoDiv = null;
window.logoDivIndex = 0;
window.browserSupportsTransparency;


function isBrowserIE6()
{
  return (
      (BrowserDetect.browser == "Explorer")
      &&
      (BrowserDetect.version == 6)
      );
}

function doesBrowserSupportTransparency()
{
  // Some problems are just there. For example, Linux browsers
  // seem to be unbelivabe slow in opacity, see also
  // http://blogs.atlassian.com/developer/2006/12/opacity_css_seems_to_kill_perf.html

  //alert(BrowserDetect.OS + ", " + BrowserDetect.browser + ", " + BrowserDetect.version);

  if (BrowserDetect.OS == 'Linux')
  {
    return false;
  }
  else if (BrowserDetect.OS == 'Mac')
  {
    if (
        (BrowserDetect.browser == "OmniWeb")
        &&
        (BrowserDetect.version >= 613)
       )
    {
      return true;
    }
    else if (
        (BrowserDetect.browser == "Safari")
        &&
        (BrowserDetect.version >= 523.15)
        )
    {
      return true;
    }
    else if (
        (BrowserDetect.browser == "Firefox")
        &&
        (BrowserDetect.version >= 2)
        )
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  else if (BrowserDetect.OS == 'Windows')
  {
    if (
        (BrowserDetect.browser == "Firefox")
        &&
        (BrowserDetect.version >= 2)
       )
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  else
  {
    return false;
  }
}


function createIE6Png(img)
{
  var imgStyle = "display:inline-block;" + img.style.cssText 

    var strNewHTML = '<span style="width:' + img.width + 'px; height:'
    + img.height + 'px;' + imgStyle + ';'
    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
    + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";

  img.outerHTML = strNewHTML;
}


function init(moveLogo)
{
  window.logoDiv = document.getElementById("SynapConLogo");
  window.logoDiv2 = document.getElementById("SynapConLogo2");

  if (isBrowserIE6() == true)
  {
    createIE6Png(window.logoDiv.childNodes[0]);
    createIE6Png(window.logoDiv2.childNodes[0]);

    createIE6Png(document.getElementById("n1"));
    createIE6Png(document.getElementById("n2"));
    createIE6Png(document.getElementById("n3"));
    createIE6Png(document.getElementById("n4"));
    createIE6Png(document.getElementById("n5"));

    if (document.getElementById("ec1") != null)
    {
      createIE6Png(document.getElementById("ec1"));
      createIE6Png(document.getElementById("ec2"));
      createIE6Png(document.getElementById("ec3"));
      createIE6Png(document.getElementById("ec4"));
      createIE6Png(document.getElementById("ec5"));
    }

    if (document.getElementById("s1") != null)
    {
      createIE6Png(document.getElementById("s1"));
      createIE6Png(document.getElementById("s2"));
      createIE6Png(document.getElementById("s3"));
    }
  }
  window.browserSupportsTransparency = doesBrowserSupportTransparency();

  var body = document.getElementsByTagName("body")[0];
  initNode(body);

  if ((moveLogo == 1) && (window.browserSupportsTransparency == true))
  {
    window.logoDiv.style.opacity = 0;
    window.logoDiv2.style.opacity = 0;
    setTimeout(showLogo, 500);
  }
}


function showLogo()
{
  if (window.logoDivIndex != 100)
  {
    window.logoDivIndex++;

    var opacity = window.logoDivIndex / 100;
    window.logoDiv.style.opacity = opacity;
    window.logoDiv.style.fitler = "alpha(opacity = " + (opacity * 100) + ")";
    window.logoDiv2.style.opacity = opacity;
    window.logoDiv2.style.fitler = "alpha(opacity = " + (opacity * 100) + ")";

    var x = window.logoDivIndex;
    window.logoDiv.style.left = (200 - 5 * (10-Math.sqrt(x))) + "px";
    window.logoDiv2.style.left = (220 + 5 * (10-Math.sqrt(x))) + "px";

    setTimeout(showLogo, 25);
  }
}


function initNode(node)
{
  if (
          (node.className == "navbuttonwrapper")
          ||
          (node.className == "navbuttonwrappersmall")
          ||
          (node.className == "navbuttonwrapperlarge")
     )
  {
    node.onmouseover=mouseOverButton;
    node.onmouseout=mouseOutButton;

    node.style.backgroundColor
            = "rgb("
            + window.inactiveButtonRed + ", "
            + window.inactiveButtonGreen + ", "
            + window.inactiveButtonBlue + ")";
  }

  var childNodes = node.childNodes;
  if ( (childNodes != null) && (childNodes.length > 0) )
  {
    var childNode = childNodes[0];
    do
    {
      initNode(childNode);
      childNode = childNode.nextSibling;
    }
    while (childNode != null);
  }
}

function getTargetFromEvent(e)
{
  var targ;
  if (!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;

  if (targ != null)
  {
          if (targ.nodeType == 3) // defeat Safari bug
                  targ = targ.parentNode;
  }

  return targ;
}


function findObjectInArray(needle, haystack)
{
  var i,n;
  for (i=0, n=haystack.length; i<n; i++)
  {
    if (haystack[i] == needle)
      return i;
  }
  return -1;
}


function removeObjectFromArray(needle, haystack)
{
  var index = findObjectInArray(needle, haystack);
  if (index > -1)
  {
    haystack.splice(index, 1);
  }
}


function mouseOutButton(e)
{
  var target = getTargetFromEvent(e);

  target.src = window.rootDir + "/images/" + target.name + ".png";

  removeObjectFromArray(target, window.lightUpButtons);
  var index = findObjectInArray(target, window.lightDownButtons);
  if (index == -1)
  {
    if (target.lightIndex == null)
      target.lightIndex = window.maxLightIndex;

    window.lightDownButtons[window.lightDownButtons.length] = target;

    if (window.timeoutActive == false)
    {
      window.timeoutActive = true;
      window.setTimeout("processButtonLights()", 10);
    }
  }
}


function mouseOverButton(e)
{
  var target = getTargetFromEvent(e);

  target.src = window.rootDir + "/images/" + target.name + "-highlighted.png";

  removeObjectFromArray(target, window.lightDownButtons);
  var index = findObjectInArray(target, window.lightUpButtons);
  if (index == -1)
  {
    if (target.lightIndex == null)
      target.lightIndex = 0;

    window.lightUpButtons[window.lightUpButtons.length] = target;

    if (window.timeoutActive == false)
    {
      window.timeoutActive = true;
      window.setTimeout("processButtonLights()", 10);
    }
  }
}


function processButtonLights()
{
  var i=window.lightUpButtons.length-1;
  while (i>=0)
  {
    var img = window.lightUpButtons[i];
    var lightIndex = img.lightIndex;
    if (lightIndex >= window.maxLightIndex)
    {
      window.lightUpButtons.splice(i, 1);
    }
    else
    {
      var redStepSize
              = Math.round((window.activeButtonRed - window.inactiveButtonRed)
                              / window.maxLightIndex);
      var red = window.inactiveButtonRed + redStepSize * lightIndex;

      var greenStepSize
              = Math.round((window.activeButtonGreen-window.inactiveButtonGreen)
                              / window.maxLightIndex);
      var green = window.inactiveButtonGreen + greenStepSize * lightIndex;

      var blueStepSize
              = Math.round((window.activeButtonBlue - window.inactiveButtonBlue)
                              / window.maxLightIndex);
      var blue = window.inactiveButtonBlue + blueStepSize * lightIndex;

      img.parentNode.parentNode.style.backgroundColor
            = "rgb(" + red + ", " + green + ", " + blue + ")";

      img.lightIndex++;
    }
    i--;
  }

  var i=window.lightDownButtons.length-1;
  while (i>=0)
  {
    var img = window.lightDownButtons[i];
    var lightIndex = img.lightIndex;
    if (lightIndex < 0)
    {
      window.lightDownButtons.splice(i, 1);
    }
    else
    {
      var redStepSize
              = Math.round((window.activeButtonRed - window.inactiveButtonRed)
                              / window.maxLightIndex);
      var red = window.inactiveButtonRed + redStepSize * lightIndex;

      var greenStepSize
              = Math.round((window.activeButtonGreen-window.inactiveButtonGreen)
                              / window.maxLightIndex);
      var green = window.inactiveButtonGreen + greenStepSize * lightIndex;

      var blueStepSize
              = Math.round((window.activeButtonBlue - window.inactiveButtonBlue)
                              / window.maxLightIndex);
      var blue = window.inactiveButtonBlue + blueStepSize * lightIndex;

      img.parentNode.parentNode.style.backgroundColor
            = "rgb(" + red + ", " + green + ", " + blue + ")";

      img.lightIndex--;
    }
    i--;
  }

  if (
       (window.lightUpButtons.length > 0)
       ||
       (window.lightDownButtons.length > 0)
     )
  {
    window.setTimeout("processButtonLights()", 10);
  }
  else
  {
    window.timeoutActive = false;
  }
}
