//#############################################################################
//# Add onload events
//#############################################################################
if ('function' == typeof WHITE_AddBodyLoadFunction)
{
  //# Setup overimages in links
  if ('function' == typeof WHITE_ChangeOverImagesInit)
  {
    WHITE_AddBodyLoadFunction(WHITE_ChangeOverImagesInit);
  }

  //# Create open/close div's intro
  if ('function' == typeof ACA_OpenCloseIntroInit)
  {
    WHITE_AddBodyLoadFunction(ACA_OpenCloseIntroInit);
  }

  //# Create open/close div's pagemenu
  if ('function' == typeof ACA_OpenCloseInit)
  {
    WHITE_AddBodyLoadFunction(ACA_OpenCloseInit);
  }

  //# Resize contentblock
  if ('function' == typeof ACA_ResizeContentDiv)
  {
    WHITE_AddBodyLoadFunction(ACA_ResizeContentDiv);
  }
}

//#############################################################################
//# Create open/close div's intro
//#############################################################################
function ACA_OpenCloseIntroInit()
{
  var aIntroContentDivs = WHITE_GetElementsByClass(document, 'introcontentitems');
  for (var iNr1=0; iNr1 < aIntroContentDivs.length; iNr1++)
  {
    var oIntroContentDiv = aIntroContentDivs[iNr1];
    var aOpenCloseIntroDivs = WHITE_GetElementsByClass(oIntroContentDiv, 'homeitem_title');
    for (var iNr2=0; iNr2 < aOpenCloseIntroDivs.length; iNr2++)
    {
      var oOpenCloseIntroDiv = aOpenCloseIntroDivs[iNr2];
      oOpenCloseIntroDiv['holder'] = oIntroContentDiv;
      WHITE_AddEvent(oOpenCloseIntroDiv, 'mouseover', ACA_MouseOverIntroEvent);
    }
  }
}

function ACA_MouseOverIntroEvent() {
  var oIntroContentDiv = this.holder;
  //# First close all contentdivs
  var aOpenCloseIntroTitleDivs = WHITE_GetElementsByClass(oIntroContentDiv, 'homeitem_content');
  for (var iNr=0; iNr < aOpenCloseIntroTitleDivs.length; iNr++)
  {
    var oOpenCloseIntroTitleDiv = aOpenCloseIntroTitleDivs[iNr];
    WHITE_AddClassName(oOpenCloseIntroTitleDiv, 'hidden');
  }

  //# Then remove highlight on titles
  var aOpenCloseIntroContentDivs = WHITE_GetElementsByClass(oIntroContentDiv, 'homeitem_title');
  for (var iNr=0; iNr < aOpenCloseIntroContentDivs.length; iNr++)
  {
    var oOpenCloseIntroContentDiv = aOpenCloseIntroContentDivs[iNr];
    WHITE_RemoveClassName(oOpenCloseIntroContentDiv, 'homeitem_title_open');
  }

  //# Highlight own item
  WHITE_AddClassName(this, 'homeitem_title_open');

  //# Open contentdiv
  var oIntroContentDiv = WHITE_GetElementFlex(this.id + 'content');
  if ('object' == typeof oIntroContentDiv)
  {
    WHITE_RemoveClassName(oIntroContentDiv, 'hidden');
  }
}

