/* SVN $Id: main.js 4 2009-05-08 14:09:17Z jeroen $ */
/* SVN $URL: file:///T:/SOURCE_SVN/WHITE_WebShop/httpdocs/js/main.js $ */

function Main_Initialize()
{
	SIDEBAR_Init();
}

if ('function' == typeof WHITE_AddBodyLoadFunction)
{
  WHITE_AddBodyLoadFunction(Main_Initialize);
}

//##############################################################################
/*# SNIP_SIDEBAR */

function SIDEBAR_Init()
{
  var oForm = WHITE_GetElementFlex('searchform');
  if ('object' == typeof oForm)
  {
    WHITE_InitFieldExplanation('searchfield', '[ zoekterm ]');
    WHITE_AddEvent(oForm, 'submit', WHITE_FormRemoveEplanationsEvent);
    WHITE_ChangeOverImagesInit();
  }
}

/*# /SNIP_SIDEBAR */
/*############################################################################*/
/*# SNIP_TAF */

  function Catalog_TAF_Initialize()
  {
    WHITE_AddEventToClass(document,'CaptchaRefresh','click',Catalog_TAF_RefreshEvent,'a');
  }

  function Catalog_TAF_RefreshEvent(oEvent)
  {
    var oNowDate=new Date();
    var oCaptcha = WHITE_GetElementFlex('captcha');
    if (oCaptcha)
    {
      oCaptcha.src = oCaptcha.src+'&'+oNowDate.getTime();
    }
    return WHITE_CancelEvent(oEvent);
  }

  if ('function' == typeof WHITE_AddBodyLoadFunction)
  {
    WHITE_AddBodyLoadFunction(Catalog_TAF_Initialize);
  }

/*# SNIP_TAF */
/*############################################################################*/
/*# /SNIP_GROUP */

  if ( 'function' == typeof WHITE_AddBodyLoadFunction
       && 'function' == typeof WHITE_UrlGoSelect_Initialize )
  {
    WHITE_AddBodyLoadFunction(WHITE_UrlGoSelect_Initialize);
  }

/*# /SNIP_GROUP */
/*############################################################################*/
/*# SNIP_PRODUCT */

function Catalog_Product_Initialize()
{
  document.iCurrentImage = 0;
  document.aImageList = new Array();
  var aChilds = new Array();

  var oDivWithImages = WHITE_GetElementFlex('imagepreload');
  if ('object' == typeof oDivWithImages)
  {
    aChilds = oDivWithImages.childNodes;
    for(var iImgNr = 0; iImgNr < aChilds.length; iImgNr++)
    {
      var oChild = aChilds[iImgNr];
      if (('object' == typeof oChild) && ('undefined' != typeof oChild.tagName))
      {
        if ('img' == oChild.tagName.toLowerCase())
        {
          var iNewIndex = document.aImageList.length;
          document.aImageList[iNewIndex] = oChild.src;

          if (WHITE_HasClassName(oChild, 'currentselected'))
          {
            document.iCurrentImage = iNewIndex;
          }
        }
      }
    }
  }

  var aThumbNailImgs = WHITE_GetElementsByClass(WHITE_GetElementFlex('detail_thumb'),'thumb_photo','img');
  for(var iImgNr = 0; iImgNr < aChilds.length; iImgNr++)
  {
    WHITE_AddEventToElement(aThumbNailImgs[iImgNr],'click','Catalog_GotoImageNr('+iImgNr+');');
  }
  Catalog_GotoImageNr(document.iCurrentImage);
}

function Catalog_GotoImageNr(iImageNr)
{
  document.iCurrentImage = iImageNr;

  if (document.iCurrentImage < 0)
  {
    document.iCurrentImage = 0;
  }
  if (document.iCurrentImage >= document.aImageList.length)
  {
    document.iCurrentImage = document.aImageList.length - 1;
  }

  var oImageElement = WHITE_GetElementFlex('detailimage');
  if ( 'object' == typeof oImageElement
       && document.aImageList[document.iCurrentImage] )
  {
    if (oImageElement.src != document.aImageList[document.iCurrentImage])
    {
      oImageElement.src = document.aImageList[document.iCurrentImage];
    }
  }

}

if ( 'function' == typeof WHITE_AddBodyLoadFunction )
{
  WHITE_AddBodyLoadFunction(Catalog_Product_Initialize);
}

/*# /SNIP_PRODUCT */
/*############################################################################*/
/*# SNIP_SEARCH */

function MAIN_SearchFormGoogleInit()
{
  var oSearchForm = WHITE_GetElementFlex('searchform');
  WHITE_AddEventToElement(oSearchForm, 'submit', MAIN_SearchFormSubmit);
}

function MAIN_SearchFormSubmit(oEvent)
{
  var oSearchForm = this;
  if ('object' == typeof oSearchForm && 'object' == typeof pageTracker)
  {
    var oSearchBox = WHITE_GetElementFlex('data[search]',oSearchForm);
    if ('object' == typeof oSearchBox && oSearchBox.value)
    {
      pageTracker._trackEvent('Search', 'SideBar Search', oSearchBox.value);
    }
  }
}

if ( 'function' == typeof WHITE_AddBodyLoadFunction )
{
  WHITE_AddBodyLoadFunction(MAIN_SearchFormGoogleInit);
}

/*# /SNIP_SEARCH */
/*############################################################################*/
/*# TIP_PRODUCTS */

function TipSelectChange(oEvent)
{
  sValue = WHITE_GetFormFieldValue(this);
  var sInCartFieldName = this.name.replace("[count]", "[incart]");
  var oCheckBox = WHITE_GetElementFlex(sInCartFieldName);
  if (oCheckBox)
  {
    if (sValue && sValue > 0)
    {
      oCheckBox.checked = 1;
    }
    else
    {
      oCheckBox.checked = 0;
    }
  }
}

function TipInCartChange(oEvent)
{
  var sSelectFieldName = this.name.replace("[incart]", "[count]");
  var oSelect = WHITE_GetElementFlex(sSelectFieldName);
  if (oSelect)
  {
    sSelectValue = WHITE_GetFormFieldValue(oSelect);
    if (this.checked && 0==sSelectValue)
    {
      if (oSelect.prevSelected && 0<oSelect.prevSelected)
      {
        oSelect.selectedIndex = oSelect.prevSelected;
      }
      else
      {
        oSelect.selectedIndex = 1;
      }
    }
    else if (!this.checked && 0<sSelectValue)
    {
      oSelect['prevSelected'] = oSelect.selectedIndex;
      oSelect.selectedIndex = 0;
    }
  }
}

function TIPMATCHING_Initialize()
{
  WHITE_InitializeFolds();
  WHITE_FoldOut('tip_openclose');
  WHITE_AddEventToClass(document, 'tipSelect',        'change',   TipSelectChange);
  WHITE_AddEventToClass(document, 'inCartCheckbox',   'click',    TipInCartChange);
}

if ('function' == typeof WHITE_AddBodyLoadFunction)
{
  WHITE_AddBodyLoadFunction(TIPMATCHING_Initialize);
}



/*# /TIP_PRODUCTS */
/*############################################################################*/
