﻿/***** Start Basket preview *****/

function initBasketPreview(vsIdDivBasket, vsIdTargetPosition, vsIdControlOpener) {
  $j("#" + vsIdControlOpener).mouseenter(function () {
    BasketPreview.initPopupPosition(vsIdDivBasket, vsIdTargetPosition);
    BasketPreview.hideAll();
    $j("#" + vsIdDivBasket).fadeIn(500);
  });

  $j("#" + vsIdControlOpener).mouseleave(function () {
    BasketPreview.hideAfterDelay($j("#" + vsIdDivBasket), 500);
  });

  $j("#" + vsIdDivBasket).mouseenter(function () {
    $j(this).data("isOpened", true);
  });

  $j("#" + vsIdDivBasket).mouseleave(function () {
    $j(this).data("isOpened", false);
    BasketPreview.hideAfterDelay($j(this), 500);
  });

  $j("a", $j("#" + vsIdDivBasket)).click(function (e) {
    e.stopPropagation();
  });
}

// Class
function BasketPreview() { }

BasketPreview.initPopupPosition = function (vsIdDivBasket, vsIdTargetPosition) {
  var oTargetPosition = $j("#" + vsIdTargetPosition).position();
  var iPopupWidth = $j("#" + vsIdDivBasket).outerWidth();
  var iPopupHeight = $j("#" + vsIdDivBasket).outerHeight();
  var iPopupX = oTargetPosition.left - iPopupWidth;
  var iPopupY = oTargetPosition.top;

  if (iPopupX < 0) {
    iPopupX = 0;
  }
  if (iPopupY < 0) {
    iPopupY = 0;
  }

  if (iPopupX + iPopupWidth > $j(window).width()) {
    iPopupX = $j(window).width() - iPopupWidth;
  }

  $j("#" + vsIdDivBasket).css("left", iPopupX);
  $j("#" + vsIdDivBasket).css("top", iPopupY);
}

BasketPreview.hideAll = function () {
  $j(".divBasketPreviewContainer").hide();
}

BasketPreview.hideAfterDelay = function (voJElementToHide, viDelay) {
  var iIdTimeout;

  iIdTimeout = setTimeout(function () {
    if (!voJElementToHide.data("isOpened")) {
      voJElementToHide.fadeOut(400);
    }
    clearTimeout(voJElementToHide.data("mouseLeaveTimeoutId"));
  }, viDelay);

  voJElementToHide.data("mouseLeaveTimeoutId", iIdTimeout);
}

/***** End Basket preview *****/

/***** Start Cart Popup preview *****/

var _iIdTimeOutCartPopup;
var _bMouseInPopup = false;
var _bMouseInPopupOpener = false;
var _bDisableCartPopup = false;

function initCartPopup(vsPopupPosition) {
  if ($j("#shopping-cart").length > 0 && $j("#divCartPopup").length > 0) {
    $j("#shopping-cart").mouseenter(function () { _bMouseInPopupOpener = true; CartPopup.initCartPopupPosition(vsPopupPosition); CartPopup.showCartPopupAfterDelay(700); });
    $j("#shopping-cart").mouseleave(function () { _bMouseInPopupOpener = false; CartPopup.initCartPopupPosition(vsPopupPosition); CartPopup.hideCartPopupAfterDelay(800); });

    $j("#divCartPopup").mouseenter(function () { _bMouseInPopup = true; });
    $j("#divCartPopup").mouseleave(function () { _bMouseInPopup = false; CartPopup.hideCartPopupAfterDelay(800); });

    $j(".cartInformationAlign").find("a").click(function () { _bDisableCartPopup = true; $j("#divCartPopup").hide(); });

    $j(window).resize(function () { CartPopup.initCartPopupPosition(vsPopupPosition); });

    CartPopup.initCartPopupPosition(vsPopupPosition);
  }
}

//Class
function CartPopup()
{ }

CartPopup.showCartPopupProductAdding = function () {
  $j(document).ready(function () {
    window.document.body.scrollTop = 0;
    window.document.documentElement.scrollTop = 0;

    CartPopup.showCartPopupHideAfterDelay(2300);
  });
}

