// Cateogry Page Scripts

$(document).ready(function() {
    
    // Fix the price formatted by store: 
    
    var ourPrice = '';
    var newPrice = '';
    $('.productPrice').each(function() {
        ourPrice = $(this).text();
        newPrice = ourPrice.replace(',', '');
        fixedPrice = parseFloat(newPrice);
        $(this).text('$' + fixedPrice.toFixed(2))
    });
    
    $('.productsCategory div.item:nth-child(3n+3)').css('border-right','0px');
    $('.productsCategory div.item:nth-child(3n+3)').after('<hr />');

  if ( typeof(splitIsActive) !== "undefined" && splitIsActive === true ) {
    $.get('/SplitPaymentCategory.html', function(html) {
      //html = html.replace(/\{NUM\_PAYMENTS\}/g, numberOfPayments).replace(/\{PAYMENT\_PRICE\}/g, '<span class="price">$'+paymentPrice.toFixed(2)+'</span>');
      $(".productsCategory .item").each(function() {
        ourPrice = $(this).find('.productPrice').text();
        newPrice = ourPrice.replace(',', '').replace('$', '');
        fixedPrice = parseFloat(newPrice);
        var paymentPrice = 0;
        paymentPrice = (fixedPrice * (1 + priceIncrease/100)) / numberOfPayments;
        // Round it to neareast .01
        paymentPrice = Math.round(paymentPrice * 100) / 100;
        $(this).height(530);
        $(this).find(".controls").css('margin-top', '20px');
        $(this).find(".addToCart").attr('src', '/btn_addtocart-small.gif');
        $(this).find(".productName").css({
          "text-align": "center",
          "margin-bottom": "1em"
        });
        var newHtml = html.replace(/\{NUM\_PAYMENTS\}/g, numberOfPayments).replace(/\{PAYMENT\_PRICE\}/g, '<span class="productPrice">$'+paymentPrice.toFixed(2)+'</span>');
        $(this).find(".itemPrice").append(newHtml);
      });
    });
  }
});
