//##############################################################################
//# INITIALIZE

function Main_Initialize()
{
  SIDEBAR_Init();
  TITLEBAR_OPENCLOSE_Initialize();
  ROLLOVER_Init();

  //DETAIL_readPreloadedImages();
  DETAIL_addEventsToPrevNext();
  DETAIL_DisabledSelectOptionsFixInit();

  //Initialize popups
  PopupIframesInit();
  if ('function' == typeof CreateSunglassPopup)
  {
    CreateSunglassPopup();
    InitClosePopupWindow();
  }

  WHITE_InitializeFolds();
  WHITE_FoldIn('company');
  WHITE_FoldIn('delivery');
  WHITE_FoldIn('orderremark');

  WHITE_InitAddBookmarkLinks();
}

if ('function' == typeof WHITE_AddBodyLoadFunction)
{
  WHITE_AddBodyLoadFunction(Main_Initialize);
}

//##############################################################################
/*# CATALOG OVERALL */

function TPL_Text(sCode,sLang,sOutputType)
{
  if ('undefined' == typeof sLang)
  {
    sLang = CATALOG_GetLang();
  } 
  if ('undefined' == typeof sOutputType)
  {
    sOutputType = 'html';
  }
  
  var aText = new Object();
  aText['FIELD_SEARCH'] = new Object;
  aText['FIELD_SEARCH']['nl'] = 'Zoeken'; 
  aText['FIELD_SEARCH']['en'] = 'Search'; 
  aText['FIELD_SEARCH']['de'] = 'Suche';

  var sResult = sCode+'['+sLang+']';    
  if ( aText[sCode] 
       && aText[sCode][sLang] )
  {
    sResult = aText[sCode][sLang];
  }  
  if ('html' == sOutputType)
  {
    sResult = WHITE_htmlentities(sResult);
  } 
  return sResult;
}