CartPopup.showCartPopupAfterDelay = function (viDelay) {
  if (_iIdTimeOutCartPopup) {
    clearTimeout(_iIdTimeOutCartPopup);
  }

  _iIdTimeOutCartPopup = setTimeout(CartPopup.showCartPopup, viDelay);
}

CartPopup.showCartPopup = function (vbForceOpen) {
  if (_iIdTimeOutCartPopup) {
    clearTimeout(_iIdTimeOutCartPopup);
  }

  if (!_bDisableCartPopup && (vbForceOpen || _bMouseInPopup || _bMouseInPopupOpener)) {


    if ($j("#divCartPopupContent").length > 0 && $j("#divCartPopupContent")[0].scrollTo) {
      $j("#divCartPopupContent")[0].scrollTo(0);
    }

    $j("#divCartPopup").fadeIn(800);
  }
}

CartPopup.showCartPopupHideAfterDelay = function (viDelay) {
  CartPopup.showCartPopup(true);

  CartPopup.hideCartPopupAfterDelay(viDelay);
}

CartPopup.hideCartPopupAfterDelay = function (viDelay) {
  if (_iIdTimeOutCartPopup) {
    clearTimeout(_iIdTimeOutCartPopup);
  }
  _iIdTimeOutCartPopup = setTimeout(CartPopup.hideCartPopup, viDelay);
}

CartPopup.hideCartPopup = function (vbForceClose) {
  if (typeof vbForceClose != "boolean") {
    vbForceClose = false; // Fix for firefox
  }

  if (vbForceClose || (!_bMouseInPopup && !_bMouseInPopupOpener)) {
    $j("#divCartPopup").fadeOut(800);
  }
}

CartPopup.initCartPopupPosition = function (vsPosition) {
  var oTargetPosition = $j("#shopping-cart").position();
  var iTargetWidth = $j("#shopping-cart").outerWidth();
  var iTargetHeight = $j("#shopping-cart").outerHeight();
  var iPopupWidth = $j("#divCartPopup").outerWidth();
  var iPopupHeight = $j("#divCartPopup").outerHeight();

  var iPopupX;
  var iPopupY;

  switch (vsPosition.toLowerCase()) {
    case "left-top":
      iPopupX = oTargetPosition.left - iPopupWidth;
      iPopupY = oTargetPosition.top;
      break;

    case "left-bottom":
      iPopupX = oTargetPosition.left - iPopupWidth;
      iPopupY = oTargetPosition.top + iTargetHeight - iPopupHeight;
      break;

    case "right-top":
      iPopupX = oTargetPosition.left + iTargetWidth;
      iPopupY = oTargetPosition.top;
      break;

    case "right-bottom":
      iPopupX = oTargetPosition.left + iTargetWidth;
      iPopupY = oTargetPosition.top + iTargetHeight - iPopupHeight;
      break;

    case "top-left":
      iPopupX = oTargetPosition.left - iPopupWidth;
      iPopupY = oTargetPosition.top - iPopupHeight;
      break;

    case "top-right":
      iPopupX = oTargetPosition.left + iTargetWidth;
      iPopupY = oTargetPosition.top - iPopupHeight;
      break;

    case "bottom-left":
      iPopupX = oTargetPosition.left - iPopupWidth;
      iPopupY = oTargetPosition.top + iTargetHeight;
      break;

    case "bottom-right":
      iPopupX = oTargetPosition.left + iTargetWidth;
      iPopupY = oTargetPosition.top + iTargetHeight;
      break;

    case "bottom-left-inside":
      iPopupX = oTargetPosition.left;
      iPopupY = oTargetPosition.top + iTargetHeight;
      break;

    case "bottom-right-inside":
      iPopupX = oTargetPosition.left - iPopupWidth;
      iPopupY = oTargetPosition.top + iTargetHeight;
      break;
  }

  if (iPopupX >= 0 && iPopupY >= 0) {
    CartPopup.positionPopup(iPopupX, iPopupY);
  }
}