//#############################################################################
//# Create open/close div's pagemenu
//#############################################################################
function ACA_OpenCloseInit()
{
  var aOpenCloseDiv = WHITE_GetElementsByClass(document, 'buttonopenclose');
  for (var iNr=0; iNr < aOpenCloseDiv.length; iNr++)
  {
    var oOpenCloseDiv = aOpenCloseDiv[iNr];
    if ('object' == typeof oOpenCloseDiv)
    {
      WHITE_AddEvent(oOpenCloseDiv, 'mouseover', ACA_MouseOverEvent);
      WHITE_AddEvent(oOpenCloseDiv, 'mouseout', ACA_MouseOutEvent);

      if (WHITE_HasClassName(oOpenCloseDiv,'menuitem'))
      {
        oOpenCloseDiv.sClassNameOn = 'menuitem_on';
      }
      else if (WHITE_HasClassName(oOpenCloseDiv,'intromenuitem'))
      {
        oOpenCloseDiv.sClassNameOn = 'intromenuitem_on';
      }

      var oOpenCloseFoldOud = WHITE_GetElementFlex(oOpenCloseDiv.id + '_foldout');
      if ('object' == typeof oOpenCloseFoldOud)
      {
        oOpenCloseDiv.FoldOut = oOpenCloseFoldOud;

        if (WHITE_HasClassName(oOpenCloseFoldOud,'hideme'))
        {
          ACA_OpenClose(oOpenCloseDiv, false, false);
          WHITE_RemoveClassName(oOpenCloseFoldOud, 'hideme');
        }
        else
        {
          ACA_OpenClose(oOpenCloseDiv, true, false);
        }
      }
      else
      {
        oOpenCloseDiv.FoldOut = false;
        WHITE_AddEvent(oOpenCloseDiv, 'click', ACA_OpenEvent);
      }
    }
  }
}

function ACA_MouseOverEvent()
{
  var oOpenCloseButton = this;
  if ('object' == typeof oOpenCloseButton)
  {
    if (WHITE_HasClassName(oOpenCloseButton, 'menuitem')
        && !WHITE_HasClassName(oOpenCloseButton, 'menuitem_on'))
    {
      WHITE_AddClassName(oOpenCloseButton, 'menuitem_over');
    }

    if (WHITE_HasClassName(oOpenCloseButton, 'intromenuitem')
        && !WHITE_HasClassName(oOpenCloseButton, 'intromenuitem_on'))
    {
      WHITE_AddClassName(oOpenCloseButton, 'intromenuitem_over');
    }
  }
}

function ACA_MouseOutEvent()
{
  var oOpenCloseButton = this;
  if ('object' == typeof oOpenCloseButton)
  {
    if (WHITE_HasClassName(oOpenCloseButton, 'menuitem')
        && !WHITE_HasClassName(oOpenCloseButton, 'menuitem_on'))
    {
      WHITE_RemoveClassName(oOpenCloseButton, 'menuitem_over');
    }

    if (WHITE_HasClassName(oOpenCloseButton, 'intromenuitem')
        && !WHITE_HasClassName(oOpenCloseButton, 'intromenuitem_on'))
    {
      WHITE_RemoveClassName(oOpenCloseButton, 'intromenuitem_over');
    }
  }
}

function ACA_OpenEvent()
{
  var oOpenCloseButton = this;
  if ('object' == typeof oOpenCloseButton)
  {
    var sGroupClassName = oOpenCloseButton.className;
    sGroupClassName = sGroupClassName.split(" ",1);

    // First close all open divs
    var aOpenButton = WHITE_GetElementsByClass(document, oOpenCloseButton.sClassNameOn);
    for (var iNr=0; iNr < aOpenButton.length; iNr++)
    {
      var oOpenButton = aOpenButton[iNr];
      if ('object' == typeof oOpenButton)
      {
        if (WHITE_HasClassName(oOpenButton, sGroupClassName))
        {
          ACA_OpenClose(oOpenButton, false, false);
        }
      }
    }

    ACA_OpenClose(oOpenCloseButton, true, true);
  }
}

function ACA_CloseEvent()
{
  var oOpenCloseButton = this;
  if ('object' == typeof oOpenCloseButton)
  {
    ACA_OpenClose(oOpenCloseButton, false, false);
    ACA_ResizeContentDiv();
  }
}

