function $cart(name) {
  return $('#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContent_ShoppingCart_cartElem_' + name);
}

window.pux = window.pux || {};

pux.initCart = function() {
  var title = $cart('wzdStep0_lblTitle');
  if (title && title.length) {
    // step 1
    // $cart('wzdStep0_lblTitle').after('<p class="cart-custom-info">V košíku jsou vloženy následující položky produkty.</p>');
    $cart('wzdStep0_btnUpdate').appendTo($cart('wzdStep0_btnEmpty').parent());
    $cart('wzdStep0_btnEmpty').parent().wrapInner('<div id="cart-change-buttons" />');
    
    $cart('wzdStep0_gridData td').each(function() {
      if($(this).text().replace(/\s/g, '') == '0,00') {
        $(this).text('');
      }
    });

    if(! $cart('wzdStep0_lblShippingPriceValue').text().match(/[0-9]/) || $cart('wzdStep0_lblShippingPriceValue').text().match(/ 0,00/)) {
      var td = $cart('wzdStep0_lblShippingPriceValue').parents('td:first');
      td.find('span').hide();
      td.prev().find('span').hide();
    }
  }

  $cart('wzdStep4_gridData td').each(function() {
    if($(this).text().replace(/\s/g, '') == '0,00') {
      $(this).text('');
    }
  });
}

$(window).load(function() {
  $('.product-page .item').each(function() {
    var h = $(this).height();
    var i = $(this).find('img');
    if (i.length) {
      var i = $(i.get(0));
      //var m = Math.max(h-i.height()-90, 0);
      i.css('margin-top', Math.round((180 - i.height()) / 2));
    }
  });
});

pux.hideUnavailableSizes = function(childAvailable, available) {
  // available je ve formatu X|X|X|X|X|X
  var data = available.split('|');
  var l = data.length;
  var selectbox = $('.ProductOptionSelector select');
  if (!selectbox.length)
    return;
  selectbox = selectbox.get(0);

  var i = 0;
  // projdi vsechny dostupne velikosti a porovnavej je na shodu se zaskrknutymi
  // dostupnymi velikostmi
  while (i < selectbox.length) {
    var isFound = false;
    for (var t = 0; t < l; t++) {
      // bezne velkosti
      if (selectbox.options[i].text.substr(0, 2) != 've')
        break;

      var cmpKey = selectbox.options[i].text.replace(/^.* ([\/\.,0-9]+)$/, '$1').replace(',', '.');
      if (data[t] == cmpKey) {
        isFound = true;
        break;
      }
    }

    if (! isFound) {
         for (var t = 0; t < l; t++) {
             // Doplnkove velikosti
            var cmpkey = selectbox.options[i].text.replace(/velikost /, '');       
            if (data[t] == cmpkey) {
              isFound = true;
              break;
            }
          } 
        
    }

    if (! isFound) {
      for (var t = 0; t < l; t++) {
        // ostatni textove hodnoty (text v Product option a Selectboxu se musi shodovat)
        if (data[t] == selectbox.options[i].text) {
          isFound = true;
          break;
        }
      }
    }

    if (! isFound) {
      // nenalezeno -> neni dostupne -> odstran ze seznamu
      selectbox.remove(i);

    } else {
      i++;
    }
  }

  $('.AddToCartTextBox').after('ks&nbsp;');
}

$(document).ready(function() {
  $("#slides").slides({
       preload: true,
       play: 5000,
       slideSpeed: 600
  });

  $('.product:nth-child(4n)').addClass('last');

pux.initCart();
});