CartPopup.positionPopup = function (viPopupX, viPopupY) {
  if (viPopupX < 0) {
    viPopupX = 0;
  }
  if (viPopupY < 0) {
    viPopupY = 0
  }
  if (viPopupX + $j("#divCartPopup").width() > $j(window).width()) {
    viPopupX = $j(window).width() - $j("#divCartPopup").width();
  }
  // Removed
  /*if (viPopupY + $j("#divCartPopup").height() > $j(window).height()) {
  viPopupY = $j(window).height() - $j("#divCartPopup").height();
  }*/

  $j("#divCartPopup").css("left", viPopupX);
  $j("#divCartPopup").css("top", viPopupY);
}

/***** End Cart Popup preview *****/


/***** Start Cart Ajax Utility *****/

// Class
function kAjaxUtility() {
  // Id of the timeout
  var oIdTimeOut;
}

// Static function of kAjaxUtility class
// Show an ajax loading panel and hide an other control
kAjaxUtility.showLoading = function (vsIdLoading, vsIdControlToHide) {
  var oLoading = document.getElementById(vsIdLoading);
  var oControlToHide = document.getElementById(vsIdControlToHide);
  var oDateNow = new Date();

  if (oControlToHide) {
    oLoading.style.height = oControlToHide.offsetHeight;
    oLoading.style.width = oControlToHide.offsetWidth;
    oControlToHide.style.display = "none";
  }
  oLoading.style.display = "block";

  oLoading.dateSet = oDateNow.getTime();
}

// Static function of kAjaxUtility class
// Hide an ajax loading panel and show an other control
kAjaxUtility.hideLoading = function (vsIdLoading, vsIdControlToShow) {
  var oLoading = document.getElementById(vsIdLoading);
  var oControlToShow = document.getElementById(vsIdControlToShow);
  var oDateNow = new Date();

  if (this.oIdTimeOut) {
    window.clearTimeout(this.oIdTimeOut);
  }

  while (oDateNow - oLoading.dateSet < oLoading.getAttribute("TimerMinDisplayTime")) {
    oDateNow = new Date();
  }
  oLoading.style.display = "none";
  if (oControlToShow) {
    oControlToShow.style.display = "block";
  }
}

// Static function of kAjaxUtility class
// Set a timeout to show a loading when it expired
kAjaxUtility.showLoadingWithTimer = function (vsIdLoading, vsIdControlToHide, timeBeforeShow) {
  var sCodeToExecute = "kAjaxUtility.showLoading('" + vsIdLoading + "','" + vsIdControlToHide + "')";
  this.oIdTimeOut = window.setTimeout(sCodeToExecute, timeBeforeShow);
}

// Static function of kAjaxUtility class
// Clear the timeout setted with kAjaxUtility.setTimer and hide the loading
kAjaxUtility.hideLoadingWithTimer = function (vsIdLoading, vsIdControlToShow, minTimeToShow) {
  var oLoading = document.getElementById(vsIdLoading);
  var oDateNow = new Date();
  var sCodeToExecute = "kAjaxUtility.hideLoading('" + vsIdLoading + "','" + vsIdControlToShow + "')";
  var iTimeBeforeHide;

  if (this.oIdTimeOut) {
    window.clearTimeout(this.oIdTimeOut);
  }

  iTimeBeforeHide = minTimeToShow - (oDateNow - oLoading.dateSet);

  if (iTimeBeforeHide < 0) {
    iTimeBeforeHide = 0;
  }

  this.oIdTimeOut = window.setTimeout(sCodeToExecute, iTimeBeforeHide);
}

// Static function of kAjaxUtility class
// Revalidate the page to restor validator status
kAjaxUtility.validatePage = function () {
  for (i = 0; i < Page_Validators.length; i++) {
    ValidatorValidate(Page_Validators[i]);
  }
  MyValidation();
}

kAjaxUtility.resetAllValidationTextBoxes = function () {
  var oInputs = document.getElementsByTagName("INPUT");

  for (var i = 0; i < oInputs.length; i++) {
    if (oInputs[i].type == "text" && oInputs[i].getAttribute("isKce")) {
      kAjaxUtility.resetValidationTextBox(oInputs[i].id);
    }
  }

  if (typeof ($j) !== "undefined") {
    $j(".ErrorMessageDiv").hide();
  }
}

// Static function of kAjaxUtility class
kAjaxUtility.resetValidationTextBoxes = function (vsIdControls) {
  var oArrayId = vsIdControls.split("|");

  for (var i = 0; i < oArrayId.length; i++) {
    this.resetValidationTextBox(oArrayId[i]);
  }
}