// Use true for bOpen to open the "oOpenCloseButton.id+foldout" div
function ACA_OpenClose(oOpenCloseButton, bOpen, bSlide)
{
  if ('object' == typeof oOpenCloseButton)
  {
    if ('object' == typeof oOpenCloseButton.FoldOut)
    {
      var oOpenCloseFoldOud = oOpenCloseButton.FoldOut;
      if (bOpen)
      {
        WHITE_AddClassName(oOpenCloseButton, oOpenCloseButton.sClassNameOn);

        WHITE_RemoveClassName(oOpenCloseFoldOud, 'hidden');

        if (bSlide)
        {
          ACA_SlideOpen(oOpenCloseFoldOud, false, 1, 1);
        }

        WHITE_RemoveEvent(oOpenCloseButton, 'click', ACA_OpenEvent);
        WHITE_AddEvent(oOpenCloseButton, 'click', ACA_CloseEvent);
      }
      else
      {
        WHITE_RemoveClassName(oOpenCloseButton, oOpenCloseButton.sClassNameOn);

        WHITE_AddClassName(oOpenCloseFoldOud, 'hidden');
        WHITE_RemoveClassName(oOpenCloseButton, 'menuitem_over');

        WHITE_RemoveEvent(oOpenCloseButton, 'click', ACA_CloseEvent);
        WHITE_AddEvent(oOpenCloseButton, 'click', ACA_OpenEvent);
      }
    }
  }
}

function ACA_SlideOpen(oElement, iToHeight, iFromHeight, iNewHeight)
{
  if ('object' == typeof oElement)
  {
    if (false == iToHeight)
    {
      //# Get original height of element and extract paddings and borders
      var iPaddingTop        = WHITE_EXTRA_GetGlobalStyle(oElement, 'paddingTop',        'padding-top');
      var iPaddingBottom     = WHITE_EXTRA_GetGlobalStyle(oElement, 'paddingBottom',     'padding-bottom');
      var iBorderWidth       = WHITE_EXTRA_GetGlobalStyle(oElement, 'borderWidth',    'border-width');
      iToHeight = oElement.offsetHeight
                  - iPaddingTop.replace(/px/,'')
                  - iPaddingBottom.replace(/px/,'')
                  - 2;//iBorderWidth.replace(/px/,'') * 2;
      //# Set height to 1px to start sliding
      oElement.style.height = '1px';
    }

    //# Slide open with 4px every 10ms
    iNewHeight += 4;
    if (iNewHeight > iToHeight)
    {
      iNewHeight = iToHeight;
    }
    oElement.style.height = iNewHeight + 'px';
    ACA_ResizeContentDiv();
    
    //# Recursive if element is not max open
    if (iToHeight != iNewHeight)
    {
      setTimeout(function() {ACA_SlideOpen(oElement, iToHeight, iFromHeight, iNewHeight);}, 10);
    }
  }
}

/*#############################################################################
# Get global defined style (style in external stylesheet)
#############################################################################*/
function WHITE_EXTRA_GetGlobalStyle(oElement, sCSSProperty, sCSSPropertyNS)
{
  //### http://www.javascriptkit.com/dhtmltutors/dhtmlcascade4.shtml
  var mStyleValue = false;

  //# For IE
  if (oElement.currentStyle)
  {
    mStyleValue = oElement.currentStyle[sCSSProperty];
  }
  //# For FireFox
  else if (window.getComputedStyle)
  {
    var oElementStyle=window.getComputedStyle(oElement, "");
    mStyleValue = oElementStyle.getPropertyValue(sCSSPropertyNS);
  }

  return mStyleValue;
}

//#############################################################################
//# Resize ContentDiv to full windows size
//#############################################################################
function ACA_ResizeContentDiv()
{
  var oSiteDiv = WHITE_GetElementFlex('site');
  if ('object' == typeof oSiteDiv)
  {
    oSiteDiv.style.minHeight = '100%';
  }

  var oFullScreen = WHITE_GetElementFlex('fullscreen');
  if ('object' == typeof oFullScreen)
  {
    var fFullScreenHeight = (oFullScreen.offsetHeight);
    oSiteDiv.style.minHeight = fFullScreenHeight + 'px';
  }
}