function CATALOG_GetLang()
{
  var sResult = 'nl';
  var aMatch = document.location.pathname.match(/^\/(\w\w)\//);
  if (aMatch && aMatch[1])
  {
    sResult = aMatch[1].toLowerCase();
  }
  return sResult;
}


/*# /CATALOG OVERALL */
//##############################################################################
/*# SNIP_SIDEBAR */

function SIDEBAR_Init()
{
  var oForm = WHITE_GetElementFlex('searchform');
  if ('object' == typeof oForm)
  {    
    WHITE_InitFieldExplanation('searchfield', TPL_Text('FIELD_SEARCH'));
    WHITE_AddEvent(oForm, 'submit', WHITE_FormRemoveEplanationsEvent);
    WHITE_ChangeOverImagesInit();
  }
}

/*# /SNIP_SIDEBAR */
/*###########################################################################*/
/*# SNIPPET_TITLEBAR_OPENCLOSE */

function TITLEBAR_OPENCLOSE_Initialize()
{
  WHITE_InitializeFolds();
  WHITE_FoldIn('send_retour');
}

/*# SNIPPET_TITLEBAR_OPENCLOSE */
/*###########################################################################*/
/*# SNIPPET_POPUP */

function PopupIframesInit()
{
  //# Defined sizes for popupdiv types
  var aPopupDivSizes = new Array;
  aPopupDivSizes['order'] = new Array;
  aPopupDivSizes['order']['width']  = 604;
  aPopupDivSizes['order']['height'] = 241;
  document['aPopupDivSizes'] = aPopupDivSizes;
}

/*# /SNIPPET_POPUP */
/*###########################################################################*/
/*# 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 */
/*############################################################################*/
/*# SNIPPET_DETAIL */

document.iCurrentImage = 0;

function isReady()
{
  return WHITE_GetBodyLoadCompleted();
}

function DETAIL_addEventsToPrevNext()
{
  var oFirstButton = WHITE_GetElementFlex('detailphotofirst');
  var oPrevButton = WHITE_GetElementFlex('detailphotoprev');
  var oNextButton = WHITE_GetElementFlex('detailphotonext');
  var oLastButton = WHITE_GetElementFlex('detailphotolast');
  if ('object' == typeof oFirstButton)
  {
    WHITE_AddEventToElement(oFirstButton, 'click', gotoFirstImage);
  }
  if ('object' == typeof oPrevButton)
  {
    WHITE_AddEventToElement(oPrevButton, 'click', gotoPrevImage);
  }
  if ('object' == typeof oNextButton)
  {
    WHITE_AddEventToElement(oNextButton, 'click', gotoNextImage);
  }
  if ('object' == typeof oLastButton)
  {
    WHITE_AddEventToElement(oLastButton, 'click', gotoLastImage);
  }
}

function gotoImage(iStep)
{
  if ( 'number' == typeof document.iCurrentImage && document.aImageList)
  {
    document.iCurrentImage += iStep;
    if (document.iCurrentImage < 0)
    {
      document.iCurrentImage = 0;
    }
    if ( document.iCurrentImage >= document.aImageList.length )
    {
      document.iCurrentImage = aImageList.length - 1;
    }
    gotoImageNr(document.iCurrentImage);
  }
}

function isIE()
{
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function gotoImageNr(iNr)
{
  //debugger;
  if ( 'number' == typeof document.iCurrentImage && document.aImageList )
  {
    if ('undefined' != typeof(iNr))
    {
      document.iCurrentImage = iNr;
    }

    var oFirstButton = WHITE_GetElementFlex('detailphotofirst');
    var oPrevButton = WHITE_GetElementFlex('detailphotoprev');
    var oNextButton = WHITE_GetElementFlex('detailphotonext');
    var oLastButton = WHITE_GetElementFlex('detailphotolast');
    if (document.iCurrentImage == 0)
    {
      changePrevNextStatus(oFirstButton, false);
      changePrevNextStatus(oPrevButton, false);
    }
    else
    {
      changePrevNextStatus(oFirstButton, true);
      changePrevNextStatus(oPrevButton, true);
    }

    if ( document.iCurrentImage == document.aImageList.length - 1)
    {
      changePrevNextStatus(oNextButton, false);
      changePrevNextStatus(oLastButton, false);
    }
    else
    {
      changePrevNextStatus(oNextButton, true);
      changePrevNextStatus(oLastButton, true);
    }

    var oCurrentImageIndicator = WHITE_GetElementFlex('detailphotocurrent');
    if ('object' == typeof oCurrentImageIndicator)
    {
      oCurrentImageIndicator.innerHTML = document.iCurrentImage + 1;
    }

    var oImageElement = WHITE_GetElementFlex('detailimage');
    if ('object' == typeof oImageElement)
    {
      if (oImageElement.src != document.aImageList[document.iCurrentImage])
      {
        oImageElement.src = document.aImageList[document.iCurrentImage];
      }
    }

    if ( WHITE_GetWindowLoadCompleted() )
    {
      var oFlashElement = getMovie("detailflashobject");
      if (oFlashElement && document.aImageList[document.iCurrentImage] && document.aImageListDetail[document.iCurrentImage])
      {
        //debugger;
        oFlashElement.setImageUrl(document.aImageListDetail[document.iCurrentImage], document.aImageList[document.iCurrentImage]); // aImageList[iCurrentImage]
      }
    }

    //add index to zoom link
    var oZoomLink = WHITE_GetElementFlex('zoom-1');
    if ('object' == typeof oZoomLink)
    {
      if (-1 == oZoomLink.href.indexOf('index='))
      {
        oZoomLink.href += '&index=' + document.iCurrentImage;
      }
      else
      {
        oZoomLink.href = oZoomLink.href.replace(/index=([0-9]+)/, 'index=' + document.iCurrentImage);
      }
    }
  }
}

function getMovie(movieName)
{
  var oFlashElement = false;
  var bIsIE = isIE();
  if (!oFlashElement)
  {
    try
    {
      if (bIsIE)
      {
        oFlashElement = document.getElementById(movieName);
      }
      else
      {
        oFlashElement = document.getElementById(movieName+"_em");
      }
    }
    catch(e) {}
  }
  if (!oFlashElement)
  {
    try
    {
      if (bIsIE)
      {
        oFlashElement = window[movieName];
      }
      else
      {
        oFlashElement = document[movieName];
      }
    }
    catch(e) {}
  }
  return oFlashElement;
}

function gotoFirstImage(oEvent)
{
  gotoImageNr(0);
  return true;
}

function gotoPrevImage(oEvent)
{
  gotoImage(-1);
  return true;
}

function gotoNextImage(oEvent)
{
  gotoImage(1);
  return true;
}

function gotoLastImage(oEvent)
{
  if (document.aImageList)
  {
    gotoImageNr(document.aImageList.length - 1);
  }
  return true;
}

function changePrevNextStatus(oImage, bStatus)
{
  if ('object' == typeof oImage)
  {
    if (bStatus)
    {
      oImage.src = oImage.src.replace(/(_off)?\.png/, '.png');
      oImage.src = oImage.src.replace(/(_off)?\.gif/, '.gif');
      WHITE_RemoveClassName(oImage, 'imagebuttondisabled');
      WHITE_AddClassName(oImage,    'imagebuttonenabled');
    }
    else
    {
      oImage.src = oImage.src.replace(/(_off)?(_over)?\.png/, '_off.png');
      oImage.src = oImage.src.replace(/(_off)?(_over)?\.gif/, '_off.gif');
      WHITE_RemoveClassName(oImage, 'imagebuttonenabled');
      WHITE_AddClassName(oImage,    'imagebuttondisabled');
    }
  }
}

//flash over info
function showFlashZoom()
{
  var oFlashDiv  = document.getElementById('photoDiv');
  var oDetailDiv = document.getElementById('infoDiv');

  oFlashDiv.style.zIndex = 100;
  oDetailDiv.style.zIndex = 10;
}

function hideFlashZoom()
{
    var oFlashElement = getMovie("detailflashobject");
    
    var oFlashDiv  = document.getElementById('photoDiv');
	  var oDetailDiv = document.getElementById('infoDiv');
	
	  oFlashDiv.style.zIndex = 10;
	  oDetailDiv.style.zIndex = 100;
		  
    if (oFlashElement)
    {
      oFlashElement.hideDetails();
    }
}

function showTab(iTabNr)
{
	var oDetailTabShow 	= document.getElementById('detailTab'+iTabNr);
	var oContentTabShow = document.getElementById('contentTab'+iTabNr);
	
	if (oContentTabShow && oDetailTabShow)
	{
		for(var i=0; i<3; i++)
		{
			oDetailTab 	= document.getElementById('detailTab'+i);
			oContentTab = document.getElementById('contentTab'+i);
			
			WHITE_RemoveClassName(oDetailTab, 'detail_tab_selected');
			WHITE_AddClassName(oContentTab, 'hidden');
		}
		
		WHITE_AddClassName(oDetailTabShow, 'detail_tab_selected');
		WHITE_RemoveClassName(oContentTabShow, 'hidden');	
	}
}

//// Emulate disabled select options in IE7
// inspired on http://www.lattimore.id.au/2005/07/01/select-option-disabled-and-the-javascript-solution/

function DETAIL_DisabledSelectOptionsFixInit()
{
  if (navigator && navigator.appName && navigator.appVersion)
  {
    var fBrowserVersion=parseFloat(navigator.appVersion);
    if ( navigator.appName=="Microsoft Internet Explorer"
         && fBrowserVersion < 8
         && document.getElementsByTagName)
  	{
  		var aSelects = document.getElementsByTagName("select");
  		if (aSelects.length > 0)
  		{
  			window.DETAIL_DisabledSelectOptionsFix_current = new Array();
  			for (var iS=0; iS < aSelects.length; iS++)
  			{
  				var oSelect = aSelects[iS];
  			  window.DETAIL_DisabledSelectOptionsFix_current[oSelect.id] = oSelect.selectedIndex;
  				WHITE_AddEvent(oSelect,'change',DETAIL_DisabledSelectOptionsRestoreEvent); // OGONE FIX
        	for (var iO=0; iO < oSelect.options.length ;iO++)
        	{
        		var oOption = oSelect.options[iO];
        	  if (oOption.disabled)
        		{
        			oOption.style.color = "graytext";
        		}
        		else
        		{
        			oOption.style.color = "menutext";
        		}
        	}
  			}
  		}
  	}
  }
}

//function DETAIL_DisabledSelectOptionsRestore(oSelect)
function DETAIL_DisabledSelectOptionsRestoreEvent(oEvent)
{
	var oSelect = this;
  if (oSelect.options[oSelect.selectedIndex].disabled)
	{
		oSelect.selectedIndex = window.DETAIL_DisabledSelectOptionsFix_current[oSelect.id];
	}
	else // enabled
	{
		window.DETAIL_DisabledSelectOptionsFix_current[oSelect.id] = oSelect.selectedIndex;
	}
}

/*# /ADVSEARCH */
/*###########################################################################*/
/*# ROLLOVER */

function ROLLOVER_Init()
{
	//Grouppage products
	WHITE_AddEventToClass('productblockholder', 'rollover', 'mouseover', showImageTwo, 'a');
	WHITE_AddEventToClass('productblockholder', 'rollover', 'mouseout', showImageOne, 'a');

	//Matching products
	WHITE_AddEventToClass('matching_foldout', 'rollover', 'mouseover', showImageTwo, 'a');
	WHITE_AddEventToClass('matching_foldout', 'rollover', 'mouseout', showImageOne, 'a');
	
	/*
	var oParent 				= WHITE_GetElementFlex('productblockholder');
	var aOverviewImages = WHITE_GetElementsByClass(oParent, 'rollover', 'a');
	
	for (var iR=0; iR < aOverviewImages.length; iR++)
	{
		var oProduct = aOverviewImages[iR];
		WHITE_AddEvent(oProduct, 'mouseover', showImageTwo);
		WHITE_AddEvent(oProduct, 'mouseout', showImageOne);
	}
	*/
}

function showImageOne(oEvent)
{
	var oRollover 			= this;
	var aImage 					= WHITE_GetElementsByClass(oRollover, 'productphoto', 'img');
	var oImage					= aImage[0];
	var aPreLoadImages 	= WHITE_GetElementsByTagName('imagepreload1', 'img');
	
	for (var iP=0; iP < aPreLoadImages.length; iP++)
	{
		var oPreloadImage = aPreLoadImages[iP];
		if (oPreloadImage.id == oImage.id)
		{
			oImage.src = oPreloadImage.src;
		}
	}
}

function showImageTwo(oEvent)
{
	var oRollover 			= this;
	var aImage 					= WHITE_GetElementsByClass(oRollover, 'productphoto', 'img');
	var oImage					= aImage[0];
	var aPreLoadImages 	= WHITE_GetElementsByTagName('imagepreload2', 'img');
	
	for (var iP=0; iP < aPreLoadImages.length; iP++)
	{
		var oPreloadImage = aPreLoadImages[iP];
		if (oPreloadImage.id == oImage.id)
		{
			oImage.src = oPreloadImage.src;
		}
	}
}

/*# /ROLLOVER */
/*###########################################################################*/
/*# SNIP_TIPPRODUCTS+MATCHING */

  function CloseAllTipProductPopupDivs()
  {
    var aAllPopupDivs = WHITE_GetElementsByClass(document, 'tipproductpopup', 'div');
    for (var iNr=0; iNr < aAllPopupDivs.length; iNr++)
    {
      WHITE_AddClassName(aAllPopupDivs[iNr], 'hidden');
    }
  }

  /*
  function TipProductHoverDivOn()
  {
    CloseAllTipProductPopupDivs();
    var oHoverDiv = this;
    if ('object' == typeof oHoverDiv)
    {
      var iSourceCode = oHoverDiv.id.substring(15);
      var sTargetId   = 'tipproductpopup' + iSourceCode;
      var oPopupDiv = WHITE_GetElementFlex(sTargetId);
      if ('object' == typeof oPopupDiv)
      {
        WHITE_RemoveClassName(oPopupDiv, 'hidden');
      }
    }
  }

  function TipProductHoverDivOff()
  {
    CloseAllTipProductPopupDivs();
  }
	*/
  
  function TipSelectChange(oEvent)
  {
    var 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_FoldIn('tip_openclose');
    WHITE_FoldOut('match_openclose');
    WHITE_AddEventToClass(document, 'tipSelect',        'change',    TipSelectChange,       'select');
    WHITE_AddEventToClass(document, 'inCartCheckbox',   'click',     TipInCartChange,       'input');
    WHITE_ChangeOverImagesInit();
  }

  if ('function' == typeof WHITE_AddBodyLoadFunction)
  {
    WHITE_AddBodyLoadFunction(TIPMATCHING_Initialize);
  }

/*# SNIP_TIPPRODUCTS+MATCHING */
/*###########################################################################*/
/*# SNIP_GROUP */

function GROUP_Initialize()
{
  WHITE_AddEventToClass(document,'UrlGoSelect','change',UrlGoSelectChangeEvent,'select');
}

function UrlGoSelectChangeEvent(oEvent)
{
  var oSelect = false;
  if ('object' == typeof this && this.type && 'select-one' == this.type)
  {
    oSelect = this;
  }
  if (oSelect)
  {
    if (oSelect.options[oSelect.selectedIndex] && oSelect.options[oSelect.selectedIndex].value)
    {
      document.location = oSelect.options[oSelect.selectedIndex].value;
    }
  }
  return WHITE_CancelEvent(oEvent);
}

if ('function' == typeof WHITE_AddBodyLoadFunction)
{
  WHITE_AddBodyLoadFunction(GROUP_Initialize);
}
  
/*# SNIP_GROUP */
/*###########################################################################*/