// Static function of kAjaxUtility class
kAjaxUtility.resetValidationTextBox = function (vsIdControl) {
  var oTextBox = document.getElementById(vsIdControl);

  if (oTextBox && self.hideMessage) {
    hideMessage(oTextBox);
  }
}

kAjaxUtility.scrollTop = function () {
  window.document.body.scrollTop = 0;
  window.document.documentElement.scrollTop = 0;
}

kAjaxUtility.ajaxRequestSentRepeater = function (vsIdRepeater, vsEventTarget, vsIdLoadingPanel, vsIdControlToHide) {
  var oLoadingPanel = document.getElementById(vsIdLoadingPanel);
  var sDelay;

  if (vsEventTarget.indexOf(vsIdRepeater) != -1) { // The event come from inside the repeater
    sDelay = oLoadingPanel.getAttribute("InitialDelayTimeInside");
  }
  else { // The event come from outside the repeater
    sDelay = oLoadingPanel.getAttribute("InitialDelayTimeOutSide");
  }

  this.showLoadingWithTimer(vsIdLoadingPanel, vsIdControlToHide, sDelay);
}

kAjaxUtility.ajaxResponseEndRepeater = function (vsIdRepeater, vsEventTarget, vsIdLoadingPanel, vsIdControlToHide) {
  var oLoadingPanel = document.getElementById(vsIdLoadingPanel);
  var sDelay;

  if (vsEventTarget.indexOf(vsIdRepeater) != -1) { // The event come from inside the repeater
    sDelay = oLoadingPanel.getAttribute("MinDisplayTimeInside");
  }
  else { // The event come from outside the repeater
    sDelay = oLoadingPanel.getAttribute("MinDisplayTimeOutside");
  }

  this.hideLoadingWithTimer(vsIdLoadingPanel, vsIdControlToHide, sDelay);
}

kAjaxUtility.waitBeforeSendAjaxRequest = function (voElement, vsCallbackFunction, viWaitTime) {
  var sScript;

  if (!viWaitTime || viWaitTime == 0) {
    viWaitTime = 1000;
  }

  sScript = "kAjaxUtility._waitBeforeSendAjaxRequestTimeOutCompleted('" + voElement.id + "', '" + kAjaxUtility._escapeStringQuote(vsCallbackFunction, false) + "');"

  kAjaxUtility._waitBeforeSendAjaxRequestClearTimeout(voElement);

  voElement.setAttribute("waitTimeoutId", setTimeout(sScript, viWaitTime));
}

kAjaxUtility._waitBeforeSendAjaxRequestTimeOutCompleted = function (vsIdElement, vsCallbackFunction) {
  var oElement = document.getElementById(vsIdElement);

  if (oElement) {
    kAjaxUtility._waitBeforeSendAjaxRequestClearTimeout(oElement);
    eval(vsCallbackFunction);
  }
}

kAjaxUtility._waitBeforeSendAjaxRequestClearTimeout = function (voElement) {
  if (voElement && voElement.getAttribute("waitTimeoutId")) {
    clearTimeout(voElement.getAttribute("waitTimeoutId"));
    voElement.setAttribute("waitTimeoutId", null);
  }
}

kAjaxUtility._escapeStringQuote = function (vsString, vbDoubleQuote) {
  if (vbDoubleQuote) {
    return vsString.replace(/"/g, '\\"');
  }
  else {
    return vsString.replace(/'/g, "\\'");
  }
}


/***** End Cart Ajax Utility *****/


/***** Start comparison list *****/

// Class
function ComparisonList() {
}
var _oProductComparisonList;

ComparisonList.init = function () {
  _oProductComparisonList = new ComparisonList();
  _oProductComparisonList.idTimeOutPopup = null;
  _oProductComparisonList.mouseInPopup = false;
  _oProductComparisonList.mouseInPopupOpener = false;
  _oProductComparisonList.disablePopup = false;
  _oProductComparisonList.idProductComparisonButtonToClose = null;

  $(document).ready(function () {
    $(".comparisonButton").mouseenter(function () {
      var iPopupPositionTop;
      var iPopupHeight;
      var iPopupNewPosition;
      var iBtnOffset = $(this).offset();
      var iBtnHeight = $(this).height();
      
      var oJQDivBtnCompare = $("#" + this.getAttribute("idDivBtnCompare"));

      _oProductComparisonList.mouseInPopupOpener = true;

      oJQDivBtnCompare.css("display", "block");
      iPopupPositionTop = oJQDivBtnCompare.offset().top - $j(window).scrollTop();
      iPopupHeight = oJQDivBtnCompare.outerHeight(true);

      if ((iPopupPositionTop + iPopupHeight) > $j(window).height()) {
        iPopupNewPosition = -((oJQDivBtnCompare.offset().top - iBtnOffset.top) + iPopupHeight);
        oJQDivBtnCompare.css("marginTop", iPopupNewPosition);
      }
      else {
        oJQDivBtnCompare.css("marginTop", "");
      }

      oJQDivBtnCompare.css("left", iBtnOffset.left);
      oJQDivBtnCompare.css("top", iBtnOffset.top + iBtnHeight);

      oJQDivBtnCompare.css("display", "none");
      ComparisonList.showPopupAfterDelay(300, this.getAttribute("idDivBtnCompare"));

    }).mouseleave(function () {
      _oProductComparisonList.mouseInPopupOpener = false;
      ComparisonList.hidePopupAfterDelay(300, this.getAttribute("idDivBtnCompare"));
    });

    $j(".divbtnCompare").mouseenter(function () {
      _oProductComparisonList.mouseInPopup = true;
    }).mouseleave(function () {
      _oProductComparisonList.mouseInPopup = false;
      ComparisonList.hidePopupAfterDelay(300, this.id);
    });
  });
}

ComparisonList.showPopupAfterDelay = function(viDelay, vsToShow) 
{
  if (_oProductComparisonList.idTimeOutPopup)
  {
    clearTimeout(_oProductComparisonList.idTimeOutPopup);
  }
  _oProductComparisonList.idTimeOutPopup = setTimeout(function () 
  {
    if (_oProductComparisonList.idTimeOutPopup)
    {
      clearTimeout(_oProductComparisonList.idTimeOutPopup); 
    }
    if (!_oProductComparisonList.disablePopup && (_oProductComparisonList.mouseInPopup || _oProductComparisonList.mouseInPopupOpener)) 
    {
      if (_oProductComparisonList.idProductComparisonButtonToClose) 
      {
        document.getElementById(_oProductComparisonList.idProductComparisonButtonToClose).style.display = "none";
      }
      document.getElementById(vsToShow).style.display = "block";
      _oProductComparisonList.idProductComparisonButtonToClose = vsToShow;
    }
  }, viDelay);
}

ComparisonList.hidePopupAfterDelay = function(viDelay, vsToShow)
{
  if (_oProductComparisonList.idTimeOutPopup)
  {
    clearTimeout(_oProductComparisonList.idTimeOutPopup);
  }

  _oProductComparisonList.idTimeOutPopup = setTimeout(function ()
  {
    if ((!_oProductComparisonList.mouseInPopup && !_oProductComparisonList.mouseInPopupOpener))
    {
      document.getElementById(vsToShow).style.display = "none";
      document.getElementById(vsToShow).style.marginTop = "";
      document.getElementById(vsToShow).style.left = "auto";
    }
  }, viDelay);
}

/***** End comparison list *****/


/***** Start position utility *****/

// Utility functions related to position and dimension of an object in a page
// All these functions are static function of class kPositionUtility and can be called like this:
// kPositionUtility.getPosition(element);

function kPositionUtility() {
  // Class
}

// Return an object with properties: x, y, width, height
kPositionUtility.getPosition = function (el) {
  var oDimension = new Object();
  if (el.getBoundingClientRect) {
    oDimension.x = el.getBoundingClientRect().left + Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
    oDimension.y = el.getBoundingClientRect().top + Math.max(document.body.scrollTop, document.documentElement.scrollTop);
    oDimension.width = el.getBoundingClientRect().right - el.getBoundingClientRect().left;
    oDimension.height = el.getBoundingClientRect().bottom - el.getBoundingClientRect().top;
  }
  else if (document.getBoxObjectFor) {
    oDimension.x = document.getBoxObjectFor(el).x;
    oDimension.y = document.getBoxObjectFor(el).y;
    oDimension.width = document.getBoxObjectFor(el).width;
    oDimension.height = document.getBoxObjectFor(el).height;
  }

  return oDimension;
}

// Return an object with properties : width, height
kPositionUtility.getDocumentSize = function () {
  var oSize = new Object();

  if (document.body.scrollHeight) {
    oSize.height = document.body.scrollHeight;
  }
  else {
    oSize.height = document.documentElement.offsetHeight;
  }

  if (document.body.scrollWidth) {
    oSize.width = document.body.scrollWidth;
  }
  else {
    oSize.width = document.documentElement.offsetWidth;
  }

  return oSize;
}

kPositionUtility.getScreenHeight = function () {
  return (parseInt(kPositionUtility.trueBody().offsetHeight));
}

kPositionUtility.getScreenWidth = function () {
  return (parseInt(kPositionUtility.trueBody().offsetWidth));
}

kPositionUtility.getScrollingDistanceVertical = function () {
  return (parseInt(document.body.scrollTop));
}

kPositionUtility.getScrollingDistanceHorizontal = function () {
  return (parseInt(document.body.scrollLeft));
}

kPositionUtility.trueBody = function () {
  return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

/***** End position utility *****/


/***** Start search *****/

function validateSubmit(voButton) {
  if (voButton.bSubmited == "true") {
    return false;
  }
  else {
    return true;
  }
}

function validateSearch(vsKeyword) {
  var bValid = false;
  var i = 0;
  while (i < vsKeyword.length && !bValid) {
    if (vsKeyword.charAt(i) != " ") {
      bValid = true;
    }
    i++;
  }
  return bValid;
}

var _bQuickSearchRequestSent = false;

function goSearch(vsSearchString, vsSearchPage) {
  if (!_bQuickSearchRequestSent) {
    _bQuickSearchRequestSent = true;
    window.location = vsSearchPage + _URLEncode($j.trim(vsSearchString));
  }
}

function ComboSearch_Init(voCombo, vsSearchDefaultValue, vsPageSearch, vsIdSearchButton) {
  $j(voCombo.get_inputDomElement()).keydown(function (e) {
    if (e.keyCode == 13) {
      if (e.preventDefault) {
        e.preventDefault();
      }
      else {
        e.returnValue = false;
      }
      
      if (validateSearch(voCombo.get_inputDomElement().id, vsSearchDefaultValue)) {
        voCombo.clearItems();
        voCombo.hideDropDown();
        voCombo.get_inputDomElement().setAttribute("preventDropDownOpen", true);
        $j("#" + vsIdSearchButton).click();
      }
    }
    else if (voCombo.get_inputDomElement().value == vsSearchDefaultValue) {
      voCombo.get_inputDomElement().value = "";
    }
  }
  );
}

function ComboSearch_ClientItemsRequesting(combo, eventArgs, size) {
  if (ComboSearch_CanOpenDropDown(combo) && eventArgs.get_text().length < size) {
    if (combo.get_items().get_count() != 0) {
      combo.hideDropDown();
      combo.clearItems();
    }
    eventArgs.set_cancel(true)
  }
  else {
    eventArgs.set_cancel(false);
  }
}

function ComboSearch_ClientItemsRequested(combo, eventArgs) {
  if (combo.get_items().get_count() > 0) {
    if (ComboSearch_CanOpenDropDown(combo) && !combo.get_dropDownVisible()) {
      combo.showDropDown();
    }
  }
  else {
    combo.hideDropDown();
  }
}

function ComboSearch_CanOpenDropDown(combo) {
  var oInput = combo.get_inputDomElement();
  if (oInput && oInput.getAttribute("preventDropDownOpen") && oInput.getAttribute("preventDropDownOpen") == true) {
    return false
  }
  else {
    return true;
  }
}

function ComboSearch_ClientDropDownOpening(combo, eventArgs) {
  if (ComboSearch_CanOpenDropDown(combo) && combo.get_items().get_count() > 0) {
    eventArgs.set_cancel(false);
  }
  else {
    eventArgs.set_cancel(true);
  }
}

function ComboSearch_ClientFocus(combo, vsDefaultText) {
  if (combo.get_text() == vsDefaultText) {
    combo.set_text("");
  }
}

function ComboSearch_ClientBlur(combo, vsDefaultText) {
  if (combo.get_text() == "") {
    combo.set_text(vsDefaultText);
  }
}

function ComboSearch_ClientSelectedIndexChanged(combo, eventArgs, vsIdGoSearch) {
  if (eventArgs.get_item() != "") {
    $j("#" + vsIdGoSearch).click();
  }
}

function ComboSearchButtonClick(vsIdCombo, vsSearchPage, vsDefaultText) {
  var oCombo = $find(vsIdCombo);
  var sValue = oCombo.get_text();

  if (validateSearch(sValue) && sValue != vsDefaultText) {
    goSearch(sValue, vsSearchPage);
  }
}

function _URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
      output += match[1];
      x += match[1].length;
    } else {
      if (clearString.charAt(x) == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


/***** End search *****/

/***** Start LeftMenu Category Loading *****/

function LeftMenuCategoryLoading() { } // Class

LeftMenuCategoryLoading.preloadSubMenus = function (voListSubMenuItems) {
  var iInterval;
  iInterval = setInterval(function () {
    if (voListSubMenuItems && voListSubMenuItems.length > 0) {
      LeftMenuCategoryLoading.initSubMenu(voListSubMenuItems[0]);
      voListSubMenuItems.splice(0, 1);
    }
    else {
      clearInterval(iInterval);
    }
  }, 200);
}

LeftMenuCategoryLoading.initSubMenu = function (voDivSubMenuItem) {
  var oJDivLoading = $j(voDivSubMenuItem).find(".divSubMenuLoading");
  var oIdCategory;

  if (oJDivLoading.length > 0 && !oJDivLoading.attr("loadingInProgress")) {
    oJDivLoading.attr("loadingInProgress", true);
    oIdCategory = LeftMenuCategoryLoading.getElementIdCategoryRecursive(voDivSubMenuItem)

    kCentricBase.Web.Ecommerce.EcomUtilsWS.GetCategoriesForMenu(oIdCategory,
      function (vsResult) {
        if (vsResult && vsResult != "") {
          LeftMenuCategoryLoading.createSubMenu(voDivSubMenuItem, Sys.Serialization.JavaScriptSerializer.deserialize(vsResult, true));
        }
      });
  }
}

LeftMenuCategoryLoading.getElementIdCategoryRecursive = function (voElement) {
  if (voElement) {
    if (voElement.getAttribute && voElement.getAttribute("idc")) {
      return voElement.getAttribute("idc");
    }
    else {
      return LeftMenuCategoryLoading.getElementIdCategoryRecursive(voElement.parentNode);
    }
  }
}

LeftMenuCategoryLoading.createSubMenu = function (voDivSubMenuItem, voMenuStructure) {
  var oJDivSubMenuItem = $j(voDivSubMenuItem);
  var sOutput = "";
  var iColumn;
  var oJULSubMenu;
  var bMustSwitchVisibility = false;

  sOutput += "<div class='scroll-pane'><ul>";

  for (var i = 0; i < voMenuStructure.length; i++) {
    sOutput += "<li><a href='" + voMenuStructure[i].Url + "'>" + voMenuStructure[i].Title + "</a></li>";
  }

  sOutput += "</ul></div>";

  oJDivSubMenuItem.empty();
  oJDivSubMenuItem.append(sOutput);

  oJULSubMenu = oJDivSubMenuItem.parents("ul.subMenu");

  if (oJULSubMenu.css("visibility") == "hidden") {
    bMustSwitchVisibility = true;
  }

  if (bMustSwitchVisibility) {
    oJULSubMenu.css("display", "block").css("visibility", "visible");
  }

  LeftMenuCategoryLoading.initScrollPane(oJDivSubMenuItem.parents("ul.subMenu"));

  if (bMustSwitchVisibility) {
    oJULSubMenu.css("visibility", "");
  }
}

LeftMenuCategoryLoading.initMouseOver = function (voJMainLI) {

  voJMainLI.mouseenter(function () {
    var oJThis = $(this);
    oJThis.data("mouseIn", true);

    setTimeout(function () {
      if (oJThis.data("mouseIn")) {
        var oJUlSubMenu = oJThis.find("ul.subMenu");
        oJUlSubMenu.css("display", "block").css("visibility", "visible");
        LeftMenuCategoryLoading.initScrollPane(oJUlSubMenu);
      }
    }, 300);

  }).mouseleave(function () {
    var oJThis = $(this);
    oJThis.data("mouseIn", false);

    setTimeout(function () {
      if (!oJThis.data("mouseIn") && !oJThis.find("div.jspVerticalBar").data("mouseDown")) {
        if (oJThis.find("ul.subMenu").data("mouseIn") != "true") {
          oJThis.find("ul.subMenu").css("display", "").css("visibility", "");
        }
      }
    }, 300);
  });

  voJMainLI.find("ul.subMenu").mouseenter(function () {
    $(this).data("mouseIn", "true");
  }).mouseleave(function () {
    $(this).data("mouseIn", "false");
  });
}

LeftMenuCategoryLoading.initScrollPane = function (voJULSubMenu) {
  var oJScrollPane;

  if (!voJULSubMenu.data("scrollInitialized")) {
    oJScrollPane = voJULSubMenu.find("div.scroll-pane");
    if (oJScrollPane.length > 0) {
      oJScrollPane.jScrollPane({ showArrows: true });
      voJULSubMenu.data("scrollInitialized", true);
    }

    voJULSubMenu.find("div.jspVerticalBar").mousedown(function () {
      $(this).data("mouseDown", true);
    }).mouseup(function () {
      $(this).data("mouseDown", false);
    });

    $(document).mouseup(function () {
      voJULSubMenu.find("div.jspVerticalBar").data("mouseDown", false);
    });
  }
}

/***** End LeftMenu Category Loading *****/

/***** Start Language Dropdown Title *****/

function BindLanguageTitleDropdowns()
{ }

BindLanguageTitleDropdowns.init = function (vsIdLanguageDropDown, vsIdTitleDropDown, vsLanguageTitlesJson) {
  var oLanguageDropDown = document.getElementById(vsIdLanguageDropDown);
  var oTitleDropDown = document.getElementById(vsIdTitleDropDown);
  var oLanguageTitles;

  if (oLanguageDropDown && oTitleDropDown) {
    oLanguageTitles = Sys.Serialization.JavaScriptSerializer.deserialize(vsLanguageTitlesJson);

    $(oLanguageDropDown).change(function () {
      BindLanguageTitleDropdowns.changeValues(oLanguageDropDown, oTitleDropDown, oLanguageTitles, false);
    });

    BindLanguageTitleDropdowns.changeValues(oLanguageDropDown, oTitleDropDown, oLanguageTitles, true);
  }
}

BindLanguageTitleDropdowns.changeValues = function (voLanguageDropdown, voTitleDropdown, voLanguageTitlesJson, vbIsInit) {
  var iSelectedIndex;
  var sSelectedValue;
  var oJTitleDropDown = $(voTitleDropdown);

  if (vbIsInit) {
    sSelectedValue = voTitleDropdown.options[voTitleDropdown.selectedIndex].value;
  }
  else {
    iSelectedIndex = voTitleDropdown.selectedIndex;
  }

  voTitleDropdown.options.length = 0;

  for (var i = 0; i < voLanguageTitlesJson.length; i++) {
    if (voLanguageTitlesJson[i].Language == voLanguageDropdown.value) {

      for (var j = 0; j < voLanguageTitlesJson[i].Titles.length; j++) {
        oJTitleDropDown.append("<option value='" + voLanguageTitlesJson[i].Titles[j] + "'>" + voLanguageTitlesJson[i].Titles[j] + "</option>");

        if (sSelectedValue && voLanguageTitlesJson[i].Titles[j] == sSelectedValue) {
          iSelectedIndex = j;
        }
      }

      break;
    }
  }
  if (iSelectedIndex == 2) {
    iSelectedIndex = 1;
  }
  voTitleDropdown.selectedIndex = iSelectedIndex;
}

/***** End Language Dropdown Title *****/
